-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_test.go
76 lines (63 loc) · 1.37 KB
/
wait_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package pipe
import (
"testing"
"time"
)
func TestWait(t *testing.T) {
t.Run("Wait", func(t *testing.T) {
input := make(chan struct{}, 1)
go func() {
<-time.After(time.Millisecond * 5)
input <- struct{}{}
close(input)
}()
start := time.Now()
<-Wait(input)
if time.Since(start).Milliseconds() < 4 {
t.Fatal("too fast")
}
if time.Since(start).Milliseconds() > 6 {
t.Fatal("too slow")
}
})
t.Run("WaitAll", func(t *testing.T) {
input1 := make(chan struct{}, 1)
input2 := make(chan struct{}, 1)
go func() {
<-time.After(time.Millisecond * 5)
input1 <- struct{}{}
close(input1)
<-time.After(time.Millisecond * 3)
input2 <- struct{}{}
close(input2)
}()
start := time.Now()
<-WaitAll(input1, input2)
if time.Since(start).Milliseconds() < 7 {
t.Fatal("too fast")
}
if time.Since(start).Milliseconds() > 9 {
t.Fatal("too slow")
}
})
t.Run("WaitAny", func(t *testing.T) {
input1 := make(chan struct{}, 1)
input2 := make(chan struct{}, 1)
go func() {
<-time.After(time.Millisecond * 5)
input1 <- struct{}{}
close(input1)
<-time.After(time.Millisecond * 3)
input2 <- struct{}{}
close(input2)
}()
start := time.Now()
<-WaitAny(input1, input2)
if time.Since(start).Milliseconds() < 4 {
t.Fatal("too fast")
}
if time.Since(start).Milliseconds() > 6 {
t.Fatal("too slow")
}
})
}