-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4.test.js
More file actions
42 lines (32 loc) · 1000 Bytes
/
ex4.test.js
File metadata and controls
42 lines (32 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const { runLockCompetition } = require('./ex4');
function assert(condition, message) {
if (!condition) throw new Error(message || 'Assertion failed');
}
async function assertThrows(fn, msg) {
let ok = false;
try { await fn(); } catch { ok = true; }
assert(ok, msg);
}
(async () => {
await assertThrows(() => runLockCompetition(0), 'rounds must validate >= 1');
const rounds = 120;
const result = await runLockCompetition(rounds);
assert(
result.counter === rounds * 2,
`Expected critical section count ${rounds * 2}, got ${result.counter}`
);
assert(result.overlapDetected === false, 'Expected strict mutual exclusion');
assert(
Number.isInteger(result.waitsObserved),
'waitsObserved must be an integer counter'
);
assert(
result.waitsObserved > 0,
'Expected blocking lock implementation to observe Atomics.wait usage'
);
console.log('ex4 tests passed');
})().catch((err) => {
console.error(err);
process.exit(1);
});