-
Notifications
You must be signed in to change notification settings - Fork 465
/
async_progress_queue_worker.js
69 lines (63 loc) · 1.68 KB
/
async_progress_queue_worker.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
'use strict';
const common = require('./common');
const assert = require('assert');
module.exports = common.runTest(test);
async function test ({ asyncprogressqueueworker }) {
await success(asyncprogressqueueworker);
await fail(asyncprogressqueueworker);
await signalTest(asyncprogressqueueworker);
}
function success (binding) {
return new Promise((resolve, reject) => {
const expected = [0, 1, 2, 3];
const actual = [];
const worker = binding.createWork(expected.length,
common.mustCall((err) => {
if (err) {
reject(err);
} else {
// All queued items shall be invoked before complete callback.
assert.deepEqual(actual, expected);
resolve();
}
}),
common.mustCall((_progress) => {
actual.push(_progress);
}, expected.length)
);
binding.queueWork(worker);
});
}
function fail (binding) {
return new Promise((resolve, reject) => {
const worker = binding.createWork(-1,
common.mustCall((err) => {
assert.throws(() => { throw err; }, /test error/);
resolve();
}),
common.mustNotCall()
);
binding.queueWork(worker);
});
}
function signalTest (binding) {
return new Promise((resolve, reject) => {
const expectedCalls = 4;
let actualCalls = 0;
const worker = binding.createSignalWork(expectedCalls,
common.mustCall((err) => {
if (err) {
reject(err);
} else {
if (actualCalls === expectedCalls) {
resolve();
}
}
}),
common.mustCall((_progress) => {
actualCalls++;
}, expectedCalls)
);
binding.queueSignalWork(worker);
});
}