forked from leetcode-tools/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_queue.js
58 lines (46 loc) · 1.11 KB
/
test_queue.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
'use strict';
const assert = require('chai').assert;
const rewire = require('rewire');
describe('queue', function() {
let Queue;
beforeEach(function() {
Queue = rewire('../lib/queue');
});
it('should ok', function(done) {
function doTask(x, q, cb) {
++q.ctx.n;
q.ctx.sum += x;
return cb();
}
const ctx = {n: 0, sum: 0};
const q = new Queue([], ctx, doTask);
q.addTask(1);
q.addTask(2);
q.addTasks([3, 4, 5]);
q.run(5, function(e, ctx) {
assert.notExists(e);
assert.equal(ctx.n, 5);
assert.equal(ctx.sum, 15);
done();
});
});
it('should ok in sequence', function(done) {
const config = {network: {}};
Queue.__set__('config', config);
function doTask(x, q, cb) {
if (!q.ctx.list) q.ctx.list = [];
q.ctx.list.push(x);
return cb();
}
const q = new Queue(null, null, doTask);
q.addTask(1);
q.addTasks([2, 3]);
q.addTasks([4]);
q.addTask(5);
q.run(null, function(e, ctx) {
assert.notExists(e);
assert.deepEqual(ctx.list, [1, 2, 3, 4, 5]);
done();
});
});
});