Offload functions to worker threads with shared memory primitives for Node.js.
1import { describe, it } from 'node:test';
2import assert from 'node:assert/strict';
3import { workers } from 'moroutine';
4import { double, add } from './fixtures/math.ts';
5
6describe('workers', () => {
7 it('executes a moroutine through the pool', async () => {
8 const run = workers(2);
9 try {
10 const result = await run(double(2));
11 assert.equal(result, 4);
12 } finally {
13 run[Symbol.dispose]();
14 }
15 });
16
17 it('handles concurrent calls across pool workers', async () => {
18 const run = workers(2);
19 try {
20 const results = await run([double(1), double(2), double(3), double(4)]);
21 assert.deepEqual(results, [2, 4, 6, 8]);
22 } finally {
23 run[Symbol.dispose]();
24 }
25 });
26
27 it('handles multiple argument moroutines', async () => {
28 const run = workers(1);
29 try {
30 const result = await run(add(10, 20));
31 assert.equal(result, 30);
32 } finally {
33 run[Symbol.dispose]();
34 }
35 });
36
37 it('dispose terminates pool workers', async () => {
38 const run = workers(2);
39 await run(double(1));
40 run[Symbol.dispose]();
41 // After dispose, calls should fail
42 await assert.rejects(() => run(double(1)));
43 });
44});