-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcondition-variable.test.ts
More file actions
198 lines (180 loc) · 7.28 KB
/
condition-variable.test.ts
File metadata and controls
198 lines (180 loc) · 7.28 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { describe, it } from "node:test";
import assert from "node:assert";
import { ConditionVariable } from "./condition-variable.ts";
import { SimulationImpl, type SimulationTask } from "./simulation.ts";
import { ArrayLogger, FixedEntropySource } from "./test-helpers.ts";
describe("ConditionVariable", () => {
it("wait + notifyAll basic flow", async () => {
// Entropy: pick waiter first from [waiter, notifier]
const sim = new SimulationImpl(new ArrayLogger(), new FixedEntropySource([0]), 0);
const cv = new ConditionVariable("test");
const order: string[] = [];
const result = await sim.runTasks([
{
name: "waiter",
f: async (task: SimulationTask) => {
await cv.wait(task, "signal");
order.push("waiter-resumed");
return "waited";
},
},
{
name: "notifier",
f: async (task: SimulationTask) => {
cv.notifyAll(task, "signal");
order.push("notifier-done");
return "notified";
},
},
]);
assert.ok(result.isOk());
assert.deepStrictEqual(result.value, ["waited", "notified"]);
// Notifier completes before waiter resumes
assert.deepStrictEqual(order, ["notifier-done", "waiter-resumed"]);
});
it("multiple waiters all wake", async () => {
// Entropy:
// 1. Pick from [w1, w2, notifier] (3 items): 0 -> w1
// 2. Pick from [w2, notifier] (2 items): 0 -> w2
// 3. After notify, pick from [w1, w2] (2 items): 0 -> w1
const sim = new SimulationImpl(new ArrayLogger(), new FixedEntropySource([0, 0, 0]), 0);
const cv = new ConditionVariable("test");
const order: string[] = [];
const result = await sim.runTasks([
{
name: "w1",
f: async (task: SimulationTask) => {
await cv.wait(task, "signal");
order.push("w1");
return "w1";
},
},
{
name: "w2",
f: async (task: SimulationTask) => {
await cv.wait(task, "signal");
order.push("w2");
return "w2";
},
},
{
name: "notifier",
f: async (task: SimulationTask) => {
cv.notifyAll(task, "signal");
order.push("notifier");
return "notifier";
},
},
]);
assert.ok(result.isOk());
assert.deepStrictEqual(result.value, ["w1", "w2", "notifier"]);
assert.deepStrictEqual(order, ["notifier", "w1", "w2"]);
});
it("notifyAll with no waiters does not throw", async () => {
const sim = new SimulationImpl(new ArrayLogger(), new FixedEntropySource([]), 0);
const cv = new ConditionVariable("test");
const result = await sim.runTasks([
{
name: "notifier",
f: async (task: SimulationTask) => {
cv.notifyAll(task, "signal");
return "done";
},
},
]);
assert.ok(result.isOk());
});
it("wait blocks until notified", async () => {
// Pick waiter first
const sim = new SimulationImpl(new ArrayLogger(), new FixedEntropySource([0]), 0);
const cv = new ConditionVariable("test");
const order: string[] = [];
const result = await sim.runTasks([
{
name: "waiter",
f: async (task: SimulationTask) => {
order.push("waiter-before");
await cv.wait(task, "signal");
order.push("waiter-after");
return "waited";
},
},
{
name: "notifier",
f: async (task: SimulationTask) => {
order.push("notifier-before");
cv.notifyAll(task, "signal");
order.push("notifier-after");
return "notified";
},
},
]);
assert.ok(result.isOk());
// Waiter starts first (picked by entropy), blocks on wait,
// notifier runs and notifies, notifier finishes, then waiter resumes
assert.ok(order.indexOf("waiter-before") < order.indexOf("notifier-before"));
assert.ok(order.indexOf("notifier-after") < order.indexOf("waiter-after"));
});
it("reuse: wait/notifyAll works across multiple cycles on the same CV", async () => {
// Entropy:
// 1. Pick from [waiter, notifier]: 0 -> waiter
// 2. After first notify + checkpoint, pick from [waiter, notifier]: 0 -> waiter
const sim = new SimulationImpl(new ArrayLogger(), new FixedEntropySource([0, 0]), 0);
const cv = new ConditionVariable("test");
const order: string[] = [];
const result = await sim.runTasks([
{
name: "waiter",
f: async (task: SimulationTask) => {
await cv.wait(task, "cycle-1");
order.push("wake-1");
await cv.wait(task, "cycle-2");
order.push("wake-2");
return "waited";
},
},
{
name: "notifier",
f: async (task: SimulationTask) => {
cv.notifyAll(task, "cycle-1");
await task.checkpoint("between");
cv.notifyAll(task, "cycle-2");
return "notified";
},
},
]);
assert.ok(result.isOk());
assert.deepStrictEqual(result.value, ["waited", "notified"]);
assert.deepStrictEqual(order, ["wake-1", "wake-2"]);
});
it("notify-before-wait causes deadlock (notifications are not sticky)", async () => {
// Entropy: pick notifier first (0.999 -> index 1 of [waiter, notifier])
const sim = new SimulationImpl(new ArrayLogger(), new FixedEntropySource([0.999]), 0);
const cv = new ConditionVariable("test");
const order: string[] = [];
const result = await sim.runTasks([
{
name: "waiter",
f: async (task: SimulationTask) => {
order.push("waiter-start");
await cv.wait(task, "signal");
order.push("waiter-resumed");
return "waited";
},
},
{
name: "notifier",
f: async (task: SimulationTask) => {
order.push("notifier-start");
cv.notifyAll(task, "signal");
order.push("notifier-done");
return "notified";
},
},
]);
assert.ok(result.isErr());
// Notifier completed, but waiter never resumed (lost wakeup)
assert.ok(order.includes("notifier-done"), "notifier should have completed");
assert.ok(!order.includes("waiter-resumed"), "waiter should never have resumed");
});
});