Offload functions to worker threads with shared memory primitives for Node.js.
8
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 59 lines 1.9 kB view raw
1import { describe, it } from 'node:test'; 2import assert from 'node:assert/strict'; 3import { shared, bytes, bool } from 'moroutine'; 4 5describe('bytes', () => { 6 it('bytes(n) creates standalone byte buffer', () => { 7 const buf = bytes(16); 8 const data = buf.load(); 9 assert.equal(data.length, 16); 10 assert.equal(data[0], 0); 11 }); 12 13 it('shared(bytes(n)) creates byte buffer', () => { 14 const buf = shared(bytes(16)); 15 assert.equal(buf.load().length, 16); 16 }); 17 18 it('load returns view (not copy)', () => { 19 const buf = bytes(4); 20 buf.view[0] = 42; 21 assert.equal(buf.load()[0], 42); 22 }); 23 24 it('store writes exact length data', () => { 25 const buf = bytes(4); 26 buf.store(new Uint8Array([1, 2, 3, 4])); 27 assert.deepEqual([...buf.load()], [1, 2, 3, 4]); 28 }); 29 30 it('store throws if length does not match', () => { 31 const buf = bytes(4); 32 assert.throws(() => buf.store(new Uint8Array([1, 2, 3])), /length/i); 33 assert.throws(() => buf.store(new Uint8Array([1, 2, 3, 4, 5])), /length/i); 34 }); 35 36 it('view provides mutable direct access', () => { 37 const buf = bytes(4); 38 buf.view[0] = 0xff; 39 buf.view[3] = 0xaa; 40 assert.equal(buf.load()[0], 0xff); 41 assert.equal(buf.load()[3], 0xaa); 42 }); 43 44 it('bytes in a struct schema', () => { 45 const s = shared({ data: bytes(8), flag: bool }); 46 s.fields.data.store(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8])); 47 s.fields.flag.store(true); 48 assert.deepEqual([...s.fields.data.load()], [1, 2, 3, 4, 5, 6, 7, 8]); 49 assert.equal(s.fields.flag.load(), true); 50 }); 51 52 it('bytes in struct shares one buffer', () => { 53 const s = shared({ data: bytes(8), flag: bool }); 54 const SHARED = Symbol.for('moroutine.shared'); 55 const dataSer = (s.fields.data as any)[SHARED](); 56 const flagSer = (s.fields.flag as any)[SHARED](); 57 assert.equal(dataSer.buffer, flagSer.buffer); 58 }); 59});