Debian Codegraph @

closure-library/third_party/closure/goog/mochikit/async/deferredlist_test.js

raw
1 /**
2 * Copyright 2009 The Closure Library Authors. All Rights Reserved.
3 * Author: [email protected] (Shawn Brenneman)
4 */
5
6 goog.setTestOnly();
7 goog.require('goog.array');
8 goog.require('goog.async.Deferred');
9 goog.require('goog.async.DeferredList');
10 goog.require('goog.testing.jsunit');
11
12 var Deferred = goog.async.Deferred;
13 var DeferredList = goog.async.DeferredList;
14
15
16 // Re-throw (after a timeout) any errors not handled in an errback.
17 Deferred.STRICT_ERRORS = true;
18
19
20 /**
21 * A list of unhandled errors.
22 * @type {Array.<Error>}
23 */
24 var storedErrors = [];
25
26
27 /**
28 * Adds a catch-all error handler to deferred objects. Unhandled errors that
29 * reach the catch-all will be rethrown during tearDown.
30 *
31 * @param {...Deferred} var_args A list of deferred objects.
32 */
33 function addCatchAll(var_args) {
34 for (var i = 0, d; d = arguments[i]; i++) {
35 d.addErrback(function(res) {
36 storedErrors.push(res);
37 });
38 }
39 }
40
41
42 /**
43 * Checks storedErrors for unhandled errors. If found, the error is rethrown.
44 */
45 function checkCatchAll() {
46 var err = storedErrors.shift();
47 goog.array.clear(storedErrors);
48
49 if (err) {
50 throw err;
51 }
52 }
53
54
55 function tearDown() {
56 checkCatchAll();
57 }
58
59 /**
60 * @param {*} res
61 */
62 function neverHappen(res) {
63 fail('This should not happen');
64 }
65
66
67 function testNoInputs() {
68 var count = 0;
69 var d = new DeferredList([]);
70
71 d.addCallback(function(res) {
72 assertArrayEquals([], res);
73 count++;
74 });
75 addCatchAll(d);
76
77 assertEquals('An empty DeferredList should fire immediately with an empty ' +
78 'list of results.',
79 1, count);
80 }
81
82
83 function testNoInputsAndFireOnOneCallback() {
84 var count = 0;
85 var d = new DeferredList([], true);
86
87 d.addCallback(function(res) {
88 assertArrayEquals([], res);
89 count++;
90 });
91 addCatchAll(d);
92
93 assertEquals('An empty DeferredList with opt_fireOnOneCallback set should ' +
94 'not fire unless callback is invoked explicitly.',
95 0, count);
96
97 d.callback([]);
98 assertEquals('Calling callback explicitly should still fire.', 1, count);
99 }
100
101
102 function testDeferredList() {
103 var count = 0;
104
105 var a = new Deferred();
106 var b = new Deferred();
107 var c = new Deferred();
108
109 var dl = new DeferredList([a, b, c]);
110
111 dl.addCallback(function(res) {
112 assertEquals('Expected 3 Deferred results.', 3, res.length);
113
114 assertTrue('Deferred a should return success.', res[0][0]);
115 assertFalse('Deferred b should return failure.', res[1][0]);
116 assertTrue('Deferred c should return success.', res[2][0]);
117
118 assertEquals('Unexpected return value for a.', 'A', res[0][1]);
119 assertEquals('Unexpected return value for c.', 'C', res[2][1]);
120
121 assertEquals('B', res[1][1]);
122
123 count++;
124 });
125
126 addCatchAll(dl);
127
128 c.callback('C');
129 assertEquals(0, count);
130
131 b.errback('B');
132 assertEquals(0, count);
133
134 a.callback('A');
135
136 checkCatchAll();
137 assertEquals('DeferredList should fire on last call or errback.', 1, count);
138 }
139
140
141 function testFireOnFirstCallback() {
142 var a = new Deferred();
143 var b = new Deferred();
144 var c = new Deferred();
145
146 var dl = new DeferredList([a, b, c], true);
147
148 dl.addCallback(function(res) {
149 assertEquals('Should be the deferred index in this mode.', 1, res[0]);
150 assertEquals('B', res[1]);
151 });
152 dl.addErrback(neverHappen);
153
154 addCatchAll(dl);
155
156 a.errback('A');
157 b.callback('B');
158
159 // Shouldn't cause any more callbacks on the DeferredList.
160 c.callback('C');
161 }
162
163
164 function testFireOnFirstErrback() {
165 var a = new Deferred();
166 var b = new Deferred();
167 var c = new Deferred();
168
169 var dl = new DeferredList([a, b, c], false, true);
170
171 dl.addCallback(neverHappen);
172 dl.addErrback(function(res) {
173 assertEquals('A', res);
174
175 // Return a non-error value to break out of the errback path.
176 return null;
177 });
178 addCatchAll(dl);
179
180 b.callback('B');
181 a.errback('A');
182
183 assertTrue(c.hasFired());
184 c.addErrback(function(res) {
185 assertTrue(
186 'The DeferredList errback should have canceled all pending inputs.',
187 res instanceof Deferred.CanceledError);
188 return null;
189 });
190 addCatchAll(c);
191
192 // Shouldn't cause any more callbacks on the DeferredList.
193 c.callback('C');
194 }
195
196
197 function testNoConsumeErrors() {
198 var count = 0;
199
200 var a = new Deferred();
201 var dl = new DeferredList([a]);
202
203 a.addErrback(function(res) {
204 count++;
205 return null;
206 });
207
208 addCatchAll(a, dl);
209
210 a.errback('oh noes');
211 assertEquals(1, count);
212 }
213
214
215 function testConsumeErrors() {
216 var count = 0;
217
218 var a = new Deferred();
219 var dl = new DeferredList([a], false, false, true);
220
221 a.addErrback(neverHappen);
222
223 addCatchAll(a, dl);
224
225 a.errback('oh noes');
226 assertEquals(0, count);
227 }
228
229
230 function testNesting() {
231
232 function upperCase(res) {
233 return res.toUpperCase();
234 }
235
236 // Concatenates a list of callback or errback results into a single string.
237 function combine(res) {
238 return goog.array.map(res, function(result) {
239 return result[1];
240 }).join('');
241 }
242
243 var a = new Deferred();
244 var b = new Deferred();
245 var c = new Deferred();
246 var d = new Deferred();
247
248 a.addCallback(upperCase);
249 b.addCallback(upperCase);
250 c.addCallback(upperCase);
251 d.addCallback(upperCase);
252
253 var dl1 = new DeferredList([a, b]);
254 var dl2 = new DeferredList([c, d]);
255
256 dl1.addCallback(combine);
257 dl2.addCallback(combine);
258
259 var dl3 = new DeferredList([dl1, dl2]);
260 dl3.addCallback(combine);
261 dl3.addCallback(function(res) {
262 assertEquals('AbCd', res);
263 });
264
265 addCatchAll(dl1, dl2, dl3);
266
267 a.callback('a');
268 c.callback('c');
269 b.errback('b');
270 d.errback('d');
271 }
272
273
274 function testGatherResults() {
275 var a = new Deferred();
276 var b = new Deferred();
277 var c = new Deferred();
278
279 var dl = DeferredList.gatherResults([a, b, c]);
280
281 dl.addCallback(function(res) {
282 assertArrayEquals(['A', 'B', 'C'], res);
283 });
284
285 addCatchAll(dl);
286
287 b.callback('B');
288 a.callback('A');
289 c.callback('C');
290 }
291
292
293 function testGatherResultsFailure() {
294 var a = new Deferred();
295 var b = new Deferred();
296 var c = new Deferred();
297
298 var dl = DeferredList.gatherResults([a, b, c]);
299
300 var firedErrback = false;
301 var firedCallback = false;
302 dl.addCallback(function() {
303 firedCallback = true;
304 });
305 dl.addErrback(function() {
306 firedErrback = true;
307 return null;
308 });
309
310 addCatchAll(dl);
311
312 b.callback('B');
313 a.callback('A');
314 c.errback();
315
316 assertTrue('Errback should be called', firedErrback);
317 assertFalse('Callback should not be called', firedCallback);
318 }
319
320
321 function testGatherResults_cancelCancelsChildren() {
322 var canceled = [];
323 var a = new Deferred(function() {
324 canceled.push('a');
325 });
326 var b = new Deferred(function() {
327 canceled.push('b');
328 });
329 var c = new Deferred(function() {
330 canceled.push('c');
331 });
332
333 var dl = new DeferredList([a, b, c]);
334
335 var firedErrback = false;
336 var firedCallback = false;
337 dl.addCallback(function() {
338 firedCallback = true;
339 });
340 dl.addErrback(function() {
341 firedErrback = true;
342 return null;
343 });
344
345 addCatchAll(dl);
346
347 b.callback('b');
348 dl.cancel();
349
350 assertTrue('Errback should be called', firedErrback);
351 assertFalse('Callback should not be called', firedCallback);
352 assertArrayEquals(['a', 'c'], canceled);
353 }
354
355
356 function testErrorCancelsPendingChildrenWhenFireOnFirstError() {
357 var canceled = [];
358 var a = new Deferred(function() {
359 canceled.push('a');
360 });
361 var b = new Deferred(function() {
362 canceled.push('b');
363 });
364 var c = new Deferred(function() {
365 canceled.push('c');
366 });
367
368 var dl = new DeferredList([a, b, c], false, true);
369
370 var firedErrback = false;
371 var firedCallback = false;
372 dl.addCallback(function() {
373 firedCallback = true;
374 });
375 dl.addErrback(function() {
376 firedErrback = true;
377 return null;
378 });
379
380 addCatchAll(dl);
381
382 a.callback('a');
383 b.errback();
384
385 assertTrue('Errback should be called', firedErrback);
386 assertFalse('Callback should not be called', firedCallback);
387 assertArrayEquals('Only C should be canceled since A and B fired.',
388 ['c'], canceled);
389 }
390
391
392 function testErrorDoesNotCancelPendingChildrenForVanillaLists() {
393 var canceled = [];
394 var a = new Deferred(function() {
395 canceled.push('a');
396 });
397 var b = new Deferred(function() {
398 canceled.push('b');
399 });
400 var c = new Deferred(function() {
401 canceled.push('c');
402 });
403
404 var dl = new DeferredList([a, b, c]);
405
406 var firedErrback = false;
407 var firedCallback = false;
408 dl.addCallback(function() {
409 firedCallback = true;
410 });
411 dl.addErrback(function() {
412 firedErrback = true;
413 return null;
414 });
415
416 addCatchAll(dl);
417
418 a.callback('a');
419 b.errback();
420 c.callback('c');
421
422 assertFalse('Errback should not be called', firedErrback);
423 assertTrue('Callback should be called', firedCallback);
424 assertArrayEquals('No cancellations', [], canceled);
425 }
426
427
428 function testInputDeferredsStillUsable() {
429 var increment = function(res) {
430 return res + 1;
431 };
432 var incrementErrback = function(res) {
433 throw res + 1;
434 };
435
436 var aComplete = false;
437 var bComplete = false;
438 var hadListCallback = false;
439
440 var a = new Deferred().addCallback(increment);
441 var b = new Deferred().addErrback(incrementErrback);
442 var c = new Deferred();
443
444 var dl = new DeferredList([a, b, c]);
445
446 a.callback(0);
447 a.addCallback(increment);
448 a.addCallback(function(res) {
449 aComplete = true;
450 assertEquals(
451 'The "a" Deferred should have had two increment callbacks.',
452 2, res);
453 });
454 assertTrue('The "a" deferred should complete before the list.', aComplete);
455
456 b.errback(0);
457 b.addErrback(incrementErrback);
458 b.addErrback(function(res) {
459 bComplete = true;
460 assertEquals(
461 'The "b" Deferred should have had two increment errbacks.',
462 2, res);
463 });
464 assertTrue('The "b" deferred should complete before the list.', bComplete);
465
466 assertFalse('The list should not fire until every input has.', dl.hasFired());
467 c.callback();
468 assertTrue(dl.hasFired());
469
470 assertFalse(hadListCallback);
471 dl.addCallback(function(results) {
472 hadListCallback = true;
473
474 var aResult = results[0];
475 var bResult = results[1];
476 var cResult = results[2];
477
478 assertTrue(aResult[0]);
479 assertEquals(
480 'Should see the result from before the second callback was added.',
481 1, aResult[1]);
482
483 assertFalse(bResult[0]);
484 assertEquals(
485 'Should see the result from before the second errback was added.',
486 1, aResult[1]);
487
488 assertTrue(cResult[0]);
489 });
490 assertTrue(hadListCallback);
491
492 addCatchAll(dl);
493 }