-
Notifications
You must be signed in to change notification settings - Fork 105
/
engine_socketio.js
366 lines (316 loc) · 10.5 KB
/
engine_socketio.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const async = require('async');
const _ = require('lodash');
const request = require('request');
const io = require('socket.io-client');
const wildcardPatch = require('socketio-wildcard')(io.Manager);
const deepEqual = require('deep-equal');
const debug = require('debug')('socketio');
const engineUtil = require('./engine_util');
const EngineHttp = require('./engine_http');
const template = engineUtil.template;
module.exports = SocketIoEngine;
function SocketIoEngine(script) {
this.config = script.config;
if (script.config.socketio) {
if (script.config.socketio.transports) {
this.transports = {
transports: script.config.socketio.transports
};
}
if (script.config.socketio.query) {
this.query = {
query: script.config.socketio.query
};
}
if (script.config.socketio.path) {
this.path = {
path: script.config.socketio.path
};
}
}
this.httpDelegate = new EngineHttp(script);
}
SocketIoEngine.prototype.createScenario = function(scenarioSpec, ee) {
var self = this;
let tasks = _.map(scenarioSpec.flow, function(rs) {
if (rs.think) {
return engineUtil.createThink(rs, _.get(self.config, 'defaults.think', {}));
}
return self.step(rs, ee);
});
return self.compile(tasks, scenarioSpec.flow, ee);
};
function markEndTime(ee, context, startedAt) {
let endedAt = process.hrtime(startedAt);
let delta = (endedAt[0] * 1e9) + endedAt[1];
ee.emit('response', delta, 0, context._uid);
}
function isResponseRequired(spec) {
return (spec.emit && spec.emit.response && spec.emit.response.channel);
}
function isAcknowledgeRequired(spec) {
return (spec.emit && spec.emit.acknowledge);
}
function processResponse(ee, data, response, context, callback) {
// Do we have supplied data to validate?
if (response.data && !deepEqual(data, response.data)) {
debug(data);
let err = 'data is not valid';
ee.emit('error', err);
return callback(err, context);
}
// If no capture or match specified, then we consider it a success at this point...
if (!response.capture && !response.match) {
return callback(null, context);
}
// Construct the (HTTP) response...
let fauxResponse = {body: JSON.stringify(data)};
// Handle the capture or match clauses...
engineUtil.captureOrMatch(response, fauxResponse, context, function(err, result) {
// Were we unable to invoke captureOrMatch?
if (err) {
debug(data);
ee.emit('error', err);
return callback(err, context);
}
// Do we have any failed matches?
let haveFailedMatches = _.some(result.matches, function(v, k) {
return !v.success;
});
// How to handle failed matches?
if (haveFailedMatches) {
// TODO: Should log the details of the match somewhere
ee.emit('error', 'Failed match');
return callback(new Error('Failed match'), context);
} else {
// Emit match events...
_.each(result.matches, function(v, k) {
ee.emit('match', v.success, {
expected: v.expected,
got: v.got,
expression: v.expression
});
});
// Populate the context with captured values
_.each(result.captures, function(v, k) {
context.vars[k] = v;
});
// Replace the base object context
// Question: Should this be JSON object or String?
context.vars.$ = fauxResponse.body;
// Increment the success count...
context._successCount++;
return callback(null, context);
}
});
}
SocketIoEngine.prototype.step = function (requestSpec, ee) {
let self = this;
if (requestSpec.loop) {
let steps = _.map(requestSpec.loop, function(rs) {
if (!rs.emit) {
return self.httpDelegate.step(rs, ee);
}
return self.step(rs, ee);
});
return engineUtil.createLoopWithCount(
requestSpec.count || -1,
steps,
{
loopValue: requestSpec.loopValue,
overValues: requestSpec.over
}
);
}
let f = function(context, callback) {
// Only process emit requests; delegate the rest to the HTTP engine (or think utility)
if (requestSpec.think) {
return engineUtil.createThink(requestSpec, _.get(self.config, 'defaults.think', {}));
}
if (!requestSpec.emit) {
let delegateFunc = self.httpDelegate.step(requestSpec, ee);
return delegateFunc(context, callback);
}
ee.emit('request');
let startedAt = process.hrtime();
let socketio = context.sockets[requestSpec.emit.namespace] || null;
if (!(requestSpec.emit && requestSpec.emit.channel && socketio)) {
return ee.emit('error', 'invalid arguments');
}
let outgoing = {
channel: template(requestSpec.emit.channel, context),
data: template(requestSpec.emit.data, context)
};
let endCallback = function (err, context) {
if (err) {
debug(err);
}
if (isAcknowledgeRequired(requestSpec)) {
// Acknowledge required so add callback to emit
socketio.emit(outgoing.channel, outgoing.data, function () {
let response = {
data: template(requestSpec.emit.acknowledge.data, context),
capture: template(requestSpec.emit.acknowledge.capture, context),
match: template(requestSpec.emit.acknowledge.match, context)
};
// Make sure data, capture or match has a default json spec for parsing socketio responses
_.each(response, function (r) {
if (_.isPlainObject(r) && !('json' in r)) {
r.json = '$.0'; // Default to the first callback argument
}
});
// Acknowledge data can take up multiple arguments of the emit callback
processResponse(ee, arguments, response, context, function (err) {
if (!err) {
markEndTime(ee, context, startedAt);
}
return callback(err, context);
});
});
} else {
// No acknowledge data is expected, so emit without a listener
socketio.emit(outgoing.channel, outgoing.data);
markEndTime(ee, context, startedAt);
return callback(null, context);
}
};
if (isResponseRequired(requestSpec)) {
let response = {
channel: template(requestSpec.emit.response.channel, context),
data: template(requestSpec.emit.response.data, context),
capture: template(requestSpec.emit.response.capture, context),
match: template(requestSpec.emit.response.match, context)
};
// Listen for the socket.io response on the specified channel
let done = false;
socketio.on(response.channel, function receive(data) {
done = true;
processResponse(ee, data, response, context, function(err) {
if (!err) {
markEndTime(ee, context, startedAt);
}
// Stop listening on the response channel
socketio.off(response.channel);
return endCallback(err, context);
});
});
// Send the data on the specified socket.io channel
socketio.emit(outgoing.channel, outgoing.data);
// If we don't get a response within the timeout, fire an error
let waitTime = self.config.timeout || 10;
waitTime *= 1000;
setTimeout(function responseTimeout() {
if (!done) {
let err = 'response timeout';
ee.emit('error', err);
return callback(err, context);
}
}, waitTime);
} else {
endCallback(null, context);
}
};
function preStep(context, callback){
// Set default namespace in emit action
requestSpec.emit.namespace = template(requestSpec.emit.namespace, context) || "/";
self.loadContextSocket(requestSpec.emit.namespace, context, function(err, socket){
if(err) {
debug(err);
ee.emit('error', err.message);
return callback(err, context);
}
return f(context, callback);
});
}
if(requestSpec.emit) {
return preStep;
} else {
return f;
}
};
SocketIoEngine.prototype.loadContextSocket = function(namespace, context, cb) {
context.sockets = context.sockets || {};
if(!context.sockets[namespace]) {
let target = this.config.target + namespace;
let tls = this.config.tls || {};
let transports = this.transports || {};
let path = template(this.path || {}, context);
let query = template(this.query || {}, context);
let options = _.extend(
{},
tls,
transports,
query,
path
);
let socket = io(target, options);
context.sockets[namespace] = socket;
wildcardPatch(socket);
socket.on('*', function () {
context.__receivedMessageCount++;
});
socket.once('connect', function() {
cb(null, socket);
});
socket.once('connect_error', function(err) {
cb(err, null);
});
} else {
return cb(null, context.sockets[namespace]);
}
};
SocketIoEngine.prototype.closeContextSockets = function (context) {
// if(context.socketio) {
// context.socketio.disconnect();
// }
if(context.sockets && Object.keys(context.sockets).length > 0) {
var namespaces = Object.keys(context.sockets);
namespaces.forEach(function(namespace){
context.sockets[namespace].disconnect();
});
}
};
SocketIoEngine.prototype.compile = function (tasks, scenarioSpec, ee) {
let config = this.config;
let self = this;
function zero(callback, context) {
context.__receivedMessageCount = 0;
ee.emit('started');
self.loadContextSocket('/', context, function done(err) {
if (err) {
ee.emit('error', err);
return callback(err, context);
}
return callback(null, context);
});
}
return function scenario(initialContext, callback) {
initialContext._successCount = 0;
initialContext._jar = request.jar();
initialContext._pendingRequests = _.size(
_.reject(scenarioSpec, function(rs) {
return (typeof rs.think === 'number');
}));
let steps = _.flatten([
function z(cb) {
return zero(cb, initialContext);
},
tasks
]);
async.waterfall(
steps,
function scenarioWaterfallCb(err, context) {
if (err) {
debug(err);
}
if (context) {
self.closeContextSockets(context);
}
return callback(err, context);
});
};
};