source: trip-planner-front/node_modules/piscina/test/test-uncaught-exception-from-handler.ts@ ceaed42

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

initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import Piscina from '..';
2import { test } from 'tap';
3import { resolve } from 'path';
4import { once } from 'events';
5
6test('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
13test('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
24test('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
48test('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});
Note: See TracBrowser for help on using the repository browser.