forked from spion/genny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
452 lines (392 loc) · 11.9 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
/*jshint esnext: true */
var t = require('tap');
var Q = require('q');
var genny = require('../');
genny.longStackSupport = true;
function errors(cb) {
setImmediate(function() {
cb(new Error('oops'))
});
}
function nowait(k, cb) {
cb(null, k);
}
function evil(cb) {
setTimeout(function() {
cb(null, 'EVIL')
}, 10);
setImmediate(function() {
cb(null, 'EVIL')
});
}
function normal(cb,r,t) {
setTimeout(function() {
cb(null, r||"OK")
}, t||30);
}
function multiresult(cb) {
setImmediate(function() {
cb(null, 'r1', 'r2')
});
}
var throws = genny.fn(function* $throws(t, message, resume) {
if (t) {
var ok = yield normal(resume());
t.equals(ok, "OK", "yields before a throw");
}
throw message;
});
var return5 = genny.fn(function* $return5(resume) {
return 5;
});
t.test(
"simple test",
genny.fn(function* (t, resume) {
yield setImmediate(resume());
t.ok(true, 'resume success');
t.end();
}));
t.test(
"correct parallel order even when callback immediately called in the same tick",
genny.fn(function* (t, resume) {
normal(resume(),"1 OK");
nowait("2 OK", resume());
normal(resume(), "3 OK", 0);
t.equals(yield, "1 OK")
t.equals(yield, "2 OK")
t.equals(yield, "3 OK");
t.end();
}));
t.test(
"correct parallel queueing",
genny.fn(function* (t, resume) {
var times = [40,30,10,50,33,5];
for (var i in times)
normal(resume(), times[i] + " OK", times[i]);
for (var i in times)
t.equals(times[i] + " OK", yield);
t.end();
}));
t.test(
"correct parallel order when things throw",
genny.fn(function* (t, resume) {
normal(resume(), "1 OK");
nowait("2 OK", resume());
throws(t, "3 NOK", resume());
normal(resume(),"4 OK");
t.equals(yield, "1 OK")
t.equals(yield, "2 OK")
try {
yield;
t.equals(0,1);
} catch (e) {
t.equals(e, "3 NOK", "catches things thrown");
}
t.equals(yield, "4 OK")
t.end();
}));
t.test(
"correct parallel order (yield [])",
genny.fn(function* (t, resume) {
var ok = yield [normal, setImmediate];
t.equals(ok[0], 'OK')
t.equals(ok[1], undefined);
t.end();
}));
t.test(
"throws error",
genny.fn(function* (t, resume) {
try {
yield errors(resume());
} catch (e) {
t.ok(e, "error was thrown");
t.end();
}
}));
t.test(
"calls callback if present instead of throwing + genny.run",
function(t) {
genny.run(function* (resume) {
yield errors(resume());
}, function(err, res) {
t.ok(err, "error present");
t.end();
});
});
t.test(
"calls callback with return result on exit",
function(t) {
genny.run(function* (resume) {
yield setImmediate(resume());
return 1;
}, function(err, res) {
t.equals(res, 1, "result present");
t.end();
});
});
t.test(
"calls callback with return result on exit when return is after a yield",
function(t) {
genny.run(function* (resume) {
var x = yield normal(resume());
return x == "OK";
}, function(err, res) {
t.ok(res, "result is true");
t.end();
});
});
t.test(
"handles functions that immediately call the callback in the same tick",
genny.fn(function* (t, resume) {
var arr = [];
for (var k = 0; k < 10; ++k)
arr.push(yield nowait(k, resume()));
t.deepEquals(arr, [0,1,2,3,4,5,6,7,8,9], 'resumed all immediate calls');
t.end();
}));
t.test(
"handles yields in/after a catch",
genny.fn(function* $test(t, resume) {
var types = [t, null];
for (var i=0; i<types.length; i++)
try {
yield throws(types[i], "threw", resume());
} catch (e) {
t.equals(e, "threw", "catches things thrown");
var res = yield return5(resume());
t.equals(res, 5, "handles yields after throws");
}
t.end();
}));
t.test(
"handles yields in/after nested catches catch",
genny.fn(function* (t, resume) {
var types = [t, null];
for (var i=0; i<types.length; i++)
for (var j=0; j<types.length; j++)
try {
yield throws(types[i], "threw", resume());
} catch (e) {
t.equals(e, "threw", "catches things thrown");
try {
yield throws(types[j], "threw again", resume());
} catch (e) {
t.equals(e, "threw again", "catches things thrown again");
var res = yield return5(resume());
t.equals(res, 5, "handles yields after throws");
}
}
t.end();
}));
t.test(
"handles returns after a catch",
genny.fn(function* (t, resume) {
var done = genny.fn(function *(resume) {
try {
yield throws(t, "threw", resume());
} catch (e) {
t.equals(e, "threw", "catches things thrown");
return "done";
}
});
var z = yield done(resume());
t.equals(z, "done", "handles return immediately after a catch");
t.end();
}));
t.test(
"functions not passed a callback can catch thrown errors",
genny.fn(function* (t, resume) {
genny.run(function* (resume) {
try {
yield throws(t, "threw", resume());
} catch (e) {
t.equals(e, "threw", "caught a thrown error");
t.end();
}
});
}));
if (0) // can't legitimatly expect the thown error to be caught as the generator might be legitimately not running or even done by the time this gets thrown, so only a global catch will catch it.
t.test(
"handles evil functions that run callbacks multiple times",
genny.fn(function* (t, resume) {
try {
yield evil(resume());
var res = yield normal(resume());
} catch (e) {
t.ok(e, "evil functions cause throw");
t.end();
}
}));
t.test(
"has a complete error stack",
genny.fn(function* completeStackTrace(t, resume) {
try {
yield errors(resume());
} catch (e) {
t.ok(~e.stack.indexOf('completeStackTrace'),
"error stack is complete");
t.end();
}
}));
t.test(
"has a complete error stack even when invoking generators",
genny.fn(function* completeStackTrace2(t, resume) {
function* innerGenerator1(resume) {
yield errors(resume());
}
function* innerGenerator2(resume) {
yield* innerGenerator1(resume.gen());
}
function* innerGenerator3(resume) {
yield* innerGenerator2(resume.gen());
}
try {
yield* innerGenerator3(resume.gen());
} catch (e) {
//console.log(e.stack);
t.ok(~e.stack.indexOf('innerGenerator1'),
"stack contains inner generator 1");
t.ok(~e.stack.indexOf('innerGenerator3'),
"stack contains inner generator 3");
t.end();
}
}));
t.test(
"has a complete error when invoking generators w/o yield*",
genny.fn(function* completeStackTraceUnstarred(t, resume) {
function* innerGenerator1(resume) {
yield errors(resume());
}
function* innerGenerator2(resume) {
yield genny.fn(innerGenerator1)(resume());
}
function* innerGenerator3(resume) {
yield genny.fn(innerGenerator2)(resume());
}
try {
yield genny.fn(innerGenerator3)(resume());
} catch (e) {
//console.log(e.stack);
t.ok(~e.stack.indexOf('innerGenerator1'),
"stack contains inner generator 1");
t.ok(~e.stack.indexOf('innerGenerator3'),
"stack contains inner generator 3");
t.end();
}
}));
t.test(
"has a complete error stack for thrown exceptions",
genny.fn(function* completeStackTrace2(t, resume) {
function* innerGenerator1(resume) {
throw new Error("Whoopsy");
}
function* innerGenerator2(resume) {
yield* innerGenerator1(resume.gen());
}
function* innerGenerator3(resume) {
yield* innerGenerator2(resume.gen());
}
try {
yield* innerGenerator3(resume.gen());
} catch (e) {
//console.log(e.stack);
t.ok(~e.stack.indexOf('innerGenerator1'),
"stack contains inner generator 1");
t.ok(~e.stack.indexOf('innerGenerator3'),
"stack contains inner generator 3");
t.end();
}
}));
t.test(
"resume.nothrow yields arrays",
genny.fn(function* (t, resume) {
var res = yield multiresult(resume.nothrow());
t.equals(res[0], null, 'first argument is error');
t.equals(res[2], 'r2', 'third argument is r2');
t.end();
}));
t.test(
"listener doesn't send results to callback",
function(t) {
genny.listener(function* (callback, resume) {
setTimeout(callback, 1);
return "resultFromGenerator";
})(function(err, res) {
t.equals(res, undefined, 'listener has no result in callback');
t.end();
})
});
t.test(
"listener doesn't send errors to callback",
function(t) {
genny.listener(function* (callback, resume) {
setTimeout(callback, 1);
throw new Error("ErrorFromGenerator");
})(function(err) {
t.notOk(err, 'listener has no error in callback');
t.end();
})
});
t.test(
"middleware doesn't send results to callback",
function(t) {
genny.middleware(function* (req, res, next) {
return true;
})(t, t, function(err, res) {
t.equals(res, undefined, 'listener has no result in callback');
t.end();
})
});
t.test(
"middleware does send errors to callback",
function(t) {
genny.middleware(function* (req, res, next) {
throw new Error("Oops");
})(t, t, function(err) {
t.ok(err, 'middleware has error in callback');
t.end();
})
});
/****************************************************
* Promises and thunks
****************************************************/
var promiseTest = function promiseTest(err) {
var d = Q.defer();
setTimeout(function() {
if (!err) d.resolve('as-promised');
else d.reject(new Error("Fail"));
}, 1);
return d.promise;
};
t.test(
"accepts promises",
genny.fn(function* (t, resume) {
var res = yield promiseTest();
t.equals(res, 'as-promised', 'promise was handled');
try {
var err = yield promiseTest(true);
} catch (e) {
t.ok(e, 'Had error');
t.end();
}
}));
t.test(
"accepts thunks",
genny.fn(function* (t) {
var res = yield normal;
t.equals(res, 'OK', 'thunk was handled');
try {
var err = yield errors;
} catch (e) {
t.ok(e, 'Had error');
t.end();
}
}));
t.test(
"accepts arrays",
genny.fn(function* (t) {
var multi = yield [normal, promiseTest()];
t.equals(multi[0], 'OK');
t.equals(multi[1], 'as-promised');
t.end();
}));