-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsink_test.go
53 lines (39 loc) · 894 Bytes
/
sink_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
package pipeline
import (
"context"
"testing"
)
func TestSinkFunc(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)
i := 0
done := make(chan struct{})
SinkFunc(ctx, inputCh, func(ctx context.Context, input bool) {
i++
done <- struct{}{}
})
<-done
if i != 1 {
t.Errorf("Expected i to be 1, got %d", i)
}
})
t.Run("test ctx.Done()", func(t *testing.T) {
ctx := make(mockContext)
defer close(ctx)
var outputs []bool
inputCh := make(chan bool, 1)
defer close(inputCh)
SinkFunc(ctx, inputCh, func(ctx context.Context, val bool) {
outputs = append(outputs, val)
})
ctx <- struct{}{}
inputCh <- true
if len(outputs) != 0 {
t.Errorf("Expected 0 outputs due to closed context, got %d", len(outputs))
}
})
}