fixed-queue.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { test } from 'tap';
  2. import { kQueueOptions } from '../dist/symbols';
  3. import { Piscina, FixedQueue, PiscinaTask as Task } from '..';
  4. import { resolve } from 'node:path';
  5. // @ts-expect-error - it misses several properties, but it's enough for the test
  6. class QueueTask implements Task {
  7. get [kQueueOptions] () {
  8. return null;
  9. }
  10. }
  11. test('queue length', async ({ equal }) => {
  12. const queue = new FixedQueue();
  13. equal(queue.size, 0);
  14. queue.push(new QueueTask());
  15. equal(queue.size, 1);
  16. queue.shift();
  17. equal(queue.size, 0);
  18. });
  19. test('queue length should not become negative', async ({ equal }) => {
  20. const queue = new FixedQueue();
  21. equal(queue.size, 0);
  22. queue.shift();
  23. equal(queue.size, 0);
  24. });
  25. test('queue remove', async ({ equal }) => {
  26. const queue = new FixedQueue();
  27. const task = new QueueTask();
  28. equal(queue.size, 0, 'should be empty on start');
  29. queue.push(task);
  30. equal(queue.size, 1, 'should contain single task after push');
  31. queue.remove(task);
  32. equal(queue.size, 0, 'should be empty after task removal');
  33. });
  34. test('remove not queued task should not lead to errors', async ({ equal }) => {
  35. const queue = new FixedQueue();
  36. const task = new QueueTask();
  37. equal(queue.size, 0, 'should be empty on start');
  38. queue.remove(task);
  39. equal(queue.size, 0, 'should be empty after task removal');
  40. });
  41. test('removing elements from intermediate CircularBuffer should not lead to issues', async ({ equal, same }) => {
  42. /*
  43. The test intends to check following scenario:
  44. 1) We fill the queue with 3 full circular buffers amount of items.
  45. 2) Empty the middle circular buffer with remove().
  46. 3) This should lead to the removal of the middle buffer from the queue:
  47. - Before emptying: tail buffer -> middle buffer -> head buffer.
  48. - After emptying: tail buffer -> head buffer.
  49. */
  50. const queue = new FixedQueue();
  51. // size of single circular buffer
  52. const batchSize = 2047;
  53. const firstBatch = Array.from({ length: batchSize }, () => new QueueTask());
  54. const secondBatch = Array.from({ length: batchSize }, () => new QueueTask());
  55. const thirdBatch = Array.from({ length: batchSize }, () => new QueueTask());
  56. const tasks = firstBatch.concat(secondBatch, thirdBatch);
  57. for (const task of tasks) {
  58. queue.push(task);
  59. }
  60. equal(queue.size, tasks.length, `should contain ${batchSize} * 3 items`);
  61. let size = queue.size;
  62. for (const task of secondBatch) {
  63. queue.remove(task);
  64. equal(queue.size, --size, `should contain ${size} items`);
  65. }
  66. const expected = firstBatch.concat(thirdBatch);
  67. const actual = [];
  68. while (!queue.isEmpty()) {
  69. const task = queue.shift();
  70. actual.push(task);
  71. }
  72. same(actual, expected);
  73. });
  74. test('removing elements from first CircularBuffer should not lead to issues', async ({ equal, same }) => {
  75. /*
  76. The test intends to check following scenario:
  77. 1) We fill the queue with 3 full circular buffers amount of items.
  78. 2) Empty the first circular buffer with remove().
  79. 3) This should lead to the removal of the tail buffer from the queue:
  80. - Before emptying: tail buffer -> middle buffer -> head buffer.
  81. - After emptying: tail buffer (previously middle) -> head buffer.
  82. */
  83. const queue = new FixedQueue();
  84. // size of single circular buffer
  85. const batchSize = 2047;
  86. const firstBatch = Array.from({ length: batchSize }, () => new QueueTask());
  87. const secondBatch = Array.from({ length: batchSize }, () => new QueueTask());
  88. const thirdBatch = Array.from({ length: batchSize }, () => new QueueTask());
  89. const tasks = firstBatch.concat(secondBatch, thirdBatch);
  90. for (const task of tasks) {
  91. queue.push(task);
  92. }
  93. equal(queue.size, tasks.length, `should contain ${batchSize} * 3 items`);
  94. let size = queue.size;
  95. for (const task of firstBatch) {
  96. queue.remove(task);
  97. equal(queue.size, --size, `should contain ${size} items`);
  98. }
  99. const expected = secondBatch.concat(thirdBatch);
  100. const actual = [];
  101. while (!queue.isEmpty()) {
  102. const task = queue.shift();
  103. actual.push(task);
  104. }
  105. same(actual, expected);
  106. });
  107. test('removing elements from last CircularBuffer should not lead to issues', async ({ equal, same }) => {
  108. /*
  109. The test intends to check following scenario:
  110. 1) We fill the queue with 3 full circular buffers amount of items.
  111. 2) Empty the last circular buffer with remove().
  112. 3) This should lead to the removal of the head buffer from the queue:
  113. - Before emptying: tail buffer -> middle buffer -> head buffer.
  114. - After emptying: tail buffer -> head buffer (previously middle).
  115. */
  116. const queue = new FixedQueue();
  117. // size of single circular buffer
  118. const batchSize = 2047;
  119. const firstBatch = Array.from({ length: batchSize }, () => new QueueTask());
  120. const secondBatch = Array.from({ length: batchSize }, () => new QueueTask());
  121. const thirdBatch = Array.from({ length: batchSize }, () => new QueueTask());
  122. const tasks = firstBatch.concat(secondBatch, thirdBatch);
  123. for (const task of tasks) {
  124. queue.push(task);
  125. }
  126. equal(queue.size, tasks.length, `should contain ${batchSize} * 3 items`);
  127. let size = queue.size;
  128. for (const task of thirdBatch) {
  129. queue.remove(task);
  130. equal(queue.size, --size, `should contain ${size} items`);
  131. }
  132. const expected = firstBatch.concat(secondBatch);
  133. const actual = [];
  134. while (!queue.isEmpty()) {
  135. const task = queue.shift();
  136. actual.push(task);
  137. }
  138. same(actual, expected);
  139. });
  140. test('simple integraion with Piscina', async ({ equal }) => {
  141. const queue = new FixedQueue();
  142. const pool = new Piscina({
  143. filename: resolve(__dirname, 'fixtures/simple-isworkerthread-named-import.ts'),
  144. taskQueue: queue
  145. });
  146. const result = await pool.runTask(null);
  147. equal(result, 'done');
  148. });
  149. test('concurrent calls with Piscina', async ({ same }) => {
  150. const queue = new FixedQueue();
  151. const pool = new Piscina({
  152. filename: resolve(__dirname, 'fixtures/eval-async.js'),
  153. taskQueue: queue
  154. });
  155. const tasks = ['1+1', '2+2', '3+3'];
  156. const results = await Promise.all(tasks.map((task) => pool.runTask(task)));
  157. // eslint-disable-next-line
  158. const expected = tasks.map(eval);
  159. same(results, expected);
  160. });