-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
84 lines (74 loc) · 1.66 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
77
78
79
80
81
82
83
84
const os = require('os');
const { test } = require('uvu');
const assert = require('uvu/assert');
const pool = require('./examples/fibonacci');
const cores = os.cpus().length;
const wait = ms => new Promise(res => setTimeout(res, ms));
test('pool.all', async () => {
const results = await Promise.all([
pool.run(10),
pool.run(20),
pool.run(30),
]);
assert.equal(results, [ 55, 6765, 832040 ]);
});
test('pool.all - error', async () => {
try {
await Promise.all([
pool.run(10),
pool.run(20),
pool.run('fail'),
]);
assert.unreachable('should have thrown');
} catch (e) {
assert.instance(e, Error);
assert.equal(e.message, 'fibonacci failed');
}
});
test('pool.run - queue', async () => {
const promises = [];
const runs = cores + 1;
for (let i = 0; i++ < runs;) {
promises.push(pool.run(i));
}
const results = await Promise.all(promises);
assert.equal(results, [
1,
1,
2,
3,
5,
8,
13,
21,
34,
].slice(0, runs));
});
test('pool.run - allows more than one pool per file', async () => {
const { pool1, pool2, pool3 } = require('./examples/multiple');
const results = await Promise.all([
pool1.run(10),
pool2.run(10),
pool3.run(10)
]);
assert.equal(results, [ 55, 54, 53 ]);
});
test('pool.queue - allows queue management', async () => {
const results = [];
pool.queue(async () => {
await wait(1);
results[0] = await pool.run(10);
});
pool.queue(async () => {
await wait(1);
results[1] = await pool.run(20);
});
pool.queue(async () => {
await wait(1);
results[2] = await pool.run(30);
});
await pool.wait();
assert.equal(results, [ 55, 6765, 832040 ]);
});
// TODO: add a test for ts-node-dev
test.run();