-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
76 lines (62 loc) · 1.35 KB
/
test.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
/* eslint-disable no-unused-vars */
"use strict";
const { Queue } = require('./index.js');
const assert = require('node:assert');
const { setTimeout } = require('node:timers/promises');
const queue = new Queue(async(task) => task(), { timeout: 2000 });
const tracker = new assert.CallTracker();
let i = 0;
const result = [];
// add task to the queue
queue.push(async() => {
await setTimeout(1000);
return 1;
});
// add task to the queue
queue.push(async() => {
await setTimeout(500);
return 2;
});
// add task to the queue
queue.push(
async() => {
await setTimeout(100);
return 3;
},
async() => {
await setTimeout(50);
return 4;
}
);
// add task to the queue
queue.push(async() => {
await setTimeout(2500);
return 5;
});
assert.strictEqual(queue.length(), 4);
assert.strictEqual(queue.running(), true);
queue.on('done', function(res, task) {
console.log('done', res);
result.push(res);
assert.strictEqual(res, ++i);
});
queue.on('timeout', tracker.calls(async function(task) {
console.log('timeout');
}, 1));
queue.on('drain', function() {
assert.strictEqual(queue.running(), false);
console.log('drain', result); // [ 1, 2, 3, 4 ]
assert.deepStrictEqual(result, [1, 2, 3, 4]);
});
process.on('exit', () => {
tracker.verify();
});
/* should be printed:
done 1
done 2
done 3
done 4
timeout
drain [ 1, 2, 3, 4 ]
done 5
*/