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 119 lines 3.2 kB view raw
1import { describe, it } from 'node:test'; 2import assert from 'node:assert/strict'; 3import { int32atomic } from 'moroutine'; 4 5describe('Int32Atomic', () => { 6 it('self-allocates and initializes to zero', () => { 7 const a = int32atomic(); 8 assert.equal(a.load(), 0); 9 }); 10 11 it('stores and loads a value', () => { 12 const a = int32atomic(); 13 a.store(42); 14 assert.equal(a.load(), 42); 15 }); 16 17 it('exposes byteSize of 4', () => { 18 assert.equal(int32atomic.byteSize, 4); 19 }); 20 21 it('add returns previous value and updates', () => { 22 const a = int32atomic(); 23 a.store(10); 24 const prev = a.add(5); 25 assert.equal(prev, 10); 26 assert.equal(a.load(), 15); 27 }); 28 29 it('sub returns previous value and updates', () => { 30 const a = int32atomic(); 31 a.store(10); 32 const prev = a.sub(3); 33 assert.equal(prev, 10); 34 assert.equal(a.load(), 7); 35 }); 36 37 it('and returns previous value and updates', () => { 38 const a = int32atomic(); 39 a.store(0b1100); 40 const prev = a.and(0b1010); 41 assert.equal(prev, 0b1100); 42 assert.equal(a.load(), 0b1000); 43 }); 44 45 it('or returns previous value and updates', () => { 46 const a = int32atomic(); 47 a.store(0b1100); 48 const prev = a.or(0b0011); 49 assert.equal(prev, 0b1100); 50 assert.equal(a.load(), 0b1111); 51 }); 52 53 it('xor returns previous value and updates', () => { 54 const a = int32atomic(); 55 a.store(0b1100); 56 const prev = a.xor(0b1010); 57 assert.equal(prev, 0b1100); 58 assert.equal(a.load(), 0b0110); 59 }); 60 61 it('exchange returns previous value and sets new', () => { 62 const a = int32atomic(); 63 a.store(42); 64 const prev = a.exchange(99); 65 assert.equal(prev, 42); 66 assert.equal(a.load(), 99); 67 }); 68 69 it('compareExchange succeeds when expected matches', () => { 70 const a = int32atomic(); 71 a.store(42); 72 const actual = a.compareExchange(42, 99); 73 assert.equal(actual, 42); 74 assert.equal(a.load(), 99); 75 }); 76 77 it('compareExchange fails when expected does not match', () => { 78 const a = int32atomic(); 79 a.store(42); 80 const actual = a.compareExchange(0, 99); 81 assert.equal(actual, 42); 82 assert.equal(a.load(), 42); 83 }); 84 85 it('waitAsync returns "not-equal" synchronously when value already differs', async () => { 86 const a = int32atomic(); 87 a.store(5); 88 const result = await a.waitAsync(0); 89 assert.equal(result, 'not-equal'); 90 }); 91 92 it('waitAsync resolves "ok" when a concurrent store + notify happens', async () => { 93 const a = int32atomic(); 94 const wait = a.waitAsync(0); 95 setImmediate(() => { 96 a.store(1); 97 a.notify(); 98 }); 99 assert.equal(await wait, 'ok'); 100 assert.equal(a.load(), 1); 101 }); 102 103 it('waitAsync resolves "timed-out" when no notify arrives', async () => { 104 const a = int32atomic(); 105 const result = await a.waitAsync(0, 20); 106 assert.equal(result, 'timed-out'); 107 }); 108 109 it('notify returns count of waiters woken', async () => { 110 const a = int32atomic(); 111 const w1 = a.waitAsync(0); 112 const w2 = a.waitAsync(0); 113 const woken = a.notify(); 114 a.store(1); 115 a.notify(); 116 assert.equal(woken + a.notify(), 2); 117 await Promise.all([w1, w2]); 118 }); 119});