source: trip-planner-front/node_modules/piscina/test/idle-timeout.ts

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.1 KB
Line 
1import Piscina from '..';
2import { test } from 'tap';
3import { resolve } from 'path';
4import { promisify } from 'util';
5
6const delay = promisify(setTimeout);
7
8test('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});
Note: See TracBrowser for help on using the repository browser.