forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread-count.ts
81 lines (69 loc) · 2.12 KB
/
thread-count.ts
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
import { resolve } from 'node:path';
import { cpus } from 'node:os';
import { once } from 'node:events';
import Piscina from '..';
import { test } from 'tap';
test('will start with minThreads and max out at maxThreads', { only: true }, async ({ equal, rejects }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
minThreads: 2,
maxThreads: 4,
concurrentTasksPerWorker: 1
});
let counter = 0;
pool.on('workerCreate', () => {
counter++;
});
equal(pool.threads.length, 2);
rejects(pool.run('while(true) {}'));
rejects(pool.run('while(true) {}'));
// #3
rejects(pool.run('while(true) {}'));
await once(pool, 'workerCreate');
// #4
rejects(pool.run('while(true) {}'));
await once(pool, 'workerCreate');
// #4 - as spawn does not happen synchronously anymore, we wait for the signal once more
rejects(pool.run('while(true) {}'));
await once(pool, 'workerCreate');
equal(pool.threads.length, 4);
await pool.destroy();
equal(pool.threads.length, 0);
equal(counter, 4);
});
test('low maxThreads sets minThreads', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
maxThreads: 1
});
equal(pool.threads.length, 1);
equal(pool.options.minThreads, 1);
equal(pool.options.maxThreads, 1);
});
test('high minThreads sets maxThreads', {
skip: cpus().length > 8
}, async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
minThreads: 16
});
equal(pool.threads.length, 16);
equal(pool.options.minThreads, 16);
equal(pool.options.maxThreads, 16);
});
test('conflicting min/max threads is error', async ({ throws }) => {
throws(() => new Piscina({
minThreads: 16,
maxThreads: 8
}), /options.minThreads and options.maxThreads must not conflict/);
});
test('thread count should be 0 upon destruction', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
minThreads: 2,
maxThreads: 4
});
equal(pool.threads.length, 2);
await pool.destroy();
equal(pool.threads.length, 0);
});