forked from adobe/brackets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync-test.js
More file actions
504 lines (417 loc) · 19.8 KB
/
Async-test.js
File metadata and controls
504 lines (417 loc) · 19.8 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
497
498
499
500
501
502
503
504
/*
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, beforeEach, afterEach, it, runs, waits, waitsFor, waitsForDone, expect, $, jasmine */
define(function (require, exports, module) {
"use strict";
var Async = require("utils/Async"),
PromiseQueue = Async.PromiseQueue;
describe("Async", function () {
describe("Chain", function () {
function zeroArgThatSucceeds() {
var d = $.Deferred();
setTimeout(function () {
d.resolve();
}, 1);
return d.promise();
}
function zeroArgThatFails() {
var d = $.Deferred();
setTimeout(function () {
d.reject();
}, 1);
return d.promise();
}
function oneArgThatSucceeds(x) {
var d = $.Deferred();
setTimeout(function () {
d.resolveWith(null, [x]);
}, 1);
return d.promise();
}
function twoArgThatSucceeds(x, y) {
var d = $.Deferred();
setTimeout(function () {
d.resolveWith(null, [x, y]);
}, 1);
return d.promise();
}
function twoArgThatFails(x, y) {
var d = $.Deferred();
setTimeout(function () {
d.rejectWith(null, [x, y]);
}, 1);
return d.promise();
}
function syncAddTwoArg(x, y) {
return x + y;
}
function syncException() {
throw new Error("sync error");
}
function createArrayComparator(arrayOne) {
return function (arrayTwo) {
var i, l;
expect(arrayOne.length).toBe(arrayTwo.length);
l = Math.min(arrayOne.length, arrayTwo.length);
for (i = 0; i < l; i++) {
expect(arrayOne[i]).toBe(arrayTwo[i]);
}
};
}
function expectChainHelper(functions, args, shouldSucceed, responseComparator) {
var done = false;
var success = false;
var response = null;
runs(function () {
var result = Async.chain(
functions,
args
);
result.done(function () {
done = true;
success = true;
response = arguments;
});
result.fail(function () {
done = true;
success = false;
response = arguments;
});
});
waitsFor(function () {
return done;
}, "The chain should complete", 100);
runs(function () {
expect(success).toBe(shouldSucceed);
responseComparator(response);
});
}
describe("Zero-argument deferreds", function () {
it("[zero-arg] work with a null argument array", function () {
expectChainHelper(
[zeroArgThatSucceeds, zeroArgThatSucceeds, zeroArgThatSucceeds],
null,
true,
createArrayComparator([])
);
});
it("[zero-arg] call error callback when first deferred fails", function () {
expectChainHelper(
[zeroArgThatFails, zeroArgThatSucceeds, zeroArgThatSucceeds],
[],
false,
createArrayComparator([])
);
});
it("[zero-arg] call error callback when middle deferred fails", function () {
expectChainHelper(
[zeroArgThatSucceeds, zeroArgThatFails, zeroArgThatSucceeds],
[],
false,
createArrayComparator([])
);
});
it("[zero-arg] call error callback when last deferred fails", function () {
expectChainHelper(
[zeroArgThatSucceeds, zeroArgThatSucceeds, zeroArgThatFails],
[],
false,
createArrayComparator([])
);
});
it("[zero-arg] call success callback when all deferreds succeed", function () {
expectChainHelper(
[zeroArgThatSucceeds, zeroArgThatSucceeds, zeroArgThatSucceeds],
[],
true,
createArrayComparator([])
);
});
it("[zero-arg] call success callback immediately if there are no deferreds", function () {
expectChainHelper(
[],
[],
true,
createArrayComparator([])
);
});
});
describe("Nonzero-argument deferreds", function () {
it("[nonzero-arg] call error callback when first deferred fails", function () {
expectChainHelper(
[twoArgThatFails, twoArgThatSucceeds, twoArgThatSucceeds],
[1, 2],
false,
createArrayComparator([1, 2])
);
});
it("[nonzero-arg] call error callback when middle deferred fails", function () {
expectChainHelper(
[twoArgThatSucceeds, twoArgThatFails, twoArgThatSucceeds],
[1, 2],
false,
createArrayComparator([1, 2])
);
});
it("[nonzero-arg] call error callback when last deferred fails", function () {
expectChainHelper(
[twoArgThatSucceeds, twoArgThatSucceeds, twoArgThatFails],
[1, 2],
false,
createArrayComparator([1, 2])
);
});
it("[nonzero-arg] call success callback when all deferreds succeed", function () {
expectChainHelper(
[twoArgThatSucceeds, twoArgThatSucceeds, twoArgThatSucceeds],
[1, 2],
true,
createArrayComparator([1, 2])
);
});
it("[nonzero-arg] call success callback immediately if there are no deferreds", function () {
expectChainHelper(
[],
[1, 2],
true,
createArrayComparator([1, 2])
);
});
});
describe("Async/sync mix", function () {
it("[async/sync] succeed with sync command at beginning", function () {
expectChainHelper(
[syncAddTwoArg, oneArgThatSucceeds, oneArgThatSucceeds],
[1, 2],
true,
createArrayComparator([3])
);
});
it("[async/sync] succeed with sync command at middle", function () {
expectChainHelper(
[twoArgThatSucceeds, syncAddTwoArg, oneArgThatSucceeds],
[1, 2],
true,
createArrayComparator([3])
);
});
it("[async/sync] succeed with sync command at end", function () {
expectChainHelper(
[twoArgThatSucceeds, twoArgThatSucceeds, syncAddTwoArg],
[1, 2],
true,
createArrayComparator([3])
);
});
it("[async/sync] call error callback if sync command fails", function () {
expectChainHelper(
[twoArgThatSucceeds, syncException, twoArgThatSucceeds],
[1, 2],
false,
function (response) {
expect(response.length).toBe(1);
expect(response[0] instanceof Error).toBe(true);
}
);
});
it("[async/sync] two sync commands in order are executed completely synchronously", function () {
var promise = null,
flag = true;
runs(function () {
function negate(b) {
return !b;
}
function setFlag(b) {
flag = b;
return flag;
}
promise = Async.chain([negate, setFlag], [flag]);
promise.done(function (b) {
expect(b).toBe(flag);
});
// note, we WANT to test this synchronously. This is not a bug
// in the unit test. A series of synchronous functions should
// execute synchronously.
expect(flag).toBe(false);
});
runs(function () {
// With (the current version) of jQuery promises, resolution and
// resolution handlers will get called synchronously. However, if we
// move to a different promise implementation (e.g. Q) then resolution
// handlers will get called asynchronously. So, we check completion
// of the promise on a separate pass.
waitsForDone(promise, "The chain to complete");
});
});
});
});
describe("Async PromiseQueue", function () {
var queue, calledFns;
beforeEach(function () {
queue = new PromiseQueue();
calledFns = {};
});
function makeFn(id, resolveNow, rejectNow) {
var result = new $.Deferred();
return {
fn: function () {
calledFns[id] = true;
if (rejectNow) {
result.reject();
} else if (resolveNow) {
result.resolve();
}
return result.promise();
},
deferred: result
};
}
it("should immediately execute a function when the queue is empty", function () {
var fnInfo = makeFn("one");
queue.add(fnInfo.fn);
expect(calledFns.one).toBe(true);
fnInfo.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should wait to execute the second added function", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
expect(calledFns.two).toBeUndefined();
fnInfo1.deferred.resolve();
expect(calledFns.two).toBe(true);
fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should properly handle the case where the first function completes synchronously", function () {
var fnInfo1 = makeFn("one", true),
fnInfo2 = makeFn("two");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
expect(calledFns.two).toBe(true);
fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should sequentially execute a second and third function if they're both added while the first is executing", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two"),
fnInfo3 = makeFn("three");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
queue.add(fnInfo3.fn);
expect(calledFns.two).toBeUndefined();
expect(calledFns.three).toBeUndefined();
fnInfo1.deferred.resolve();
expect(calledFns.two).toBe(true);
expect(calledFns.three).toBeUndefined();
fnInfo2.deferred.resolve();
expect(calledFns.three).toBe(true);
fnInfo3.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should sequentially execute a second and third function if the third is added while the second is executing", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two"),
fnInfo3 = makeFn("three");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
expect(calledFns.two).toBeUndefined();
fnInfo1.deferred.resolve();
expect(calledFns.two).toBe(true);
queue.add(fnInfo3.fn);
expect(calledFns.three).toBeUndefined();
fnInfo2.deferred.resolve();
expect(calledFns.three).toBe(true);
fnInfo3.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should execute a second function when added to the empty queue after the first function has completed", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
fnInfo1.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
queue.add(fnInfo2.fn);
expect(calledFns.two).toBe(true);
fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should execute the second function if the first function is rejected", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
expect(calledFns.two).toBeUndefined();
fnInfo1.deferred.reject();
expect(calledFns.two).toBe(true);
fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should execute the third function after first and second functions are rejected", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two"),
fnInfo3 = makeFn("three");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
queue.add(fnInfo3.fn);
expect(calledFns.two).toBeUndefined();
expect(calledFns.three).toBeUndefined();
fnInfo1.deferred.reject();
expect(calledFns.two).toBe(true);
fnInfo2.deferred.reject();
expect(calledFns.three).toBe(true);
fnInfo3.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
it("should execute the second function after the already-rejected first function is added to the queue", function () {
var fnInfo1 = makeFn("one", false, true),
fnInfo2 = makeFn("two");
queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);
queue.add(fnInfo2.fn);
expect(calledFns.two).toBe(true);
fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
});
});
});