From 4547ff6c5b5657a26ddf507f6674de6c4a78ce30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 16 Jul 2024 19:44:47 +0200 Subject: [PATCH] test: add Worker SAB tests --- tests/helpers/worker-sab.js | 12 ++++++++++++ tests/test-worker-sab.js | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/helpers/worker-sab.js create mode 100644 tests/test-worker-sab.js diff --git a/tests/helpers/worker-sab.js b/tests/helpers/worker-sab.js new file mode 100644 index 00000000..febeb68e --- /dev/null +++ b/tests/helpers/worker-sab.js @@ -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}`); +}); diff --git a/tests/test-worker-sab.js b/tests/test-worker-sab.js new file mode 100644 index 00000000..7ae429b3 --- /dev/null +++ b/tests/test-worker-sab.js @@ -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);