vq / vlib / v / tests / concurrency / spawn_in_if_match_expr_test.v
39 lines · 33 sloc · 956 bytes · 5ad37a5a8656c438c68dabd31c9d36467390e046
Raw
1// Regression test for https://github.com/vlang/v/issues/27485
2// Assigning an `if`/`match` expression whose branches are `spawn` calls used to
3// emit a C ternary with statement-level spawn setup inside it, which failed to
4// compile (and, once that was fixed, produced an unjoinable/detached thread).
5
6fn ret(x int) int {
7 return x
8}
9
10fn test_spawn_in_if_expr() {
11 c := true
12 t := if c { spawn ret(11) } else { spawn ret(22) }
13 assert t.wait() == 11
14}
15
16fn test_spawn_in_if_expr_false_branch() {
17 c := false
18 t := if c { spawn ret(11) } else { spawn ret(22) }
19 assert t.wait() == 22
20}
21
22fn test_spawn_in_match_expr() {
23 x := 2
24 t := match x {
25 1 { spawn ret(1) }
26 2 { spawn ret(2) }
27 else { spawn ret(99) }
28 }
29
30 assert t.wait() == 2
31}
32
33fn test_spawn_in_if_expr_collected_into_array() {
34 mut threads := []thread int{}
35 for i in 0 .. 4 {
36 threads << if i % 2 == 0 { spawn ret(i) } else { spawn ret(i * 10) }
37 }
38 assert threads.wait() == [0, 10, 2, 30]
39}
40