forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkers.ts
56 lines (48 loc) · 1.65 KB
/
workers.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
import { resolve } from 'node:path';
import { test } from 'tap';
import Piscina from '../dist';
test('workers are marked as destroyed if destroyed', async t => {
let index = 0;
// Its expected to have one task get balanced twice due to the load balancer distribution
// first task enters, its distributed; second is enqueued, once first is done, second is distributed and normalizes
t.plan(4);
let workersFirstRound = [];
let workersSecondRound = [];
const pool = new Piscina({
filename: resolve(__dirname, 'fixtures/eval.js'),
minThreads: 2,
maxThreads: 2,
concurrentTasksPerWorker: 1,
loadBalancer (_task, workers) {
if (workersFirstRound.length === 0) {
workersFirstRound = workers;
workersSecondRound = workers;
} else if (
workersFirstRound[0].id !== workers[0].id
) {
workersSecondRound = workers;
}
// Verify distribution to properly test this feature
const candidate = workers[index++ % workers.length];
if (candidate.currentUsage !== 0 && !candidate.isRunningAbortableTask) {
return null;
}
return candidate;
}
});
const tasks = [];
const controller = new AbortController();
const signal = controller.signal;
tasks.push(pool.run('while (true) {}', {
signal
}));
for (let n = 0; n < 5; n++) {
tasks.push(pool.run('new Promise(resolve => setTimeout(resolve, 500))'));
}
controller.abort();
await Promise.allSettled(tasks);
t.strictNotSame(workersFirstRound, workersSecondRound);
t.equal(workersFirstRound.length, 2);
t.ok(workersFirstRound[0].destroyed);
t.notOk(workersFirstRound[0].terminating);
});