-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtee_test.go
86 lines (69 loc) · 1.52 KB
/
tee_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
77
78
79
80
81
82
83
84
85
86
package pipeline
import (
"testing"
)
func TestTee(t *testing.T) {
t.Run("test happy path", func(t *testing.T) {
ctx := make(mockContext)
defer close(ctx)
inputCh := make(chan bool, 1)
inputCh <- true
close(inputCh)
outputCh1, outputCh2 := Tee(ctx, inputCh)
got1, got2 := false, false
for {
select {
case <-ctx.Done():
t.Error("Should not have timed out")
return
case output := <-outputCh1:
if output != true {
t.Errorf("Expected output to be true, got %v", output)
} else {
got1 = true
}
case output := <-outputCh2:
if output != true {
t.Errorf("Expected output to be true, got %v", output)
} else {
got2 = true
}
default:
if got1 && got2 {
return
}
}
}
})
t.Run("test input stream closing", func(t *testing.T) {
ctx := make(mockContext)
defer close(ctx)
inputCh := make(chan bool)
outputCh1, outputCh2 := Tee(ctx, inputCh)
close(inputCh)
_, ok := <-outputCh1
if ok {
t.Errorf("Expected output to be closed, got %v", ok)
}
_, ok = <-outputCh2
if ok {
t.Errorf("Expected output to be closed, got %v", ok)
}
})
t.Run("test ctx.Done()", func(t *testing.T) {
ctx := make(mockContext)
defer close(ctx)
inputCh := make(chan bool)
defer close(inputCh)
outputCh1, outputCh2 := Tee(ctx, inputCh)
ctx <- struct{}{}
_, ok := <-outputCh1
if ok {
t.Errorf("Expected output to be closed, got %v", ok)
}
_, ok = <-outputCh2
if ok {
t.Errorf("Expected output to be closed, got %v", ok)
}
})
}