Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add Worker SAB tests #613

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions tests/helpers/worker-sab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
self.addEventListener('message', e => {
const magic = 42;
const i32 = e.data;

Atomics.wait(i32, 0, 0);

self.postMessage({ success: i32[0] === magic });
});

self.addEventListener('messageerror', e => {
throw new Error(`Opps! ${e}`);
});
27 changes: 27 additions & 0 deletions tests/test-worker-sab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import assert from 'tjs:assert';
import path from 'tjs:path';


const magic = 42;
const sab = new SharedArrayBuffer(16);
const i32 = new Int32Array(sab);

const w = new Worker(path.join(import.meta.dirname, 'helpers', 'worker-sab.js'));
const timer = setTimeout(() => {
w.terminate();
assert.fail('Timeout out waiting for worker');
}, 1500);
w.onmessage = event => {
clearTimeout(timer);
assert.ok(event.data.success, 'The value change was detected');
assert.eq(i32[0], magic, 'Magic value was set');
w.terminate();
};
w.onmessageerror = event => {
assert.fail(`Error receiving message from worker: ${event}`);
};
w.postMessage(i32);
setTimeout(() => {
Atomics.store(i32, 0 , magic);
Atomics.notify(i32, 0);
}, 100);
Loading