1 | module sync |
2 | |
3 | import time |
4 | |
5 | fn test_waitgroup_reuse() { |
6 | mut wg := new_waitgroup() |
7 | |
8 | wg.add(1) |
9 | wg.done() |
10 | |
11 | wg.add(1) |
12 | mut executed := false |
13 | spawn fn (mut wg WaitGroup, executed voidptr) { |
14 | defer { |
15 | wg.done() |
16 | } |
17 | unsafe { |
18 | *(&bool(executed)) = true |
19 | } |
20 | time.sleep(100 * time.millisecond) |
21 | assert wg.wait_count == 1 |
22 | }(mut wg, voidptr(&executed)) |
23 | |
24 | wg.wait() |
25 | assert executed |
26 | assert wg.wait_count == 0 |
27 | } |
28 | |
29 | fn test_waitgroup_no_use() { |
30 | mut done := false |
31 | spawn fn (done voidptr) { |
32 | time.sleep(1 * time.second) |
33 | if *(&bool(done)) == false { |
34 | panic('test_waitgroup_no_use did not complete in time') |
35 | } |
36 | }(voidptr(&done)) |
37 | |
38 | mut wg := new_waitgroup() |
39 | wg.wait() |
40 | done = true |
41 | } |