|
| 1 | +'use strict'; |
| 2 | +require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const child_process = require('child_process'); |
| 5 | +const { Worker } = require('worker_threads'); |
| 6 | + |
| 7 | +if (process.argv[2] === 'child') { |
| 8 | + const i32arr = new Int32Array(new SharedArrayBuffer(8)); |
| 9 | + assert.strictEqual(Atomics.wait(i32arr, 0, 1), 'not-equal'); |
| 10 | + assert.strictEqual(Atomics.wait(i32arr, 0, 0, 10), 'timed-out'); |
| 11 | + |
| 12 | + new Worker(` |
| 13 | + const i32arr = require('worker_threads').workerData; |
| 14 | + Atomics.store(i32arr, 1, -1); |
| 15 | + Atomics.notify(i32arr, 1); |
| 16 | + Atomics.wait(i32arr, 1, -1); |
| 17 | + `, { eval: true, workerData: i32arr }); |
| 18 | + |
| 19 | + Atomics.wait(i32arr, 1, 0); |
| 20 | + assert.strictEqual(Atomics.load(i32arr, 1), -1); |
| 21 | + Atomics.store(i32arr, 1, 0); |
| 22 | + Atomics.notify(i32arr, 1); |
| 23 | + return; |
| 24 | +} |
| 25 | + |
| 26 | +const proc = child_process.spawnSync( |
| 27 | + process.execPath, |
| 28 | + [ '--trace-atomics-wait', __filename, 'child' ], |
| 29 | + { encoding: 'utf8', stdio: [ 'inherit', 'inherit', 'pipe' ] }); |
| 30 | + |
| 31 | +if (proc.status !== 0) console.log(proc); |
| 32 | +assert.strictEqual(proc.status, 0); |
| 33 | + |
| 34 | +const expectedLines = [ |
| 35 | + { threadId: 0, offset: 0, value: 1, timeout: 'inf', |
| 36 | + message: 'started' }, |
| 37 | + { threadId: 0, offset: 0, value: 1, timeout: 'inf', |
| 38 | + message: 'did not wait because the values mismatched' }, |
| 39 | + { threadId: 0, offset: 0, value: 0, timeout: '10', |
| 40 | + message: 'started' }, |
| 41 | + { threadId: 0, offset: 0, value: 0, timeout: '10', |
| 42 | + message: 'timed out' }, |
| 43 | + { threadId: 0, offset: 4, value: 0, timeout: 'inf', |
| 44 | + message: 'started' }, |
| 45 | + { threadId: 1, offset: 4, value: -1, timeout: 'inf', |
| 46 | + message: 'started' }, |
| 47 | + { threadId: 0, offset: 4, value: 0, timeout: 'inf', |
| 48 | + message: 'was woken up by another thread' }, |
| 49 | + { threadId: 1, offset: 4, value: -1, timeout: 'inf', |
| 50 | + message: 'was woken up by another thread' } |
| 51 | +]; |
| 52 | + |
| 53 | +let SABAddress; |
| 54 | +const re = /^\[Thread (?<threadId>\d+)\] Atomics\.wait\((?<SAB>(?:0x)?[0-9a-f]+) \+ (?<offset>\d+), (?<value>-?\d+), (?<timeout>inf|infinity|[0-9.]+)\) (?<message>.+)$/; |
| 55 | +for (const line of proc.stderr.split('\n').map((line) => line.trim())) { |
| 56 | + if (!line) continue; |
| 57 | + console.log('Matching', { line }); |
| 58 | + const actual = line.match(re).groups; |
| 59 | + const expected = expectedLines.shift(); |
| 60 | + |
| 61 | + if (SABAddress === undefined) |
| 62 | + SABAddress = actual.SAB; |
| 63 | + else |
| 64 | + assert.strictEqual(actual.SAB, SABAddress); |
| 65 | + |
| 66 | + assert.strictEqual(+actual.threadId, expected.threadId); |
| 67 | + assert.strictEqual(+actual.offset, expected.offset); |
| 68 | + assert.strictEqual(+actual.value, expected.value); |
| 69 | + assert.strictEqual(actual.message, expected.message); |
| 70 | + if (expected.timeout === 'inf') |
| 71 | + assert.match(actual.timeout, /inf(inity)?/); |
| 72 | + else |
| 73 | + assert.strictEqual(actual.timeout, expected.timeout); |
| 74 | +} |
0 commit comments