-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_test.go
65 lines (51 loc) · 1.16 KB
/
filter_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
package pipeline
import (
"context"
"testing"
)
func TestFilter(t *testing.T) {
t.Run("test happy path", func(t *testing.T) {
ctx := make(mockContext)
defer close(ctx)
inputCh := make(chan bool, 2)
inputCh <- true
inputCh <- false
close(inputCh)
trueCh, falseCh := FilterFunc(ctx, inputCh, func(ctx context.Context, b bool) bool {
return b
})
val := <-trueCh
if val != true {
t.Errorf("Expected output to be true, got false")
}
val = <-falseCh
if val != false {
t.Errorf("Expected output to be false, got true")
}
_, ok := <-trueCh
if ok {
t.Errorf("Expected output to be closed, but was not")
}
_, ok = <-falseCh
if ok {
t.Errorf("Expected output to be closed, but was not")
}
})
t.Run("test ctx.Done()", func(t *testing.T) {
ctx := make(mockContext)
inputCh := make(chan bool)
defer close(inputCh)
trueCh, falseCh := FilterFunc(ctx, inputCh, func(ctx context.Context, b bool) bool {
return b
})
ctx <- struct{}{}
_, ok := <-trueCh
if ok {
t.Errorf("Expected output to be closed, but was not")
}
_, ok = <-falseCh
if ok {
t.Errorf("Expected output to be closed, but was not")
}
})
}