test-resourcelimits.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import Piscina from '..';
  2. import { test } from 'tap';
  3. import { resolve } from 'path';
  4. test('resourceLimits causes task to reject', async ({ equal, rejects }) => {
  5. const worker = new Piscina({
  6. filename: resolve(__dirname, 'fixtures/resource-limits.js'),
  7. resourceLimits: {
  8. maxOldGenerationSizeMb: 16,
  9. maxYoungGenerationSizeMb: 4,
  10. codeRangeSizeMb: 16
  11. }
  12. });
  13. worker.on('error', () => {
  14. // Ignore any additional errors that may occur.
  15. // This may happen because when the Worker is
  16. // killed a new worker is created that may hit
  17. // the memory limits immediately. When that
  18. // happens, there is no associated Promise to
  19. // reject so we emit an error event instead.
  20. // We don't care so much about that here. We
  21. // could potentially avoid the issue by setting
  22. // higher limits above but rather than try to
  23. // guess at limits that may work consistently,
  24. // let's just ignore the additional error for
  25. // now.
  26. });
  27. const limits : any = worker.options.resourceLimits;
  28. equal(limits.maxOldGenerationSizeMb, 16);
  29. equal(limits.maxYoungGenerationSizeMb, 4);
  30. equal(limits.codeRangeSizeMb, 16);
  31. rejects(worker.runTask(null),
  32. /Worker terminated due to reaching memory limit: JS heap out of memory/);
  33. });