|
| 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 SABAddress = proc.stderr.match(/Atomics\.wait\((?<SAB>.+) \+/).groups.SAB; |
| 35 | +const actualTimeline = proc.stderr |
| 36 | + .replace(new RegExp(SABAddress, 'g'), '<address>') |
| 37 | + .replace(new RegExp(`\\(node:${proc.pid}\\) `, 'g'), '') |
| 38 | + .replace(/\binf(inity)?\b/gi, 'inf') |
| 39 | + .replace(/\r/g, '') |
| 40 | + .trim(); |
| 41 | +console.log('+++ normalized stdout +++'); |
| 42 | +console.log(actualTimeline); |
| 43 | +console.log('--- normalized stdout ---'); |
| 44 | + |
| 45 | +const begin = |
| 46 | +`[Thread 0] Atomics.wait(<address> + 0, 1, inf) started |
| 47 | +[Thread 0] Atomics.wait(<address> + 0, 1, inf) did not wait because the \ |
| 48 | +values mismatched |
| 49 | +[Thread 0] Atomics.wait(<address> + 0, 0, 10) started |
| 50 | +[Thread 0] Atomics.wait(<address> + 0, 0, 10) timed out`; |
| 51 | + |
| 52 | +const expectedTimelines = [ |
| 53 | + `${begin} |
| 54 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) started |
| 55 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) started |
| 56 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread |
| 57 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`, |
| 58 | + `${begin} |
| 59 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) started |
| 60 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread |
| 61 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) started |
| 62 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \ |
| 63 | +values mismatched`, |
| 64 | + `${begin} |
| 65 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) started |
| 66 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) started |
| 67 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread |
| 68 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \ |
| 69 | +values mismatched`, |
| 70 | + `${begin} |
| 71 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) started |
| 72 | +[Thread 0] Atomics.wait(<address> + 4, 0, inf) did not wait because the \ |
| 73 | +values mismatched |
| 74 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) started |
| 75 | +[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \ |
| 76 | +values mismatched` |
| 77 | +]; |
| 78 | + |
| 79 | +assert(expectedTimelines.includes(actualTimeline)); |
0 commit comments