2020-08-23 02:12:05 +02:00
|
|
|
import sync
|
|
|
|
|
|
|
|
const (
|
|
|
|
num_iterations = 10000
|
|
|
|
)
|
|
|
|
|
|
|
|
fn get_val_from_chan(ch chan i64) ?i64 {
|
2020-09-08 00:36:05 +02:00
|
|
|
r := <-ch ?
|
2020-08-23 02:12:05 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// this function gets an array of channels for `i64`
|
|
|
|
fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) {
|
|
|
|
mut msg := ''
|
|
|
|
for {
|
|
|
|
mut s := get_val_from_chan(chs[0]) or {
|
|
|
|
msg = err.str()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
s++
|
|
|
|
chs[1] <- s
|
|
|
|
}
|
|
|
|
assert msg == 'channel closed'
|
|
|
|
sem.post()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_channel_array_mut() {
|
|
|
|
mut chs := [chan i64{}, chan i64{cap: 10}]
|
|
|
|
sem := sync.new_semaphore()
|
|
|
|
go do_rec_calc_send(chs, sem)
|
|
|
|
mut t := i64(100)
|
|
|
|
for _ in 0 .. num_iterations {
|
|
|
|
chs[0] <- t
|
|
|
|
t = <-chs[1]
|
|
|
|
}
|
2020-09-08 00:36:05 +02:00
|
|
|
chs[0].close()
|
2020-08-23 02:12:05 +02:00
|
|
|
sem.wait()
|
|
|
|
assert t == 100 + num_iterations
|
|
|
|
}
|