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 { mo } from 'moroutine';
4
5describe('mo()', () => {
6 it('returns a function', () => {
7 const double = mo(import.meta, (x: number) => 2 * x);
8 assert.equal(typeof double, 'function');
9 });
10
11 it('calling it returns a Task with correct args', () => {
12 const double = mo(import.meta, (x: number) => 2 * x);
13 const task = double(2);
14 assert.deepEqual(task.args, [2]);
15 });
16
17 it('Task is a thenable', () => {
18 const double = mo(import.meta, (x: number) => 2 * x);
19 const task = double(2);
20 assert.equal(typeof task.then, 'function');
21 });
22
23 it('tasks from the same moroutine share an id', () => {
24 const double = mo(import.meta, (x: number) => 2 * x);
25 assert.equal(double(1).id, double(2).id);
26 });
27
28 it('different moroutines have different ids', () => {
29 const a = mo(import.meta, (x: number) => x + 1);
30 const b = mo(import.meta, (x: number) => x + 2);
31 assert.notEqual(a(1).id, b(1).id);
32 });
33});