-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
458 lines (377 loc) · 13.1 KB
/
index.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* 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 io = require('socket.io-client');
const wildcardPatch = require('socketio-wildcard')(io.Manager);
const deepEqual = require('fast-deep-equal');
const debug = require('debug')('socketio-v2');
const engineUtil = require('@artilleryio/int-commons').engine_util;
const EngineHttp = require('@artilleryio/int-core').engine_http;
const template = engineUtil.template;
module.exports = SocketIoEngine;
function SocketIoEngine(script) {
this.config = script.config;
this.socketioOpts = this.config.socketio || {};
this.httpDelegate = new EngineHttp(script);
}
SocketIoEngine.prototype.createScenario = function (scenarioSpec, ee) {
const self = this;
// Adds scenario overridden configuration into the static config
this.socketioOpts = { ...this.socketioOpts, ...scenarioSpec.socketio };
const tasks = _.map(scenarioSpec.flow, function (rs) {
if (typeof rs.think !== 'undefined') {
return engineUtil.createThink(
rs,
_.get(self.config, 'defaults.think', {})
);
}
return self.step(rs, ee);
});
return self.compile(tasks, scenarioSpec.flow, ee);
};
function markEndTime(ee, _, startedAt) {
const endedAt = process.hrtime(startedAt);
const delta = endedAt[0] * 1e9 + endedAt[1];
ee.emit('histogram', 'socketio.response_time', delta / 1e6);
}
function isResponseRequired(spec) {
return (
spec.emit && spec.response && (spec.response.channel || spec.response.on)
);
}
function isAcknowledgeRequired(spec) {
return spec.emit && spec.acknowledge;
}
function isValid(data, response) {
if (_.isArray(response.data)) {
//we check if it's an array first (as arrays are objects), and if it's an array, do a deep equality check between both arrays
return deepEqual(data, response.data);
}
if (_.isObject(response.data)) {
//`json` key is added at some point to the response.data object, to use with `captureOrMatch` function
//we should omit it when comparing the response to the data
const expectedResponse = _.omit(response.data, 'json');
const actualResponse = data[data.length - 1]; // if response.data is not an array, we compare it to the last element of the actual response
return deepEqual(actualResponse, expectedResponse);
}
if (_.isString(response.data)) {
const expectedResponse = response.data;
let actualResponse = data[data.length - 1]; // if response.data is not an array, we compare it to the last element of the actual response
// unless the user wants to test against the entire response
if (response.concat) {
actualResponse = data.join('');
}
debug(
`checking if string ${expectedResponse} is a partial match for string ${actualResponse}`
);
return actualResponse.includes(expectedResponse); //we accept a partial match if it's a string
}
debug(`unexpected data type for response.data: ${typeof response.data}`);
return false;
}
function processResponse(ee, data, response, context, callback) {
// Do we have supplied data to validate?
if (response.data && !isValid(data, response)) {
debug('data is not valid:');
debug(data);
debug(response);
const 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...
const 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);
}
if (result !== null) {
// Do we have any failed matches?
const failedMatches = _.filter(result.matches, (v) => {
return !v.success;
});
// How to handle failed matches?
if (failedMatches.length > 0) {
debug(failedMatches);
// TODO: Should log the details of the match somewhere
ee.emit('error', 'Failed match');
return callback(new Error('Failed match'), context);
} else {
// Populate the context with captured values
_.each(result.captures, function (v, k) {
context.vars[k] = v.value;
});
}
// 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) {
const self = this;
if (requestSpec.loop) {
const steps = _.map(requestSpec.loop, function (rs) {
if (!rs.emit && !rs.loop) {
debug(rs);
return self.httpDelegate.step(rs, ee);
}
return self.step(rs, ee);
});
return engineUtil.createLoopWithCount(requestSpec.count || -1, steps, {
loopValue: requestSpec.loopValue,
loopElement: requestSpec.loopElement || '$loopElement',
overValues: requestSpec.over,
whileTrue: self.config.processor
? self.config.processor[requestSpec.whileTrue]
: undefined
});
}
const 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) {
debug(requestSpec);
const delegateFunc = self.httpDelegate.step(requestSpec, ee);
return delegateFunc(context, callback);
}
ee.emit('counter', 'socketio.emit', 1);
ee.emit('rate', 'socketio.emit_rate');
const startedAt = process.hrtime();
const socketio = context.sockets[requestSpec.namespace] || null;
if (!(requestSpec.emit && socketio)) {
debug('invalid arguments');
ee.emit('error', 'invalid arguments');
// TODO: Provide a more helpful message
callback(new Error('socketio: invalid arguments'));
}
const outgoing = requestSpec.emit.channel
? [
template(requestSpec.emit.channel, context),
template(requestSpec.emit.data, context)
]
: Array.from(requestSpec.emit).map((arg) => template(arg, context));
const endCallback = function (err, context, needEmit) {
if (err) {
debug(err);
}
if (isAcknowledgeRequired(requestSpec)) {
const ackCallback = function (...args) {
const response = {
data: template(
requestSpec.acknowledge.data || requestSpec.acknowledge.args,
context
),
capture: template(requestSpec.acknowledge.capture, context),
match: template(requestSpec.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, args, response, context, function (err) {
if (!err) {
markEndTime(ee, context, startedAt);
}
return callback(err, context);
});
};
// Acknowledge required so add callback to emit
if (needEmit) {
socketio.emit(...outgoing, ackCallback);
} else {
ackCallback();
}
} else {
// No acknowledge data is expected, so emit without a listener
if (needEmit) {
socketio.emit(...outgoing);
}
markEndTime(ee, context, startedAt);
return callback(err, context);
}
}; // endCallback
if (isResponseRequired(requestSpec)) {
const response = {
channel: template(
requestSpec.response.channel || requestSpec.response.on,
context
),
concat: template(requestSpec.response.concat, context),
data: template(
requestSpec.response.data || requestSpec.response.args,
context
),
capture: template(requestSpec.response.capture, context),
match: template(requestSpec.response.match, context)
};
// Listen for the socket.io response on the specified channel
let done = false;
let responseData = [];
socketio.on(response.channel, function receive(...args) {
responseData.push(...args);
if (isValid(responseData, response)) {
done = true;
processResponse(ee, responseData, response, context, function (err) {
if (!err) {
markEndTime(ee, context, startedAt);
}
// Stop listening on the response channel
socketio.off(response.channel);
return endCallback(err, context, false);
});
}
});
// Send the data on the specified socket.io channel
socketio.emit(...outgoing);
// If we don't get a response within the timeout, fire an error
const waitTime = (self.config.timeout || 10) * 1000;
setTimeout(function responseTimeout() {
if (!done) {
if (responseData.length) {
processResponse(
ee,
responseData,
response,
context,
function (err) {
if (!err) {
markEndTime(ee, context, startedAt);
}
// Stop listening on the response channel
socketio.off(response.channel);
// called
return endCallback(err, context, false);
}
);
return;
}
const err = 'response timeout';
ee.emit('error', err);
return callback(err, context);
}
}, waitTime);
} else {
endCallback(null, context, true);
}
};
function preStep(context, callback) {
// Set default namespace in emit action
requestSpec.namespace = template(requestSpec.namespace, context) || '';
self.loadContextSocket(requestSpec.namespace, context, function (err) {
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]) {
const target = this.config.target + namespace;
const tls = this.config.tls || {};
const socketioOpts = template(this.socketioOpts, context);
const options = _.extend(
{},
socketioOpts, // templated
tls
);
const 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);
});
socket.once('error', function (err) {
cb(err, socket);
});
} else {
return cb(null, context.sockets[namespace]);
}
};
SocketIoEngine.prototype.closeContextSockets = function (context) {
if (context.sockets && Object.keys(context.sockets).length > 0) {
const namespaces = Object.keys(context.sockets);
namespaces.forEach(function (namespace) {
context.sockets[namespace].disconnect();
});
}
};
SocketIoEngine.prototype.compile = function (tasks, scenarioSpec, ee) {
const 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 = self.httpDelegate.setInitialContext(initialContext);
initialContext._pendingRequests = _.size(
_.reject(scenarioSpec, function (rs) {
return typeof rs.think === 'number';
})
);
const 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);
});
};
};