-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathor_done_test.go
51 lines (47 loc) · 1004 Bytes
/
or_done_test.go
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
package go_concurrent_utils
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestOrDone(t *testing.T) {
t.Run("finish not by done", func(t *testing.T) {
done := make(chan interface{})
ints := IntGenerator(done, []int{1, 1, 1, 1}...)
iface := make(chan interface{})
go func() {
defer close(iface)
for i := range ints {
iface <- i
}
}()
for val := range OrDone(done, iface) {
assert.Equal(t, 1, val.(int))
}
})
t.Run("finish by done", func(t *testing.T) {
done := make(chan interface{})
ints := IntGenerator(done, []int{1, 1, 1, 1}...)
iface := make(chan interface{})
go func() {
defer close(iface)
for i := range ints {
iface <- i
}
}()
close(done)
finishedByDone := false
loop:
for {
select {
case <-OrDone(done, iface):
finishedByDone = true
break loop
case <-time.After(5 * time.Second):
assert.Fail(t, "not finished by done")
break loop
}
}
assert.True(t, finishedByDone)
})
}