2021-04-09 23:40:35 +03:00
|
|
|
import time
|
2020-08-10 07:22:20 +03:00
|
|
|
|
2021-01-22 10:37:29 +03:00
|
|
|
fn do_rec(ch chan int, resch chan i64) {
|
2020-08-10 07:22:20 +03:00
|
|
|
mut sum := i64(0)
|
2020-10-26 15:41:57 +03:00
|
|
|
for {
|
2021-01-22 10:37:29 +03:00
|
|
|
a := <-ch or {
|
2020-08-10 07:22:20 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
sum += a
|
|
|
|
}
|
2021-01-22 10:37:29 +03:00
|
|
|
assert ch.closed == true
|
2020-08-10 07:22:20 +03:00
|
|
|
println(sum)
|
2021-01-22 10:37:29 +03:00
|
|
|
resch <- sum
|
2020-08-10 07:22:20 +03:00
|
|
|
}
|
|
|
|
|
2021-01-22 10:37:29 +03:00
|
|
|
fn do_send(ch chan int) {
|
2020-08-10 07:22:20 +03:00
|
|
|
for i in 0 .. 8000 {
|
2021-01-22 10:37:29 +03:00
|
|
|
ch <- i
|
2020-08-10 07:22:20 +03:00
|
|
|
}
|
2021-01-22 10:37:29 +03:00
|
|
|
assert ch.closed == false
|
2020-08-10 07:22:20 +03:00
|
|
|
ch.close()
|
2021-01-22 10:37:29 +03:00
|
|
|
assert ch.closed == true
|
2020-08-10 07:22:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_channel_close_buffered_multi() {
|
2021-01-22 10:37:29 +03:00
|
|
|
ch := chan int{cap: 10}
|
|
|
|
resch := chan i64{}
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_send(ch)
|
2020-08-10 07:22:20 +03:00
|
|
|
mut sum := i64(0)
|
|
|
|
for _ in 0 .. 4 {
|
2021-01-22 10:37:29 +03:00
|
|
|
sum += <- resch
|
2020-08-10 07:22:20 +03:00
|
|
|
}
|
|
|
|
assert sum == i64(8000) * (8000 - 1) / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_channel_close_unbuffered_multi() {
|
2021-01-22 10:37:29 +03:00
|
|
|
ch := chan int{}
|
|
|
|
resch := chan i64{}
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_send(ch)
|
2020-08-10 07:22:20 +03:00
|
|
|
mut sum := i64(0)
|
|
|
|
for _ in 0 .. 4 {
|
2021-01-22 10:37:29 +03:00
|
|
|
sum += <-resch
|
2020-08-10 07:22:20 +03:00
|
|
|
}
|
|
|
|
assert sum == i64(8000) * (8000 - 1) / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_channel_close_buffered() {
|
2021-01-22 10:37:29 +03:00
|
|
|
ch := chan int{cap: 100}
|
|
|
|
resch := chan i64{}
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_send(ch)
|
2020-08-10 07:22:20 +03:00
|
|
|
mut sum := i64(0)
|
2021-01-22 10:37:29 +03:00
|
|
|
sum += <-resch
|
2020-08-10 07:22:20 +03:00
|
|
|
assert sum == i64(8000) * (8000 - 1) / 2
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_channel_close_unbuffered() {
|
2021-01-22 10:37:29 +03:00
|
|
|
ch := chan int{}
|
|
|
|
resch := chan i64{cap: 100}
|
|
|
|
go do_rec(ch, resch)
|
|
|
|
go do_send(ch)
|
2020-08-10 07:22:20 +03:00
|
|
|
mut sum := i64(0)
|
2021-01-22 10:37:29 +03:00
|
|
|
sum += <-resch
|
2020-08-10 07:22:20 +03:00
|
|
|
assert sum == i64(8000) * (8000 - 1) / 2
|
|
|
|
}
|
2021-04-09 23:40:35 +03:00
|
|
|
|
|
|
|
fn test_channel_send_close_buffered() {
|
|
|
|
ch := chan int{cap: 1}
|
|
|
|
t := go fn (ch chan int) {
|
|
|
|
ch <- 31
|
|
|
|
mut x := 45
|
|
|
|
ch <- 17 or {
|
|
|
|
x = -133
|
|
|
|
}
|
|
|
|
assert x == -133
|
|
|
|
}(ch)
|
|
|
|
time.sleep(100 * time.millisecond)
|
|
|
|
ch.close()
|
|
|
|
mut r := <-ch
|
|
|
|
r = <-ch or { 23 }
|
|
|
|
assert r == 23
|
|
|
|
t.wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_channel_send_close_unbuffered() {
|
|
|
|
time.sleep(1 * time.second)
|
|
|
|
ch := chan int{}
|
|
|
|
t := go fn (ch chan int) {
|
|
|
|
mut x := 31
|
|
|
|
ch <- 177 or {
|
|
|
|
x = -71
|
|
|
|
}
|
|
|
|
assert x == -71
|
|
|
|
}(ch)
|
|
|
|
time.sleep(100 * time.millisecond)
|
|
|
|
ch.close()
|
|
|
|
r := <-ch or { 238 }
|
|
|
|
assert r == 238
|
|
|
|
t.wait()
|
|
|
|
}
|