-
Notifications
You must be signed in to change notification settings - Fork 2
/
cshared_test.go
615 lines (510 loc) · 12.8 KB
/
cshared_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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package plugin
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
"unsafe"
"github.com/alecthomas/assert/v2"
"github.com/vmihailenco/msgpack/v5"
"github.com/calyptia/plugin/metric"
)
type testPluginInputCallbackCtrlC struct{}
func (t testPluginInputCallbackCtrlC) Init(ctx context.Context, fbit *Fluentbit) error {
return nil
}
func (t testPluginInputCallbackCtrlC) Collect(ctx context.Context, ch chan<- Message) error {
return nil
}
func init() {
registerWG.Done()
}
func TestMain(m *testing.M) {
defer flbPluginReset()
m.Run()
}
func TestInputCallbackCtrlC(t *testing.T) {
theInputLock.Lock()
theInput = testPluginInputCallbackCtrlC{}
theInputLock.Unlock()
cdone := make(chan bool)
timeout := time.NewTimer(1 * time.Second)
defer timeout.Stop()
ptr := unsafe.Pointer(nil)
// prepare channel for input explicitly.
prepareInputCollector(false)
go func() {
FLBPluginInputCallback(&ptr, nil)
cdone <- true
}()
select {
case <-cdone:
timeout.Stop()
runCancel()
case <-timeout.C:
t.Fatalf("timed out ...")
}
}
var testPluginInputCallbackDangleFuncs atomic.Int64
type testPluginInputCallbackDangle struct{}
func (t testPluginInputCallbackDangle) Init(ctx context.Context, fbit *Fluentbit) error {
return nil
}
func (t testPluginInputCallbackDangle) Collect(ctx context.Context, ch chan<- Message) error {
testPluginInputCallbackDangleFuncs.Add(1)
ch <- Message{
Time: time.Now(),
Record: map[string]string{
"Foo": "BAR",
},
}
return nil
}
// TestInputCallbackDangle assures the API will not attempt to invoke
// Collect multiple times. This is inline with backward-compatible
// behavior.
func TestInputCallbackDangle(t *testing.T) {
theInputLock.Lock()
theInput = testPluginInputCallbackDangle{}
theInputLock.Unlock()
cdone := make(chan bool)
ptr := unsafe.Pointer(nil)
// prepare channel for input explicitly.
prepareInputCollector(false)
go func() {
t := time.NewTicker(collectInterval)
defer t.Stop()
FLBPluginInputCallback(&ptr, nil)
for {
select {
case <-t.C:
FLBPluginInputCallback(&ptr, nil)
case <-cdone:
return
}
}
}()
timeout := time.NewTimer(5 * time.Second)
<-timeout.C
timeout.Stop()
runCancel()
cdone <- true
// Test the assumption that only a single goroutine is
// ingesting records.
if testPluginInputCallbackDangleFuncs.Load() != 1 {
t.Fatalf("Too many callbacks: %d",
testPluginInputCallbackDangleFuncs.Load())
}
}
var testPluginInputCallbackInfiniteFuncs atomic.Int64
type testPluginInputCallbackInfinite struct{}
func (t testPluginInputCallbackInfinite) Init(ctx context.Context, fbit *Fluentbit) error {
return nil
}
func (t testPluginInputCallbackInfinite) Collect(ctx context.Context, ch chan<- Message) error {
testPluginInputCallbackInfiniteFuncs.Add(1)
for {
select {
default:
ch <- Message{
Time: time.Now(),
Record: map[string]string{
"Foo": "BAR",
},
}
// for tests to correctly pass our infinite loop needs
// to return once the context has been finished.
case <-ctx.Done():
return nil
}
}
}
// TestInputCallbackInfinite is a test for the main method most plugins
// use where they do not return from the first invocation of collect.
func TestInputCallbackInfinite(t *testing.T) {
theInputLock.Lock()
theInput = testPluginInputCallbackInfinite{}
theInputLock.Unlock()
cdone := make(chan bool)
cshutdown := make(chan bool)
ptr := unsafe.Pointer(nil)
// prepare channel for input explicitly.
prepareInputCollector(false)
go func() {
t := time.NewTicker(collectInterval)
defer t.Stop()
for {
select {
case <-t.C:
FLBPluginInputCallback(&ptr, nil)
if ptr != nil {
cdone <- true
return
}
case <-cshutdown:
return
}
}
}()
timeout := time.NewTimer(10 * time.Second)
defer timeout.Stop()
select {
case <-cdone:
runCancel()
// make sure Collect is not being invoked after Done().
time.Sleep(collectInterval * 10)
// Test the assumption that only a single goroutine is
// ingesting records.
if testPluginInputCallbackInfiniteFuncs.Load() != 1 {
t.Fatalf("Too many callbacks: %d",
testPluginInputCallbackInfiniteFuncs.Load())
}
return
case <-timeout.C:
runCancel()
cshutdown <- true
// This test seems to fail some what frequently because the Collect goroutine
// inside cshared is never being scheduled.
t.Fatalf("timed out ...")
}
}
type testPluginInputCallbackLatency struct{}
func (t testPluginInputCallbackLatency) Init(ctx context.Context, fbit *Fluentbit) error {
return nil
}
func (t testPluginInputCallbackLatency) Collect(ctx context.Context, ch chan<- Message) error {
tick := time.NewTimer(time.Second * 1)
for {
select {
case <-tick.C:
for i := 0; i < 128; i++ {
ch <- Message{
Time: time.Now(),
Record: map[string]string{
"Foo": "BAR",
},
}
}
tick.Reset(time.Second * 1)
case <-ctx.Done():
return nil
}
}
}
// TestInputCallbackInfiniteLatency is a test of the latency between
// messages.
func TestInputCallbackLatency(t *testing.T) {
theInputLock.Lock()
theInput = testPluginInputCallbackLatency{}
theInputLock.Unlock()
cdone := make(chan bool)
cstarted := make(chan bool)
cmsg := make(chan []byte)
// prepare channel for input explicitly.
prepareInputCollector(false)
go func() {
t := time.NewTicker(collectInterval)
defer t.Stop()
buf, _ := testFLBPluginInputCallback()
if len(buf) > 0 {
cmsg <- buf
}
cstarted <- true
for {
select {
case <-cdone:
fmt.Println("---- collect done")
return
case <-t.C:
buf, _ := testFLBPluginInputCallback()
if len(buf) > 0 {
cmsg <- buf
}
}
}
}()
<-cstarted
fmt.Println("---- started")
timeout := time.NewTimer(5 * time.Second)
defer timeout.Stop()
msgs := 0
for {
select {
case buf := <-cmsg:
dec := msgpack.NewDecoder(bytes.NewReader(buf))
for {
msg, err := decodeMsg(dec, "test-tag")
if errors.Is(err, io.EOF) {
break
}
if err != nil {
t.Fatalf("decode error: %v", err)
}
msgs++
if time.Since(msg.Time) > time.Millisecond*5 {
t.Errorf("latency too high: %fms",
float64(time.Since(msg.Time)/time.Millisecond))
}
}
case <-timeout.C:
runCancel()
cdone <- true
if msgs < 128 {
t.Fatalf("too few messages: %d", msgs)
}
return
}
}
}
type testInputCallbackInfiniteConcurrent struct{}
var (
concurrentWait sync.WaitGroup
concurrentCountStart atomic.Int64
concurrentCountFinish atomic.Int64
)
func (t testInputCallbackInfiniteConcurrent) Init(ctx context.Context, fbit *Fluentbit) error {
return nil
}
func (t testInputCallbackInfiniteConcurrent) Collect(ctx context.Context, ch chan<- Message) error {
fmt.Printf("---- infinite concurrent collect\n")
for i := 0; i < 64; i++ {
go func(ch chan<- Message, id int) {
fmt.Printf("---- infinite concurrent started: %d\n", id)
concurrentCountStart.Add(1)
ch <- Message{
Time: time.Now(),
Record: map[string]string{
"ID": fmt.Sprintf("%d", id),
},
}
concurrentCountFinish.Add(1)
concurrentWait.Done()
fmt.Printf("---- infinite concurrent finished: %d\n", id)
}(ch, i)
fmt.Printf("---- infinite concurrent starting: %d\n", i)
}
// for tests to correctly pass our infinite loop needs
// to return once the context has been finished.
<-ctx.Done()
return nil
}
// TestInputCallbackInfiniteConcurrent is meant to make sure we do not
// break anythin with respect to concurrent ingest.
func TestInputCallbackInfiniteConcurrent(t *testing.T) {
theInputLock.Lock()
theInput = testInputCallbackInfiniteConcurrent{}
theInputLock.Unlock()
cdone := make(chan bool)
cstarted := make(chan bool)
ptr := unsafe.Pointer(nil)
concurrentWait.Add(64)
// prepare channel for input explicitly.
prepareInputCollector(false)
go func(cstarted chan bool) {
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
FLBPluginInputCallback(&ptr, nil)
cstarted <- true
for {
select {
case <-ticker.C:
FLBPluginInputCallback(&ptr, nil)
case <-runCtx.Done():
return
}
}
}(cstarted)
go func() {
concurrentWait.Wait()
cdone <- true
}()
<-cstarted
timeout := time.NewTimer(10 * time.Second)
select {
case <-cdone:
runCancel()
case <-timeout.C:
runCancel()
// this test seems to timeout semi-frequently... need to get to
// the bottom of it...
t.Fatalf("---- timed out: %d/%d ...",
concurrentCountStart.Load(),
concurrentCountFinish.Load())
}
}
type testOutputHandlerReflect struct {
param string
flushCounter metric.Counter
log Logger
Test *testing.T
Check func(t *testing.T, msg Message) error
}
func (plug *testOutputHandlerReflect) Init(ctx context.Context, fbit *Fluentbit) error {
plug.flushCounter = fbit.Metrics.NewCounter("flush_total", "Total number of flushes", "gstdout")
plug.param = fbit.Conf.String("param")
plug.log = fbit.Logger
return nil
}
func (plug *testOutputHandlerReflect) Flush(ctx context.Context, ch <-chan Message) error {
plug.Test.Helper()
count := 0
for {
select {
case msg := <-ch:
rec := reflect.ValueOf(msg.Record)
if rec.Kind() != reflect.Map {
return fmt.Errorf("incorrect record type in flush")
}
if plug.Check != nil {
if err := plug.Check(plug.Test, msg); err != nil {
return err
}
}
count++
case <-ctx.Done():
if count <= 0 {
return fmt.Errorf("no records flushed")
}
return nil
}
}
}
type testOutputHandlerMapString struct {
param string
flushCounter metric.Counter
log Logger
}
func (plug *testOutputHandlerMapString) Init(ctx context.Context, fbit *Fluentbit) error {
plug.flushCounter = fbit.Metrics.NewCounter("flush_total", "Total number of flushes", "gstdout")
plug.param = fbit.Conf.String("param")
plug.log = fbit.Logger
return nil
}
func (plug *testOutputHandlerMapString) Flush(ctx context.Context, ch <-chan Message) error {
count := 0
for {
select {
case msg := <-ch:
record, ok := msg.Record.(map[string]interface{})
if !ok {
return fmt.Errorf("unable to convert record to map[string]")
}
for _, value := range record {
_, ok := value.(string)
if !ok {
return fmt.Errorf("unable to convert value")
}
}
count++
case <-ctx.Done():
if count <= 0 {
return fmt.Errorf("no records flushed")
}
return nil
}
}
}
// TestOutput is a simple output test. It also shows which format of records
// we currently support and how they should be handled. Feel free to use this
// code as an example of how to implement the Flush receive for output plugins.
//
// At the moment all Message.Records will be sent as a `map[string]interface{}`.
// Older plugins will have to do as testOutputHandlerMapString.Flush does
// and cast the actual value as a string.
func TestOutputSimulated(t *testing.T) {
var wg sync.WaitGroup
ctxt, cancel := context.WithCancel(context.Background())
ch := make(chan Message)
tag := "tag"
outputReflect := testOutputHandlerReflect{Test: t}
wg.Add(1)
go func(ctxt context.Context, wg *sync.WaitGroup, ch <-chan Message) {
err := outputReflect.Flush(ctxt, ch)
if err != nil {
t.Error(err)
}
wg.Done()
}(ctxt, &wg, ch)
ch <- Message{
Time: time.Now(),
Record: map[string]interface{}{
"foo": "bar",
"bar": "1",
},
tag: &tag,
}
cancel()
wg.Wait()
wg = sync.WaitGroup{}
ctxt, cancel = context.WithCancel(context.Background())
outputMapString := testOutputHandlerMapString{}
wg.Add(1)
go func(ctxt context.Context, wg *sync.WaitGroup, ch <-chan Message) {
err := outputMapString.Flush(ctxt, ch)
if err != nil {
t.Error(err)
t.Fail()
}
wg.Done()
}(ctxt, &wg, ch)
ch <- Message{
Time: time.Now(),
Record: map[string]interface{}{
"foo": "bar",
"foobar": "1",
},
tag: &tag,
}
cancel()
wg.Wait()
close(ch)
}
func TestOutputFlush(t *testing.T) {
var wg sync.WaitGroup
now := time.Now().UTC()
out := testOutputHandlerReflect{
Test: t,
Check: func(t *testing.T, msg Message) error {
defer wg.Done()
assert.Equal(t, now, msg.Time)
record := assertType[map[string]any](t, msg.Record)
foo := assertType[string](t, record["foo"])
assert.Equal(t, "bar", foo)
bar := assertType[int8](t, record["bar"])
assert.Equal(t, 3, bar)
foobar := assertType[float64](t, record["foobar"])
assert.Equal(t, 1.337, foobar)
return nil
},
}
_ = prepareOutputFlush(&out)
msg := Message{
Time: now,
Record: map[string]any{
"foo": "bar",
"bar": 3,
"foobar": 1.337,
},
}
b, err := msgpack.Marshal([]any{
&EventTime{msg.Time},
msg.Record,
})
assert.NoError(t, err)
wg.Add(1)
assert.NoError(t, pluginFlush("foobar", b))
wg.Wait()
}
func assertType[T any](tb testing.TB, got any) T {
tb.Helper()
var want T
v, ok := got.(T)
assert.True(tb, ok, "Expected types to be equal:\n-%T\n+%T", want, got)
return v
}