-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_test.go
274 lines (237 loc) · 6.52 KB
/
layer_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package sinoname
import (
"context"
"errors"
"fmt"
"sync/atomic"
"testing"
"time"
"go.uber.org/goleak"
"golang.org/x/sync/errgroup"
)
type timeoutTransformer struct {
add string
d time.Duration
}
func (t timeoutTransformer) Transform(ctx context.Context, in MessagePacket) (MessagePacket, error) {
select {
case <-ctx.Done():
return MessagePacket{}, ctx.Err()
case <-time.After(t.d):
in.setAndIncrement(in.Message + t.add)
return in, nil
}
}
func newTimeoutTransformer(add string, d time.Duration) TransformerFactory {
return func(cfg *Config) (Transformer, bool) {
return timeoutTransformer{add, d}, false
}
}
type skipTransformer struct {
add string
skip int
}
func (t skipTransformer) Transform(ctx context.Context, in MessagePacket) (MessagePacket, error) {
in.Skip = t.skip
in.setAndIncrement(in.Message + t.add)
return in, nil
}
func newSkipTransformer(add string, skip int) TransformerFactory {
return func(cfg *Config) (Transformer, bool) {
return skipTransformer{add, skip}, false
}
}
type errTransformer struct {
err error
}
func (t errTransformer) Transform(context.Context, MessagePacket) (MessagePacket, error) {
return MessagePacket{}, t.err
}
func newErrorTransformer(err error) TransformerFactory {
return func(cfg *Config) (Transformer, bool) {
return errTransformer{err}, false
}
}
type statefullTransformer struct {
state int32
}
func (t *statefullTransformer) Transform(ctx context.Context, in MessagePacket) (MessagePacket, error) {
state := atomic.AddInt32(&t.state, 1)
in.setAndIncrement(fmt.Sprintf("%v%d", in, state))
return in, nil
}
func newStatefullTransformer() TransformerFactory {
return func(cfg *Config) (Transformer, bool) {
return &statefullTransformer{}, true
}
}
type addTransformer struct {
add string
}
func (t addTransformer) Transform(ctx context.Context, in MessagePacket) (MessagePacket, error) {
in.setAndIncrement(in.Message + t.add)
return in, nil
}
func newAddTransformer(add string) TransformerFactory {
return func(cfg *Config) (Transformer, bool) {
return addTransformer{add: add}, false
}
}
func TestLayerCloseProducerChannel(t *testing.T) {
defer goleak.VerifyNone(t)
t.Run("Without_Values", func(t *testing.T) {
layers := []Layer{
newUniformLayer(Noop),
newTransformerLayer(Noop),
}
for _, layer := range layers {
ch := make(chan MessagePacket)
close(ch)
testLayerChanClose(t, context.Background(), layer, ch, 0, 1*time.Second, 1*time.Second)
}
})
t.Run("With_Values", func(t *testing.T) {
layers := []Layer{
newUniformLayer(
newTimeoutTransformer("1", 1*time.Second),
newTimeoutTransformer("2", 2*time.Second),
),
newTransformerLayer(
newTimeoutTransformer("1", 1*time.Second),
newTimeoutTransformer("2", 2*time.Second),
),
}
for _, layer := range layers {
ch := make(chan MessagePacket, 1)
ch <- MessagePacket{}
close(ch)
testLayerChanClose(t, context.Background(), layer, ch, 2, 3*time.Second, 1*time.Second)
}
})
}
func TestLayerContextCancel(t *testing.T) {
defer goleak.VerifyNone(t)
t.Run("Manual_Close", func(t *testing.T) {
for _, v := range []struct {
l Layer
n int
timeout, deadline time.Duration
}{
{
l: newUniformLayer(newTimeoutTransformer("1", 1*time.Microsecond), newTimeoutTransformer("2", 10*time.Second)),
n: 0,
timeout: 0,
deadline: 3 * time.Second,
},
{
l: newTransformerLayer(newTimeoutTransformer("1", 1*time.Second), newTimeoutTransformer("2", 10*time.Second)),
n: 1,
timeout: 3 * time.Second,
deadline: 2 * time.Second,
},
} {
ch := make(chan MessagePacket, 1)
ch <- MessagePacket{}
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
testLayerChanClose(t, ctx, v.l, ch, v.n, v.timeout, v.deadline)
}
})
t.Run("Error_Close", func(t *testing.T) {
layers := []Layer{
newUniformLayer(
newErrorTransformer(errors.New("test error")),
newTimeoutTransformer("1", 10*time.Second),
),
newTransformerLayer(
newErrorTransformer(errors.New("test error")),
newTimeoutTransformer("1", 10*time.Second),
),
}
for _, layer := range layers {
ch := make(chan MessagePacket, 1)
ch <- MessagePacket{}
testLayerChanClose(t, context.Background(), layer, ch, 0, 0, 1*time.Second)
}
})
}
func TestLayerSkip(t *testing.T) {
defer goleak.VerifyNone(t)
t.Run("Handle_Skip_Layers", func(t *testing.T) {
layers := []Layers{
[]Layer{
newTransformerLayer(newSkipTransformer("1", 2)),
newTransformerLayer(newAddTransformer("2")),
newTransformerLayer(newAddTransformer("2")),
newTransformerLayer(newAddTransformer("3")),
},
[]Layer{
newUniformLayer(newSkipTransformer("1", 2)),
newUniformLayer(newAddTransformer("2")),
newUniformLayer(newAddTransformer("2")),
newUniformLayer(newAddTransformer("3")),
},
}
for _, l := range layers {
recv, _, err := l.Run(context.Background(), MessagePacket{})
if err != nil {
t.Fatal(err)
}
v := <-recv
if v.Message[len(v.Message)-1] != '3' || v.Message[len(v.Message)-2] == '2' {
t.Fatal("didnt expect packet:", v)
}
}
})
t.Run("Handle_Skip_All_Layers", func(t *testing.T) {
layers := []Layers{
[]Layer{
newTransformerLayer(newSkipTransformer("1", 3)),
newTransformerLayer(newAddTransformer("2")),
newTransformerLayer(newAddTransformer("2")),
newTransformerLayer(newAddTransformer("2")),
},
[]Layer{
newUniformLayer(newSkipTransformer("1", 3)),
newUniformLayer(newAddTransformer("2")),
newUniformLayer(newAddTransformer("2")),
newUniformLayer(newAddTransformer("2")),
},
}
for _, l := range layers {
recv, _, err := l.Run(context.Background(), MessagePacket{})
if err != nil {
t.Fatal(err)
}
v := <-recv
if len(v.Message) != 1 || v.Message[0] != '1' {
t.Fatal("didnt expect packet:", v)
}
}
})
}
func testLayerChanClose(t *testing.T, ctx context.Context, l Layer, src chan MessagePacket, n int, timeout, deadline time.Duration) {
g, ctx := errgroup.WithContext(ctx)
sink, err := l.PumpOut(ctx, g, src)
if err != nil {
t.Fatal(err)
}
for i := 0; i < n; i++ {
select {
case _, ok := <-sink:
if !ok {
t.Fatal("didnt expect sink to close yet")
}
// val recieced from sink.
case <-time.After(timeout):
t.Fatal("values should be available before timeout")
}
}
select {
case _, ok := <-sink:
if ok {
t.Fatal("recieved unexpected non close message")
}
case <-time.After(deadline):
t.Fatal("expected channel to be closed", n)
}
}