-
Notifications
You must be signed in to change notification settings - Fork 13
/
collect_test.go
161 lines (153 loc) · 3.34 KB
/
collect_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package pipeline
import (
"context"
"reflect"
"testing"
"time"
)
// TestCollect tests the following cases of the Collect func
// 1. Closes when in closes
// 2. Remains Open if in remains open
// 3. Collects max and returns immediately
// 4. Returns everything passed in if less than max after duration
// 5. After duration with nothing in the buffer, nothing is returned, channel remains open
// 6. Flushes the buffer if the context is canceled
func TestCollect(t *testing.T) {
t.Parallel()
const maxTestDuration = time.Second
type args struct {
maxSize int
maxDuration time.Duration
in []int
inDelay time.Duration
ctxTimeout time.Duration
}
type want struct {
out [][]int
open bool
}
for _, test := range []struct {
name string
args args
want want
}{{
name: "out closes when in closes",
args: args{
maxSize: 20,
maxDuration: maxTestDuration,
in: nil,
inDelay: 0,
ctxTimeout: maxTestDuration,
},
want: want{
out: nil,
open: false,
},
}, {
name: "out remains open if in remains open",
args: args{
maxSize: 2,
maxDuration: maxTestDuration,
in: []int{1, 2, 3},
inDelay: (maxTestDuration / 2) - (10 * time.Millisecond),
ctxTimeout: maxTestDuration,
},
want: want{
out: [][]int{{1, 2}},
open: true,
},
}, {
name: "collects maxSize inputs and returns",
args: args{
maxSize: 2,
maxDuration: maxTestDuration / 10 * 9,
inDelay: maxTestDuration / 10,
in: []int{1, 2, 3, 4, 5},
ctxTimeout: maxTestDuration / 10 * 9,
},
want: want{
out: [][]int{
{1, 2},
{3, 4},
{5},
},
open: false,
},
}, {
name: "collection returns after maxDuration with < maxSize",
args: args{
maxSize: 10,
maxDuration: maxTestDuration / 4,
inDelay: (maxTestDuration / 4) - (25 * time.Millisecond),
in: []int{1, 2, 3, 4, 5},
ctxTimeout: maxTestDuration / 4,
},
want: want{
out: [][]int{
{1},
{2},
{3},
{4},
},
open: true,
},
}, {
name: "collection flushes buffer when the context is canceled",
args: args{
maxSize: 10,
maxDuration: maxTestDuration,
inDelay: 0,
in: []int{1, 2, 3, 4, 5},
ctxTimeout: 0,
},
want: want{
out: [][]int{
{1, 2, 3, 4, 5},
},
open: false,
},
}} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// Create the in channel
in := make(chan int)
go func() {
defer close(in)
for _, i := range test.args.in {
time.Sleep(test.args.inDelay)
in <- i
}
}()
// Create the context
ctx, cancel := context.WithTimeout(context.Background(), test.args.ctxTimeout)
defer cancel()
// Collect responses
collect := Collect(ctx, test.args.maxSize, test.args.maxDuration, in)
timeout := time.After(maxTestDuration)
var outs [][]int
var isOpen bool
loop:
for {
select {
case out, open := <-collect:
if !open {
isOpen = false
break loop
}
isOpen = true
outs = append(outs, out)
case <-timeout:
break loop
}
}
// Expecting to close or stay open
if test.want.open != isOpen {
t.Errorf("open = %t, want %t", isOpen, test.want.open)
}
// Expecting outputs
if !reflect.DeepEqual(test.want.out, outs) {
t.Errorf("out = %v, want %v", outs, test.want.out)
}
})
}
}