forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidle-timeout.ts
44 lines (35 loc) · 1.12 KB
/
idle-timeout.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
import Piscina from '..';
import { test } from 'tap';
import { resolve } from 'path';
import { promisify } from 'util';
const delay = promisify(setTimeout);
test('idle timeout will let go of threads early', async ({ equal }) => {
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/wait-for-others.ts'),
idleTimeout: 500,
minThreads: 1,
maxThreads: 2
});
equal(pool.threads.length, 1);
const buffer = new Int32Array(new SharedArrayBuffer(4));
const firstTasks = [
pool.run([buffer, 2]),
pool.run([buffer, 2])
];
equal(pool.threads.length, 2);
const earlyThreadIds = await Promise.all(firstTasks);
equal(pool.threads.length, 2);
await delay(2000);
equal(pool.threads.length, 1);
const secondTasks = [
pool.run([buffer, 4]),
pool.run([buffer, 4])
];
equal(pool.threads.length, 2);
const lateThreadIds = await Promise.all(secondTasks);
// One thread should have been idle in between and exited, one should have
// been reused.
equal(earlyThreadIds.length, 2);
equal(lateThreadIds.length, 2);
equal(new Set([...earlyThreadIds, ...lateThreadIds]).size, 3);
});