1'use strict';
2require('../common');
3const assert = require('assert');
4const child_process = require('child_process');
5const { Worker } = require('worker_threads');
6
7if (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
26const proc = child_process.spawnSync(
27  process.execPath,
28  [ '--trace-atomics-wait', __filename, 'child' ],
29  { encoding: 'utf8', stdio: [ 'inherit', 'inherit', 'pipe' ] });
30
31if (proc.status !== 0) console.log(proc);
32assert.strictEqual(proc.status, 0);
33
34const SABAddress = proc.stderr.match(/Atomics\.wait\((?<SAB>.+) \+/).groups.SAB;
35const 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();
41console.log('+++ normalized stdout +++');
42console.log(actualTimeline);
43console.log('--- normalized stdout ---');
44
45const 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 \
48values mismatched
49[Thread 0] Atomics.wait(<address> + 0, 0, 10) started
50[Thread 0] Atomics.wait(<address> + 0, 0, 10) timed out`;
51
52const 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 1] Atomics.wait(<address> + 4, 0, inf) started
60[Thread 0] Atomics.wait(<address> + 4, -1, inf) started
61[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
62[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
63  `${begin}
64[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
65[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
66[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
67[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
68  `${begin}
69[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
70[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
71[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
72[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
73values mismatched`,
74  `${begin}
75[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
76[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
77[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
78[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
79values mismatched`,
80  `${begin}
81[Thread 1] Atomics.wait(<address> + 4, 0, inf) started
82[Thread 0] Atomics.wait(<address> + 4, -1, inf) started
83[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
84[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
85values mismatched`,
86  `${begin}
87[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
88[Thread 0] Atomics.wait(<address> + 4, 0, inf) did not wait because the \
89values mismatched
90[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
91[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
92values mismatched`,
93  `${begin}
94[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
95[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
96[Thread 0] Atomics.wait(<address> + 4, 0, inf) did not wait because the \
97values mismatched
98[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
99];
100
101assert(expectedTimelines.includes(actualTimeline));
102