-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge_test.go
122 lines (92 loc) · 2.31 KB
/
bridge_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package pipeline
import (
"context"
"testing"
)
func TestBridge(t *testing.T) {
t.Run("test happy path", func(t *testing.T) {
inputChs := make(chan (<-chan bool), 2)
inputCh1 := make(chan bool, 1)
inputCh1 <- true
close(inputCh1)
inputCh2 := make(chan bool, 1)
inputCh2 <- true
close(inputCh2)
inputChs <- inputCh1
inputChs <- inputCh2
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
outputCh := Bridge(ctx, inputChs)
val1 := <-outputCh
if val1 != true {
t.Errorf("Expected output to be true, got %v", val1)
}
val2 := <-outputCh
if val2 != true {
t.Errorf("Expected output to be true, got %v", val2)
}
})
t.Run("test input stream closing", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
inputChs := make(chan (<-chan bool))
outputCh := Bridge(ctx, inputChs)
close(inputChs)
_, ok := <-outputCh
if ok {
t.Errorf("Expected output channel to be closed")
}
})
t.Run("test an input stream closing", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
inputChs := make(chan (<-chan bool), 2)
inputCh1 := make(chan bool, 1)
inputCh2 := make(chan bool)
inputCh3 := make(chan bool, 1)
outputCh := Bridge(ctx, inputChs)
inputChs <- inputCh1
inputChs <- inputCh2
inputChs <- inputCh3
close(inputCh2)
inputCh1 <- true
inputCh3 <- true
val1 := <-outputCh
if val1 != true {
t.Errorf("Expected output to be true, got %v", val1)
}
val2 := <-outputCh
if val2 != true {
t.Errorf("Expected output to be true, got %v", val2)
}
select {
case _, ok := <-outputCh:
if !ok {
t.Errorf("Expected output channel to still be open")
}
default:
inputCh1 <- true
inputCh3 <- true
}
_, ok := <-outputCh
if !ok {
t.Errorf("Expected output channel to be open")
}
})
t.Run("test ctx.Done()", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
inputChs := make(chan (<-chan bool), 2)
inputCh1 := make(chan bool, 1)
defer close(inputCh1)
inputCh2 := make(chan bool, 1)
defer close(inputCh2)
inputChs <- inputCh1
inputChs <- inputCh2
outputCh := Bridge(ctx, inputChs)
cancel()
_, ok := <-outputCh
if ok {
t.Errorf("Expected output to be closed, but was not")
}
})
}