forked from lovoo/goka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_integration_test.go
388 lines (333 loc) · 8.59 KB
/
processor_integration_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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package goka_test
import (
"context"
"errors"
"fmt"
"log"
"strings"
"testing"
"time"
"github.com/facebookgo/ensure"
"github.com/lovoo/goka"
"github.com/lovoo/goka/codec"
"github.com/lovoo/goka/kafka"
"github.com/lovoo/goka/mock"
"github.com/lovoo/goka/storage"
"github.com/lovoo/goka/tester"
)
func doTimed(t *testing.T, do func()) error {
ch := make(chan bool)
go func() {
do()
close(ch)
}()
select {
case <-time.After(2 * time.Second):
t.Fail()
return errors.New("function took too long to complete")
case <-ch:
}
return nil
}
func TestProcessor_StatelessContext(t *testing.T) {
ctrl := mock.NewMockController(t)
defer ctrl.Finish()
var (
tester = tester.New(t)
)
callPersist := func(ctx goka.Context, message interface{}) {
log.Println("processing")
// call a random setvalue, this is expected to fail
ctx.SetValue("value")
t.Errorf("SetValue should panic. We should not have come to that point.")
}
proc, err := goka.NewProcessor(nil,
goka.DefineGroup(
"stateless-ctx",
goka.Input("input-topic", new(codec.Bytes), callPersist),
),
goka.WithTester(tester),
)
ensure.Nil(t, err)
done := make(chan bool)
go func() {
err = proc.Run(context.Background())
ensure.NotNil(t, err)
close(done)
}()
err = doTimed(t, func() {
// consume a random key/message, the content doesn't matter as this should fail
tester.Consume("input-topic", "key", []byte("msg"))
<-done
})
ensure.Nil(t, err)
}
func TestProcessor_ProducerError(t *testing.T) {
t.Run("SetValue", func(t *testing.T) {
tester := tester.New(t)
tester.ReplaceEmitHandler(func(topic, key string, value []byte) *kafka.Promise {
return kafka.NewPromise().Finish(errors.New("producer error"))
})
consume := func(ctx goka.Context, msg interface{}) {
ctx.SetValue(msg)
}
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", new(codec.String), consume),
goka.Persist(new(codec.String)),
),
goka.WithTester(tester),
)
ensure.Nil(t, err)
var (
procErrors error
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
go func() {
procErrors = proc.Run(ctx)
close(done)
}()
tester.Consume("topic", "key", "world")
cancel()
<-done
ensure.NotNil(t, procErrors)
})
t.Run("Emit", func(t *testing.T) {
tester := tester.New(t)
tester.ReplaceEmitHandler(func(topic, key string, value []byte) *kafka.Promise {
return kafka.NewPromise().Finish(errors.New("producer error"))
})
consume := func(ctx goka.Context, msg interface{}) {
ctx.Emit("blubbb", "key", []byte("some message is emitted"))
}
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", new(codec.String), consume),
goka.Persist(new(codec.String)),
),
goka.WithTester(tester),
)
ensure.Nil(t, err)
var (
processorErrors error
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
go func() {
processorErrors = proc.Run(ctx)
close(done)
}()
tester.Consume("topic", "key", "world")
cancel()
<-done
ensure.True(t, processorErrors != nil)
})
t.Run("Value-stateless", func(t *testing.T) {
tester := tester.New(t)
tester.ReplaceEmitHandler(func(topic, key string, value []byte) *kafka.Promise {
return kafka.NewPromise().Finish(errors.New("producer error"))
})
consume := func(ctx goka.Context, msg interface{}) {
func() {
defer goka.PanicStringContains(t, "stateless")
_ = ctx.Value()
}()
}
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", new(codec.String), consume),
),
goka.WithTester(tester),
)
ensure.Nil(t, err)
var (
processorErrors error
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
go func() {
processorErrors = proc.Run(ctx)
close(done)
}()
tester.Consume("topic", "key", "world")
// stopping the processor. It should actually not produce results
cancel()
<-done
ensure.Nil(t, processorErrors)
})
}
func TestProcessor_consumeFail(t *testing.T) {
tester := tester.New(t)
consume := func(ctx goka.Context, msg interface{}) {
ctx.Fail(errors.New("consume-failed"))
}
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", new(codec.String), consume),
),
goka.WithTester(tester),
)
ensure.Nil(t, err)
var (
processorErrors error
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
go func() {
processorErrors = proc.Run(ctx)
close(done)
}()
tester.Consume("topic", "key", "world")
cancel()
<-done
ensure.True(t, strings.Contains(processorErrors.Error(), "consume-failed"))
}
func TestProcessor_consumePanic(t *testing.T) {
tester := tester.New(t)
consume := func(ctx goka.Context, msg interface{}) {
panic("panicking")
}
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", new(codec.String), consume),
),
goka.WithTester(tester),
)
ensure.Nil(t, err)
var (
processorErrors error
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
go func() {
processorErrors = proc.Run(ctx)
close(done)
}()
tester.Consume("topic", "key", "world")
cancel()
<-done
ensure.NotNil(t, processorErrors)
ensure.True(t, strings.Contains(processorErrors.Error(), "panicking"))
}
type nilValue struct{}
type nilCodec struct{}
func (nc *nilCodec) Decode(data []byte) (interface{}, error) {
if data == nil {
return new(nilValue), nil
}
return data, nil
}
func (nc *nilCodec) Encode(val interface{}) ([]byte, error) {
return nil, nil
}
func TestProcessor_consumeNil(t *testing.T) {
tests := []struct {
name string
cb goka.ProcessCallback
handling goka.NilHandling
codec goka.Codec
}{
{
"ignore",
func(ctx goka.Context, msg interface{}) {
t.Error("should never call consume")
t.Fail()
},
goka.NilIgnore,
new(codec.String),
},
{
"process",
func(ctx goka.Context, msg interface{}) {
if msg != nil {
t.Errorf("message should be nil:%v", msg)
t.Fail()
}
},
goka.NilProcess,
new(codec.String),
},
{
"decode",
func(ctx goka.Context, msg interface{}) {
if _, ok := msg.(*nilValue); !ok {
t.Errorf("message should be a decoded nil value: %T", msg)
t.Fail()
}
},
goka.NilDecode,
new(nilCodec),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tester := tester.New(t)
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", tc.codec, tc.cb),
),
goka.WithTester(tester),
goka.WithNilHandling(tc.handling),
)
ensure.Nil(t, err)
var (
processorErrors error
done = make(chan struct{})
ctx, cancel = context.WithCancel(context.Background())
)
go func() {
processorErrors = proc.Run(ctx)
close(done)
}()
tester.Consume("topic", "key", nil)
cancel()
<-done
ensure.Nil(t, processorErrors)
})
}
}
// tests shutting down the processor during recovery
func TestProcessor_failOnRecover(t *testing.T) {
var (
recovered int
processorErrors error
_ = processorErrors // make linter happy
done = make(chan struct{})
msgToRecover = 100
)
tester := tester.New(t)
consume := func(ctx goka.Context, msg interface{}) {
log.Println("consuming message..", ctx.Key())
}
proc, err := goka.NewProcessor([]string{"broker"},
goka.DefineGroup("test",
goka.Input("topic", new(codec.String), consume),
goka.Persist(new(codec.Bytes)),
),
goka.WithTester(tester),
goka.WithUpdateCallback(func(s storage.Storage, partition int32, key string, value []byte) error {
log.Printf("recovered state: %s: %s", key, string(value))
recovered++
time.Sleep(1 * time.Millisecond)
return nil
}),
)
ensure.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
go func() {
processorErrors = proc.Run(ctx)
close(done)
}()
for i := 0; i < msgToRecover; i++ {
tester.Consume("test-table", "key", []byte(fmt.Sprintf("state-%d", i)))
}
// let's wait until half of them are roughly recovered
time.Sleep(50 * time.Millisecond)
// stop the processor and wait for it
cancel()
<-done
// make sure the recovery was aborted, so we have recovered something but not all
// we can't test that anymore since there is no recovery-functionality in the tester implemented
//ensure.True(t, recovered > 0 && recovered < msgToRecover)
}