-
Notifications
You must be signed in to change notification settings - Fork 712
Expand file tree
/
Copy pathstream-router.js
More file actions
496 lines (391 loc) · 14.9 KB
/
Copy pathstream-router.js
File metadata and controls
496 lines (391 loc) · 14.9 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var assert = require('assert');
var extend = require('extend');
var through = require('through2');
var util = require('../../lib/common/util.js');
var uuid = require('node-uuid');
describe('streamRouter', function() {
var streamRouter;
var streamRouterOverrides = {};
var UUID = uuid.v1();
function FakeClass() {}
before(function() {
streamRouter = require('../../lib/common/stream-router.js');
var streamRouterCached = extend(true, {}, streamRouter);
Object.keys(streamRouter).forEach(function(streamRouterMethod) {
if (typeof streamRouter[streamRouterMethod] !== 'function') {
return;
}
streamRouter[streamRouterMethod] = function() {
var args = arguments;
if (streamRouterOverrides[streamRouterMethod]) {
return streamRouterOverrides[streamRouterMethod].apply(this, args);
}
return streamRouterCached[streamRouterMethod].apply(this, args);
};
});
});
beforeEach(function() {
FakeClass.prototype = { methodToExtend: function() { return UUID; } };
streamRouterOverrides = {};
});
describe('extend', function() {
it('should overwrite a method on a class', function() {
var originalMethod = FakeClass.prototype.methodToExtend;
streamRouter.extend(FakeClass, 'methodToExtend');
var overwrittenMethod = FakeClass.prototype.methodToExtend;
assert.notEqual(originalMethod, overwrittenMethod);
});
it('should accept an array or string method names', function() {
var originalMethod = FakeClass.prototype.methodToExtend;
FakeClass.prototype.anotherMethodToExtend = function() {};
var anotherMethod = FakeClass.prototype.anotherMethodToExtend;
var methodsToExtend = ['methodToExtend', 'anotherMethodToExtend'];
streamRouter.extend(FakeClass, methodsToExtend);
assert.notEqual(originalMethod, FakeClass.prototype.methodToExtend);
assert.notEqual(anotherMethod, FakeClass.prototype.anotherMethodToExtend);
});
it('should parse the arguments', function(done) {
streamRouterOverrides.parseArguments_ = function(args) {
assert.deepEqual([].slice.call(args), [1, 2, 3]);
done();
};
streamRouterOverrides.router_ = util.noop;
streamRouter.extend(FakeClass, 'methodToExtend');
FakeClass.prototype.methodToExtend(1, 2, 3);
});
it('should call router when the original method is called', function(done) {
var expectedReturnValue = FakeClass.prototype.methodToExtend();
var parsedArguments = { a: 'b', c: 'd' };
streamRouterOverrides.parseArguments_ = function() {
return parsedArguments;
};
streamRouterOverrides.router_ = function(args, originalMethod) {
assert.strictEqual(args, parsedArguments);
assert.equal(originalMethod(), expectedReturnValue);
done();
};
streamRouter.extend(FakeClass, 'methodToExtend');
FakeClass.prototype.methodToExtend();
});
it('should maintain `this` context', function(done) {
FakeClass.prototype.methodToExtend = function() { return this.uuid; };
var cls = new FakeClass();
cls.uuid = uuid.v1();
streamRouterOverrides.router_ = function(args, originalMethod) {
assert.equal(originalMethod(), cls.uuid);
done();
};
streamRouter.extend(FakeClass, 'methodToExtend');
cls.methodToExtend();
});
it('should return what the router returns', function() {
var uniqueValue = 234;
streamRouterOverrides.router_ = function() {
return uniqueValue;
};
streamRouter.extend(FakeClass, 'methodToExtend');
assert.equal(FakeClass.prototype.methodToExtend(), uniqueValue);
});
});
describe('parseArguments_', function() {
it('should set defaults', function() {
var parsedArguments = streamRouter.parseArguments_([]);
assert.strictEqual(Object.keys(parsedArguments.query).length, 0);
assert.strictEqual(parsedArguments.callback, undefined);
assert.strictEqual(parsedArguments.maxResults, -1);
assert.strictEqual(parsedArguments.autoPaginate, true);
});
it('should detect a callback if first argument is a function', function() {
var args = [ util.noop ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.callback, args[0]);
});
it('should use any other first argument as query', function() {
var args = [ 'string' ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.query, args[0]);
});
it('should not make an undefined value the query', function() {
var args = [ undefined, util.noop ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.deepEqual(parsedArguments.query, {});
});
it('should detect a callback if last argument is a function', function() {
var args = [ 'string', util.noop ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.callback, args[1]);
});
it('should not assign a callback if a fn is not provided', function() {
var args = [ 'string' ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.callback, undefined);
});
it('should set maxResults from query.maxResults', function() {
var args = [ { maxResults: 10 } ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.maxResults, args[0].maxResults);
});
it('should set maxResults from query.limitVal', function() {
var args = [ { limitVal: 10 } ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.maxResults, args[0].limitVal);
});
it('should set maxResults from query.pageSize', function() {
var args = [ { pageSize: 10 } ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.maxResults, args[0].pageSize);
});
it('should set autoPaginate: false if there is a maxResults', function() {
var args = [ { maxResults: 10 }, util.noop ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.autoPaginate, false);
});
it('should set autoPaginate: false query.autoPaginate', function() {
var args = [ { autoPaginate: false }, util.noop ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.autoPaginate, false);
});
it('should set autoPaginate: false with query.autoPaginateVal', function() {
var args = [ { autoPaginateVal: false }, util.noop ];
var parsedArguments = streamRouter.parseArguments_(args);
assert.strictEqual(parsedArguments.autoPaginate, false);
});
});
describe('router_', function() {
beforeEach(function() {
streamRouterOverrides.runAsStream_ = util.noop;
});
describe('callback mode', function() {
describe('autoPaginate', function() {
it('should call runAsStream_ when autoPaginate:true', function(done) {
var parsedArguments = {
autoPaginate: true,
callback: util.noop
};
streamRouterOverrides.runAsStream_ = function(args, originalMethod) {
assert.strictEqual(args, parsedArguments);
originalMethod();
return through();
};
streamRouter.router_(parsedArguments, done);
});
it('should execute callback on error', function(done) {
var error = new Error('Error.');
var parsedArguments = {
autoPaginate: true,
callback: function(err) {
assert.strictEqual(err, error);
done();
}
};
streamRouterOverrides.runAsStream_ = function() {
var stream = through();
setImmediate(function() {
stream.emit('error', error);
});
return stream;
};
streamRouter.router_(parsedArguments, util.noop);
});
it('should return all results on end', function(done) {
var results = ['a', 'b', 'c'];
var parsedArguments = {
autoPaginate: true,
callback: function(err, results_) {
assert.deepEqual(results_.toString().split(''), results);
done();
}
};
streamRouterOverrides.runAsStream_ = function() {
var stream = through();
setImmediate(function() {
results.forEach(function(result) {
stream.push(result);
});
stream.push(null);
});
return stream;
};
streamRouter.router_(parsedArguments, util.noop);
});
});
describe('manual pagination', function() {
it('should recoginze autoPaginate: false', function(done) {
var parsedArguments = {
autoPaginate: false,
query: {
a: 'b',
c: 'd'
},
callback: done
};
streamRouter.router_(parsedArguments, function(query, callback) {
assert.deepEqual(query, parsedArguments.query);
callback();
});
});
});
});
describe('stream mode', function() {
it('should call runAsStream_', function(done) {
var parsedArguments = {
query: { a: 'b', c: 'd' }
};
streamRouterOverrides.runAsStream_ = function(args, originalMethod) {
assert.deepEqual(args, parsedArguments);
originalMethod();
};
streamRouter.router_(parsedArguments, done);
});
it('should return the value of runAsStream_', function() {
var parsedArguments = {
query: { a: 'b', c: 'd' }
};
var stream = through();
streamRouterOverrides.runAsStream_ = function() {
return stream;
};
var stream_ = streamRouter.router_(parsedArguments, assert.ifError);
assert.strictEqual(stream_, stream);
});
});
});
describe('runAsStream_', function() {
describe('stream mode', function() {
var PARSED_ARGUMENTS = {
query: {
a: 'b', c: 'd'
}
};
it('should call original method when stream opens', function(done) {
function originalMethod(query) {
assert.strictEqual(query, PARSED_ARGUMENTS.query);
done();
}
var rs = streamRouter.runAsStream_(PARSED_ARGUMENTS, originalMethod);
rs.on('data', util.noop); // Trigger the underlying `_read` event.
});
it('should emit an error if one occurs', function(done) {
var error = new Error('Error.');
function originalMethod(query, callback) {
setImmediate(function() {
callback(error);
});
}
var rs = streamRouter.runAsStream_(PARSED_ARGUMENTS, originalMethod);
rs.on('data', util.noop); // Trigger the underlying `_read` event.
rs.on('error', function(err) {
assert.deepEqual(err, error);
done();
});
});
it('should push results onto the stream', function(done) {
var results = ['a', 'b', 'c'];
var resultsReceived = [];
function originalMethod(query, callback) {
setImmediate(function() {
callback(null, results);
});
}
var rs = streamRouter.runAsStream_(PARSED_ARGUMENTS, originalMethod);
rs.on('data', function(result) {
resultsReceived.push(result);
});
rs.on('end', function() {
assert.deepEqual(resultsReceived, ['a', 'b', 'c']);
done();
});
});
describe('limits', function() {
var limit = 1;
function originalMethod(query, callback) {
setImmediate(function() {
callback(null, [1, 2, 3]);
});
}
it('should respect maxResults', function(done) {
var numResultsReceived = 0;
streamRouter.runAsStream_({ maxResults: limit }, originalMethod)
.on('data', function() { numResultsReceived++; })
.on('end', function() {
assert.strictEqual(numResultsReceived, limit);
done();
});
});
});
it('should get more results if nextQuery exists', function(done) {
var nextQuery = { a: 'b', c: 'd' };
var nextQuerySent = false;
function originalMethod(query, callback) {
if (nextQuerySent) {
assert.deepEqual(query, nextQuery);
done();
return;
}
setImmediate(function() {
nextQuerySent = true;
callback(null, [], nextQuery);
});
}
var rs = streamRouter.runAsStream_(PARSED_ARGUMENTS, originalMethod);
rs.on('data', util.noop); // Trigger the underlying `_read` event.
});
it('should not push more results if stream ends early', function(done) {
var results = ['a', 'b', 'c'];
function originalMethod(query, callback) {
setImmediate(function() {
callback(null, results);
});
}
var rs = streamRouter.runAsStream_(PARSED_ARGUMENTS, originalMethod);
rs.on('data', function(result) {
if (result === 'b') {
// Pre-maturely end the stream.
this.end();
}
assert.notEqual(result, 'c');
});
rs.on('end', function() {
done();
});
});
it('should not get more results if stream ends early', function(done) {
var results = ['a', 'b', 'c'];
var originalMethodCalledCount = 0;
function originalMethod(query, callback) {
originalMethodCalledCount++;
setImmediate(function() {
callback(null, results, {});
});
}
var rs = streamRouter.runAsStream_(PARSED_ARGUMENTS, originalMethod);
rs.on('data', function(result) {
if (result === 'b') {
// Pre-maturely end the stream.
this.end();
}
});
rs.on('end', function() {
assert.equal(originalMethodCalledCount, 1);
done();
});
});
});
});
});