blob: a7e26fe8590b691b63d19e95cbc6f05c8761df64 (
plain)
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
|
import sync
const (
num_iterations = 10000
)
fn get_val_from_chan(ch chan i64) ?i64 {
r := <-ch ?
return r
}
// this function gets an array of channels for `i64`
fn do_rec_calc_send(chs []chan i64, mut sem sync.Semaphore) {
mut msg := ''
for {
mut s := get_val_from_chan(chs[0]) or {
msg = err.msg
break
}
s++
chs[1] <- s
}
assert msg == 'channel closed'
sem.post()
}
fn test_channel_array_mut() {
mut chs := [chan i64{}, chan i64{cap: 10}]
mut sem := sync.new_semaphore()
go do_rec_calc_send(chs, mut sem)
mut t := i64(100)
for _ in 0 .. num_iterations {
chs[0] <- t
t = <-chs[1]
}
chs[0].close()
sem.wait()
assert t == 100 + num_iterations
}
|