Skip to content

Commit a638a1f

Browse files
blackrabbit99Sebmaster
authored andcommitted
Add tests for error handling
Tests are currently node-only since error handling is not implemented for node. It requires special handling due to it using child processes.
1 parent b60d633 commit a638a1f

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

test/specs/api.spec.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@
6060
});
6161
});
6262

63+
it('should .spawn() handle errors', function () {
64+
if (isNode) return;
65+
66+
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
67+
68+
var done = false;
69+
var error = null;
70+
71+
runs(function () {
72+
p.spawn(function (data) {
73+
throw ('Test error');
74+
return ['something', 'completly', 'else'];
75+
}).then(function () {
76+
77+
}, function (e) {
78+
error = e;
79+
done = true;
80+
});
81+
});
82+
83+
waitsFor(function () {
84+
return done;
85+
}, "it should finish", 500);
86+
87+
runs(function () {
88+
expect(typeof error).toEqual('object');
89+
expect(error.message).toMatch(/Test\serror/);
90+
});
91+
});
92+
6393
it('should .map() correctly', function () {
6494
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
6595

@@ -108,6 +138,72 @@
108138
});
109139
});
110140

141+
it('should map handle error correctly', function () {
142+
if(isNode) return;
143+
144+
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js', maxWorkers: 2 });
145+
146+
var done = false;
147+
var fail = false;
148+
var error = null;
149+
var result = null;
150+
151+
runs(function () {
152+
p.map(function (el) {
153+
if(el === 2) throw('Test error');
154+
return el + 1;
155+
}).then(function (data) {
156+
result = data;
157+
done = true;
158+
}, function(e){
159+
error = e;
160+
fail = true;
161+
});
162+
});
163+
164+
waitsFor(function () {
165+
return fail;
166+
}, "it should finish", 500);
167+
168+
runs(function () {
169+
expect(result).toEqual(null);
170+
expect(typeof error).toEqual('object');
171+
expect(error.message).toMatch(/Test\serror/);
172+
});
173+
});
174+
175+
it('should only fire promise once for errors + successful calls', function () {
176+
if (isNode) return;
177+
178+
var p = new Parallel([1, 2, 3], { evalPath: 'lib/eval.js' });
179+
180+
var done = false;
181+
var fires = 0;
182+
183+
runs(function () {
184+
p.map(function (el) {
185+
if (el === 1) throw new Error('a');
186+
return el;
187+
}).then(function (data) {
188+
fires++;
189+
}, function () {
190+
fires++;
191+
});
192+
});
193+
194+
setTimeout(function () {
195+
done = true;
196+
}, 1000);
197+
198+
waitsFor(function () {
199+
return done;
200+
}, "it should finish", 2000);
201+
202+
runs(function () {
203+
expect(fires).toEqual(1);
204+
});
205+
});
206+
111207
it('should chain .map() correctly', function () {
112208
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
113209

@@ -187,6 +283,39 @@
187283
});
188284
});
189285

286+
it('should reduce handle error correctly', function () {
287+
if(isNode) return;
288+
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js', maxWorkers: 2 });
289+
290+
var done = false;
291+
var fail = false;
292+
var error = null;
293+
var result = null;
294+
295+
runs(function () {
296+
p.reduce(function (n) {
297+
if(n[1] === 2) throw('Test error');
298+
return n[0] + n[1];
299+
}).then(function (data) {
300+
result = data;
301+
done = true;
302+
}, function(e){
303+
error = e;
304+
fail = true;
305+
});
306+
});
307+
308+
waitsFor(function () {
309+
return fail;
310+
}, "it should finish", 500);
311+
312+
runs(function () {
313+
expect(result).toEqual(null);
314+
expect(typeof error).toEqual('object');
315+
expect(error.message).toMatch(/Test\serror/);
316+
});
317+
});
318+
190319
it('should process data returned from .then()', function () {
191320
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
192321

@@ -217,6 +346,76 @@
217346
});
218347
});
219348

349+
it('should process data returned from .then() when errCb occurs', function () {
350+
if(isNode) return;
351+
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
352+
353+
var done = false;
354+
var result = null;
355+
356+
runs(function () {
357+
p.map(function (el) {
358+
if(el === 2) throw('Test error');
359+
return el + 1;
360+
}).then(function (data) {
361+
// some stuff
362+
}, function(e){
363+
return 5;
364+
}).then(function (data) {
365+
result = data;
366+
done = true;
367+
});
368+
});
369+
370+
waitsFor(function () {
371+
return done;
372+
}, "it should finish", 500);
373+
374+
runs(function () {
375+
expect(result).toEqual(5);
376+
});
377+
});
378+
379+
it('should process data returned from .then() when error occurs into then', function () {
380+
if(isNode) return;
381+
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });
382+
383+
var done = false;
384+
var result = null;
385+
var fail = false;
386+
var error = null;
387+
388+
runs(function () {
389+
p.map(function (el) {
390+
return el + 1;
391+
}).then(function (data) {
392+
throw('Test error');
393+
}, function(e){
394+
error = e;
395+
fail = true;
396+
return 5;
397+
}).then(function (data) {
398+
result = data;
399+
done = true;
400+
}, function(){
401+
//some stuff
402+
});
403+
});
404+
405+
waitsFor(function () {
406+
return done;
407+
}, "it should finish", 500);
408+
409+
waitsFor(function () {
410+
return fail;
411+
}, "it should finish", 500);
412+
413+
runs(function () {
414+
expect(result).toEqual(5);
415+
expect(error).toMatch(/Test\serror/);
416+
});
417+
});
418+
220419
if (!isNode) {
221420
it('should work with require()d scripts (web-exclusive)', function () {
222421
var p = new Parallel([1, 2, 3], { evalPath: isNode ? undefined : 'lib/eval.js' });

0 commit comments

Comments
 (0)