blob: 1551d830d73ee6e161260adf4701cd7301aad31f (
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
const n = 1000
const c = 100
fn f(ch chan int) {
for _ in 0 .. n {
_ := <-ch
}
ch.close()
}
fn test_push_or_unbuffered() {
ch := chan int{}
go f(ch)
mut j := 0
for {
ch <- j or { break }
j++
}
assert j == n
}
fn test_push_or_buffered() {
ch := chan int{cap: c}
go f(ch)
mut j := 0
for {
ch <- j or { break }
j++
}
// we don't know how many elements are in the buffer when the channel
// is closed, so check j against an interval
assert j >= n
assert j <= n + c
}
fn g(ch chan int, res chan int) {
mut j := 0
for {
ch <- j or { break }
j++
}
println('done $j')
res <- j
}
fn test_many_senders() {
ch := chan int{}
res := chan int{}
go g(ch, res)
go g(ch, res)
go g(ch, res)
mut k := 0
for _ in 0 .. 3 * n {
k = <-ch
}
ch.close()
mut sum := <-res
sum += <-res
sum += <-res
assert sum == 3 * n
}
|