1 | import Piscina from '..';
|
---|
2 | import { test } from 'tap';
|
---|
3 | import { resolve } from 'path';
|
---|
4 | import { once } from 'events';
|
---|
5 |
|
---|
6 | test('uncaught exception resets Worker', async ({ rejects }) => {
|
---|
7 | const pool = new Piscina({
|
---|
8 | filename: resolve(__dirname, 'fixtures/eval.js')
|
---|
9 | });
|
---|
10 | await rejects(pool.runTask('throw new Error("not_caught")'), /not_caught/);
|
---|
11 | });
|
---|
12 |
|
---|
13 | test('uncaught exception in immediate resets Worker', async ({ rejects }) => {
|
---|
14 | const pool = new Piscina({
|
---|
15 | filename: resolve(__dirname, 'fixtures/eval.js')
|
---|
16 | });
|
---|
17 | await rejects(
|
---|
18 | pool.runTask(`
|
---|
19 | setImmediate(() => { throw new Error("not_caught") });
|
---|
20 | new Promise(() => {}) /* act as if we were doing some work */
|
---|
21 | `), /not_caught/);
|
---|
22 | });
|
---|
23 |
|
---|
24 | test('uncaught exception in immediate after task yields error event', async ({ equal }) => {
|
---|
25 | const pool = new Piscina({
|
---|
26 | filename: resolve(__dirname, 'fixtures/eval.js'),
|
---|
27 | maxThreads: 1,
|
---|
28 | useAtomics: false
|
---|
29 | });
|
---|
30 |
|
---|
31 | const errorEvent : Promise<Error[]> = once(pool, 'error');
|
---|
32 |
|
---|
33 | const taskResult = pool.runTask(`
|
---|
34 | setTimeout(() => { throw new Error("not_caught") }, 500);
|
---|
35 | 42
|
---|
36 | `);
|
---|
37 |
|
---|
38 | equal(await taskResult, 42);
|
---|
39 |
|
---|
40 | // Hack a bit to make sure we get the 'exit'/'error' events.
|
---|
41 | equal(pool.threads.length, 1);
|
---|
42 | pool.threads[0].ref();
|
---|
43 |
|
---|
44 | // This is the main aassertion here.
|
---|
45 | equal((await errorEvent)[0].message, 'not_caught');
|
---|
46 | });
|
---|
47 |
|
---|
48 | test('using parentPort is treated as an error', async ({ rejects }) => {
|
---|
49 | const pool = new Piscina({
|
---|
50 | filename: resolve(__dirname, 'fixtures/eval.js')
|
---|
51 | });
|
---|
52 | await rejects(
|
---|
53 | pool.runTask(`
|
---|
54 | require('worker_threads').parentPort.postMessage("some message");
|
---|
55 | new Promise(() => {}) /* act as if we were doing some work */
|
---|
56 | `), /Unexpected message on Worker: 'some message'/);
|
---|
57 | });
|
---|