async-context.ts 945 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { createHook, executionAsyncId } from 'async_hooks';
  2. import Piscina from '..';
  3. import { test } from 'tap';
  4. import { resolve } from 'path';
  5. test('postTask() calls the correct async hooks', async ({ equal }) => {
  6. let taskId;
  7. let initCalls = 0;
  8. let beforeCalls = 0;
  9. let afterCalls = 0;
  10. let resolveCalls = 0;
  11. const hook = createHook({
  12. init (id, type) {
  13. if (type === 'Piscina.Task') {
  14. initCalls++;
  15. taskId = id;
  16. }
  17. },
  18. before (id) {
  19. if (id === taskId) beforeCalls++;
  20. },
  21. after (id) {
  22. if (id === taskId) afterCalls++;
  23. },
  24. promiseResolve () {
  25. if (executionAsyncId() === taskId) resolveCalls++;
  26. }
  27. });
  28. hook.enable();
  29. const pool = new Piscina({
  30. filename: resolve(__dirname, 'fixtures/eval.js')
  31. });
  32. await pool.runTask('42');
  33. hook.disable();
  34. equal(initCalls, 1);
  35. equal(beforeCalls, 1);
  36. equal(afterCalls, 1);
  37. equal(resolveCalls, 1);
  38. });