test-uncaught-exception-from-handler.ts 1.7 KB

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