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