1 | import sync |
2 | |
3 | const ( |
4 | num_iterations = 10000 |
5 | ) |
6 | |
7 | fn get_val_from_chan(ch chan i64) ?i64 { |
8 | r := <-ch? |
9 | return r |
10 | } |
11 | |
12 | // this function gets an array of channels for `i64` |
13 | fn do_rec_calc_send(chs []chan i64, mut sem sync.Semaphore) { |
14 | mut msg := '' |
15 | for { |
16 | mut s := get_val_from_chan(chs[0]) or { |
17 | msg = err.msg() |
18 | break |
19 | } |
20 | s++ |
21 | chs[1] <- s |
22 | } |
23 | assert msg == 'channel closed' |
24 | sem.post() |
25 | } |
26 | |
27 | fn test_channel_array_mut() { |
28 | mut chs := [chan i64{}, chan i64{cap: 10}] |
29 | mut sem := sync.new_semaphore() |
30 | spawn do_rec_calc_send(chs, mut sem) |
31 | mut t := i64(100) |
32 | for _ in 0 .. num_iterations { |
33 | chs[0] <- t |
34 | t = <-chs[1] |
35 | } |
36 | chs[0].close() |
37 | sem.wait() |
38 | assert t == 100 + num_iterations |
39 | } |