[6a3a178] | 1 | import Piscina from '..';
|
---|
| 2 | import { test } from 'tap';
|
---|
| 3 | import { resolve } from 'path';
|
---|
| 4 | import { promisify } from 'util';
|
---|
| 5 |
|
---|
| 6 | const delay = promisify(setTimeout);
|
---|
| 7 |
|
---|
| 8 | test('idle timeout will let go of threads early', async ({ equal }) => {
|
---|
| 9 | const pool = new Piscina({
|
---|
| 10 | filename: resolve(__dirname, 'fixtures/wait-for-others.ts'),
|
---|
| 11 | idleTimeout: 500,
|
---|
| 12 | minThreads: 1,
|
---|
| 13 | maxThreads: 2
|
---|
| 14 | });
|
---|
| 15 |
|
---|
| 16 | equal(pool.threads.length, 1);
|
---|
| 17 | const buffer = new Int32Array(new SharedArrayBuffer(4));
|
---|
| 18 |
|
---|
| 19 | const firstTasks = [
|
---|
| 20 | pool.runTask([buffer, 2]),
|
---|
| 21 | pool.runTask([buffer, 2])
|
---|
| 22 | ];
|
---|
| 23 | equal(pool.threads.length, 2);
|
---|
| 24 |
|
---|
| 25 | const earlyThreadIds = await Promise.all(firstTasks);
|
---|
| 26 | equal(pool.threads.length, 2);
|
---|
| 27 |
|
---|
| 28 | await delay(2000);
|
---|
| 29 | equal(pool.threads.length, 1);
|
---|
| 30 |
|
---|
| 31 | const secondTasks = [
|
---|
| 32 | pool.runTask([buffer, 4]),
|
---|
| 33 | pool.runTask([buffer, 4])
|
---|
| 34 | ];
|
---|
| 35 | equal(pool.threads.length, 2);
|
---|
| 36 |
|
---|
| 37 | const lateThreadIds = await Promise.all(secondTasks);
|
---|
| 38 |
|
---|
| 39 | // One thread should have been idle in between and exited, one should have
|
---|
| 40 | // been reused.
|
---|
| 41 | equal(earlyThreadIds.length, 2);
|
---|
| 42 | equal(lateThreadIds.length, 2);
|
---|
| 43 | equal(new Set([...earlyThreadIds, ...lateThreadIds]).size, 3);
|
---|
| 44 | });
|
---|