1 |
import Condition from "@framework/concurrent/Condition"; |
2 |
import { beforeEach, describe, expect, it } from "vitest"; |
3 |
|
4 |
describe("Condition", () => { |
5 |
let condition: Condition; |
6 |
|
7 |
beforeEach(() => { |
8 |
condition = new Condition(); |
9 |
}); |
10 |
|
11 |
it("should wait until signaled", async () => { |
12 |
let isSignaled = false; |
13 |
|
14 |
setTimeout(() => { |
15 |
condition.signal(); |
16 |
isSignaled = true; |
17 |
}, 100); |
18 |
|
19 |
await condition.wait(); |
20 |
|
21 |
expect(isSignaled).toBe(true); |
22 |
}); |
23 |
|
24 |
it("should handle multiple waiters", async () => { |
25 |
let isSignaled1 = false; |
26 |
let isSignaled2 = false; |
27 |
|
28 |
setTimeout(() => { |
29 |
condition.signal(); |
30 |
isSignaled1 = true; |
31 |
}, 100); |
32 |
|
33 |
setTimeout(() => { |
34 |
condition.signal(); |
35 |
isSignaled2 = true; |
36 |
}, 200); |
37 |
|
38 |
await Promise.all([condition.wait(), condition.wait()]); |
39 |
|
40 |
expect(isSignaled1).toBe(true); |
41 |
expect(isSignaled2).toBe(true); |
42 |
}); |
43 |
}); |