thread-count.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import Piscina from '..';
  2. import { cpus } from 'os';
  3. import { test } from 'tap';
  4. import { resolve } from 'path';
  5. test('will start with minThreads and max out at maxThreads', async ({ equal, rejects }) => {
  6. const pool = new Piscina({
  7. filename: resolve(__dirname, 'fixtures/eval.js'),
  8. minThreads: 2,
  9. maxThreads: 4
  10. });
  11. equal(pool.threads.length, 2);
  12. rejects(pool.runTask('while(true) {}'));
  13. equal(pool.threads.length, 2);
  14. rejects(pool.runTask('while(true) {}'));
  15. equal(pool.threads.length, 2);
  16. rejects(pool.runTask('while(true) {}'));
  17. equal(pool.threads.length, 3);
  18. rejects(pool.runTask('while(true) {}'));
  19. equal(pool.threads.length, 4);
  20. rejects(pool.runTask('while(true) {}'));
  21. equal(pool.threads.length, 4);
  22. await pool.destroy();
  23. });
  24. test('low maxThreads sets minThreads', async ({ equal }) => {
  25. const pool = new Piscina({
  26. filename: resolve(__dirname, 'fixtures/eval.js'),
  27. maxThreads: 1
  28. });
  29. equal(pool.threads.length, 1);
  30. equal(pool.options.minThreads, 1);
  31. equal(pool.options.maxThreads, 1);
  32. });
  33. test('high minThreads sets maxThreads', {
  34. skip: cpus().length > 8
  35. }, async ({ equal }) => {
  36. const pool = new Piscina({
  37. filename: resolve(__dirname, 'fixtures/eval.js'),
  38. minThreads: 16
  39. });
  40. equal(pool.threads.length, 16);
  41. equal(pool.options.minThreads, 16);
  42. equal(pool.options.maxThreads, 16);
  43. });
  44. test('conflicting min/max threads is error', async ({ throws }) => {
  45. throws(() => new Piscina({
  46. minThreads: 16,
  47. maxThreads: 8
  48. }), /options.minThreads and options.maxThreads must not conflict/);
  49. });
  50. test('thread count should be 0 upon destruction', async ({ equal }) => {
  51. const pool = new Piscina({
  52. filename: resolve(__dirname, 'fixtures/eval.js'),
  53. minThreads: 2,
  54. maxThreads: 4
  55. });
  56. equal(pool.threads.length, 2);
  57. await pool.destroy();
  58. equal(pool.threads.length, 0);
  59. });