-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathrtctwst.js
executable file
·218 lines (203 loc) · 7.64 KB
/
rtctwst.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var child_process = require('child_process'),
Twst = require('twst').Twst,
RtcTwst = null;
// Subclass Twst
exports.RtcTwst = RtcTwst = function(opts) {
Twst.call(this, opts);
this.nextPageIndex = 0;
//rtwst.on('return', function(idx, msg) { console.log('RETURN:', idx, msg); });
//rtwst.on('callback', function(idx, msg) { console.log('CALLBACK:', idx, msg); });
this.on('error', function(idx, msg) {
console.error(idx + ' ERROR:', msg.data);
});
this.on('console.log', function(idx, msg) {
console.log(idx + ' CONSOLE.LOG:', msg.data.join(' '));
});
this.on('console.warn', function(idx, msg) {
console.warn(idx + ' CONSOLE.WARN:', msg.data.join(' '));
});
this.on('console.error', function(idx, msg) {
console.error(idx + ' CONSOLE.ERROR:', msg.data.join(' '));
});
this.on('close', function(idx, msg) {
console.log(idx + ' CLOSE:', msg.data);
});
if (opts.startPages) { this.startPages(opts); }
};
RtcTwst.prototype = Object.create(Twst.prototype);
RtcTwst.prototype.constructor = RtcTwst;
RtcTwst.prototype.cleanup_exit = function(exitCode) {
console.log("Cleaning up and exiting with code " + exitCode);
this.broadcast('window.callPhantom("QUIT")');
setTimeout(function() {
process.exit(exitCode);
}, 1000);
}
RtcTwst.prototype.dockerPage = function(url, opts) {
opts = opts || {};
var self = this,
prefix = opts.prefix || "",
timeout = opts.timeout || 60000;
docker_args = ['run', '-it', '-d', // '--rm',
'-v', process.cwd() + '/test:/test',
'slimerjs-wily',
'slimerjs', '/test/launch.js',
url,
timeout];
console.log(prefix + 'launching slimerjs in docker');
var out = child_process.execFileSync('docker', docker_args,
{encoding: 'utf8'}),
id = out.replace(/[\r\n]*$/,'');
//var page = child_process.spawn('docker', docker_args);
console.log('launched docker container ' + id);
var page = child_process.spawn('docker', ['logs', '-t', '-f', id]);
page.docker_id = id;
page.on('close', function (code) {
console.log(prefix + 'docker logs exited with code ' + code);
if (opts.nocleanup) {
process.exit(exitCode);
} else {
//this.cleanup_exit(code);
}
});
if (opts.verbose) {
page.stdout.on('data', function(chunk) {
var line = chunk.toString('utf8').replace(/\r\n$/,'');
if (line === '') { return; }
console.log(prefix + line);
});
}
return page;
}
RtcTwst.prototype.startPages = function(opts) {
var self = this,
cur_client_cnt = Object.keys(self.clients).length,
prefix = opts.prefix + this.nextPageIndex + ': ';
//console.log("startPages:", opts.clientCount, this.nextPageIndex, cur_client_cnt);
if (cur_client_cnt >= opts.clientCount) {
opts.pagesCallback();
return;
}
if (this.nextPageIndex <= cur_client_cnt) {
console.log('Starting docker client ' + this.nextPageIndex);
self.dockerPage(opts.url, {prefix: prefix,
timeout: opts.timeout,
verbose: opts.verbose});
this.nextPageIndex += 1;
}
setTimeout(function() { self.startPages(opts); }, 100);
}
////////////////////////////////////
// RTC specific
RtcTwst.prototype.get_node_info = function(timeout, callback) {
this.collect(function() {
// Evaluates in page context so 'node' variable is implicit
if (typeof node !== 'undefined' && node) {
var data = {id: node._self.id,
state: node._self.state,
serverMapKeys: Object.keys(node._self.serverMap)};
return data;
} else {
return null;
}
}, {timeout: timeout}, callback);
}
RtcTwst.prototype.get_leader_idx = function(timeout, callback) {
var self = this;
self.get_node_info(timeout, function(status, nodes) {
if (status) {
for (var cid in nodes) {
var node = nodes[cid].data;
if (node.state === 'leader') {
callback(true, cid);
return;
}
}
callback(false, null);
} else {
callback(false, null);
}
});
}
RtcTwst.prototype.wait_cluster_up = function(timeout, callback) {
var self = this,
server_count = Object.keys(self.clients).length,
start_time = Date.now();
var checkfn = function () {
// Gather data from the nodes
self.get_node_info(2000+timeout/10, function(status, nodes) {
var elapsed = Date.now() - start_time;
if (!status) {
console.log("get_node_info timed out");
callback(false, nodes, elapsed)
}
// Pull out some stats
var states = {leader:[], candidate:[], follower:[]},
nodeCnts = [];
for (var i in nodes) {
var node = nodes[i].data;
if (node) {
//console.log("node:", node, "node.state:", node.state);
states[node.state].push(i);
nodeCnts.push(node.serverMapKeys.length);
} else {
nodeCnts.push(0);
}
}
var totalNodeCnt = nodeCnts.reduce(function(a,b) {return a+b}, 0);
console.log('Cluster states: ' + JSON.stringify(states) +
', node counts: ' + JSON.stringify(nodeCnts));
// Exit if cluster is up or we timeout
if (states.leader.length === 1 &&
states.candidate.length === 0 &&
states.follower.length === server_count-1 &&
totalNodeCnt === server_count*server_count) {
callback(true, nodes, elapsed);
} else if (elapsed > timeout) {
callback(false, nodes, elapsed);
} else {
//setTimeout(checkfn, 500);
setTimeout(checkfn, 100);
}
});
}
checkfn();
}
RtcTwst.prototype.wait_cluster_predicate = function(timeout, predicate, callback) {
var self = this,
server_count = Object.keys(self.clients).length,
start_time = Date.now();
var checkfn = function () {
self.collect(predicate, {timeout: timeout}, function(status, data) {
var elapsed = Date.now() - start_time;
if (!status) {
callback(false, data, elapsed)
}
var falseStates = [],
trueStates = [],
trueCnt = 0;
for (var n in data) {
if (data[n].data) {
trueCnt += 1;
trueStates.push(n);
} else {
falseStates.push(n);
}
}
//console.log('Predicate data: ' + JSON.stringify(data));
console.log('Predicate false: ' + JSON.stringify(falseStates) +
', true: ' + JSON.stringify(trueStates) +
', true count: ' + trueCnt);
// Exit if cluster is up or we timeout
if (trueCnt >= server_count) {
callback(true, data, elapsed);
} else if (elapsed > timeout) {
callback(false, data, elapsed);
} else {
setTimeout(checkfn, 50);
//setTimeout(checkfn, 2000);
}
});
}
checkfn();
}