forked from gjbae1212/fluent-bit-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoutput_pubsub_test.go
More file actions
552 lines (491 loc) · 13 KB
/
output_pubsub_test.go
File metadata and controls
552 lines (491 loc) · 13 KB
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
package main
import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
"sync"
"testing"
"time"
"unsafe"
"cloud.google.com/go/pubsub"
"github.com/fluent/fluent-bit-go/output"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
)
type testOutput struct {
inc int
}
// MockPublishResult implements pubsub.PublishResult for testing
type MockPublishResult struct {
delay time.Duration
err error
getCalls int
mutex sync.Mutex
concurrencyTracker *ConcurrencyTracker
}
// ConcurrencyTracker tracks concurrent executions
type ConcurrencyTracker struct {
activeCalls int32
maxActiveCalls int32
mutex sync.Mutex
}
func (m *MockPublishResult) Get(ctx context.Context) (string, error) {
m.mutex.Lock()
m.getCalls++
m.mutex.Unlock()
// Track concurrency if tracker is provided
if m.concurrencyTracker != nil {
m.concurrencyTracker.mutex.Lock()
m.concurrencyTracker.activeCalls++
if m.concurrencyTracker.activeCalls > m.concurrencyTracker.maxActiveCalls {
m.concurrencyTracker.maxActiveCalls = m.concurrencyTracker.activeCalls
}
m.concurrencyTracker.mutex.Unlock()
defer func() {
m.concurrencyTracker.mutex.Lock()
m.concurrencyTracker.activeCalls--
m.concurrencyTracker.mutex.Unlock()
}()
}
if m.delay > 0 {
time.Sleep(m.delay)
}
return "message-id", m.err
}
func (m *MockPublishResult) Ready() <-chan struct{} {
ch := make(chan struct{})
close(ch)
return ch
}
// MockOutputWrapper for testing configuration
type MockOutputWrapper struct {
configs map[string]string
}
func (m *MockOutputWrapper) Register(ctx unsafe.Pointer, name string, desc string) int {
return 0
}
func (m *MockOutputWrapper) GetConfigKey(ctx unsafe.Pointer, key string) string {
if m.configs == nil {
return ""
}
return m.configs[key]
}
func (m *MockOutputWrapper) NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder {
return nil
}
func (m *MockOutputWrapper) GetRecord(dec *output.FLBDecoder) (ret int, ts interface{}, rec map[interface{}]interface{}) {
return 1, nil, nil // Return non-zero to stop iteration
}
func (o *testOutput) Register(ctx unsafe.Pointer, name string, desc string) int {
return output.FLBPluginRegister(ctx, name, desc)
}
func (o *testOutput) GetConfigKey(ctx unsafe.Pointer, key string) string {
switch key {
case "Project":
return os.Getenv("PROJECT_ID")
case "Topic":
return os.Getenv("TOPIC_NAME")
case "Debug":
return "true"
case "Timeout":
return "10000"
case "ByteThreshold":
return "1000000"
case "CountThreshold":
return "100"
case "DelayThreshold":
return "100"
case "Format":
return "json"
case "ParallelConfirm":
return ""
case "ConfirmWorkers":
return ""
case "BufferedByteLimit":
return "1000000"
case "Region":
return os.Getenv("REGION")
default:
return ""
}
}
func (o *testOutput) NewDecoder(data unsafe.Pointer, length int) *output.FLBDecoder {
return nil
}
func (o *testOutput) GetRecord(dec *output.FLBDecoder) (ret int, ts interface{}, rec map[interface{}]interface{}) {
if o.inc == 0 {
o.inc++
return 0, output.FLBTime{Time: time.Now()}, map[interface{}]interface{}{
"testvalue1": []byte("record1"),
"testvalue2": []byte("record2"),
}
}
return -1, nil, nil
}
func TestFLBPluginInit(t *testing.T) {
assert := assert.New(t)
err := godotenv.Load()
if err != nil {
t.Log("Error loading .env file")
}
wrapper = OutputWrapper(&testOutput{})
if os.Getenv("PROJECT_ID") == "" || os.Getenv("TOPIC_NAME") == "" {
assert.Equal(output.FLB_ERROR, FLBPluginInit(nil))
} else {
assert.Equal(output.FLB_OK, FLBPluginInit(nil))
}
}
func TestFLBPluginFlush(t *testing.T) {
assert := assert.New(t)
err := godotenv.Load()
if err != nil {
t.Log("Error loading .env file")
}
wrapper = OutputWrapper(&testOutput{})
if os.Getenv("PROJECT_ID") == "" || os.Getenv("TOPIC_NAME") == "" {
return
}
ok := FLBPluginFlush(nil, 0, nil)
assert.Equal(output.FLB_OK, ok)
projectId := os.Getenv("PROJECT_ID")
topicName := os.Getenv("TOPIC_NAME")
if projectId == "" || topicName == "" {
return
}
keeper, err := NewKeeper(projectId, topicName, "", nil)
assert.NoError(err)
sub := keeper.(*GooglePubSub).client.Subscription(topicName)
go func() {
sub.Receive(context.Background(), func(ctx context.Context, m *pubsub.Message) {
log.Printf("Got message: %s", m.Data)
m.Ack()
})
}()
time.Sleep(5 * time.Second)
}
func TestInterfaceToBytes(t *testing.T) {
assert := assert.New(t)
now := time.Now()
tests := map[string]struct {
input interface{}
output []byte
}{
"float": {
input: float64(10.0),
output: []byte(fmt.Sprintf("%f", float64(10.0))),
},
"[]byte": {
input: []byte(string("hello")),
output: []byte(string("hello")),
},
"int": {
input: int(20),
output: []byte(string("20")),
},
"string": {
input: "hello",
output: []byte(string("hello")),
},
"time": {
input: now,
output: []byte(now.Format(time.RFC3339)),
},
"bool": {
input: true,
output: []byte("true"),
},
"etc": {
input: map[string]string{"hello": "world"},
output: []byte(fmt.Sprintf("%v", map[string]string{"hello": "world"})),
},
}
for _, t := range tests {
output := interfaceToBytes(t.input)
assert.Equal(t.output, output)
}
}
func TestParallelConfirmConfiguration(t *testing.T) {
assert := assert.New(t)
tests := []struct {
name string
parallelConfirmConfig string
confirmWorkersConfig string
expectedParallelConfirm bool
expectedConfirmWorkers int
expectError bool
}{
{
name: "Default values",
parallelConfirmConfig: "",
confirmWorkersConfig: "",
expectedParallelConfirm: false,
expectedConfirmWorkers: 10,
expectError: false,
},
{
name: "Enable parallel confirm",
parallelConfirmConfig: "true",
confirmWorkersConfig: "20",
expectedParallelConfirm: true,
expectedConfirmWorkers: 20,
expectError: false,
},
{
name: "Disable parallel confirm",
parallelConfirmConfig: "false",
confirmWorkersConfig: "50",
expectedParallelConfirm: false,
expectedConfirmWorkers: 50,
expectError: false,
},
{
name: "Workers out of range (too high)",
parallelConfirmConfig: "true",
confirmWorkersConfig: "150",
expectedParallelConfirm: true,
expectedConfirmWorkers: 10, // Should remain default
expectError: false,
},
{
name: "Workers out of range (zero)",
parallelConfirmConfig: "true",
confirmWorkersConfig: "0",
expectedParallelConfirm: true,
expectedConfirmWorkers: 10, // Should remain default
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset global variables
parallelConfirm = false
confirmWorkers = 10
// Setup mock wrapper
mockWrapper := &MockOutputWrapper{
configs: map[string]string{
"ParallelConfirm": tt.parallelConfirmConfig,
"ConfirmWorkers": tt.confirmWorkersConfig,
},
}
// Save original wrapper and restore after test
originalWrapper := wrapper
wrapper = mockWrapper
defer func() { wrapper = originalWrapper }()
// Test the configuration parsing logic (from FLBPluginInit)
var err error
// Parse ParallelConfirm
pc := mockWrapper.GetConfigKey(nil, "ParallelConfirm")
if pc != "" {
parallelConfirm, err = strconv.ParseBool(pc)
if err != nil && !tt.expectError {
t.Fatalf("Expected no error, got: %v", err)
}
}
// Parse ConfirmWorkers
cw := mockWrapper.GetConfigKey(nil, "ConfirmWorkers")
if cw != "" {
v, err := strconv.Atoi(cw)
if err != nil && !tt.expectError {
t.Fatalf("Expected no error, got: %v", err)
}
if err == nil && v > 0 && v <= 100 {
confirmWorkers = v
}
}
// Validate results
assert.Equal(tt.expectedParallelConfirm, parallelConfirm,
"parallelConfirm should match expected value")
assert.Equal(tt.expectedConfirmWorkers, confirmWorkers,
"confirmWorkers should match expected value")
})
}
}
func TestParallelConfirmPerformance(t *testing.T) {
// Create mock results with delays
numResults := 50
results := make([]*MockPublishResult, numResults)
for i := 0; i < numResults; i++ {
results[i] = &MockPublishResult{
delay: 10 * time.Millisecond, // Simulate network delay
err: nil,
}
}
// Test sequential processing
sequentialStart := time.Now()
for _, result := range results {
result.Get(context.Background())
}
sequentialDuration := time.Since(sequentialStart)
// Reset results for parallel test
for i := 0; i < numResults; i++ {
results[i] = &MockPublishResult{
delay: 10 * time.Millisecond,
err: nil,
}
}
// Test parallel processing
parallelStart := time.Now()
var wg sync.WaitGroup
var mutex sync.Mutex
errors := []error{}
batchSize := 10
for i := 0; i < len(results); i += batchSize {
end := i + batchSize
if end > len(results) {
end = len(results)
}
for j := i; j < end; j++ {
wg.Add(1)
go func(result *MockPublishResult) {
defer wg.Done()
if _, err := result.Get(context.Background()); err != nil {
mutex.Lock()
errors = append(errors, err)
mutex.Unlock()
}
}(results[j])
}
wg.Wait()
}
parallelDuration := time.Since(parallelStart)
// Parallel should be significantly faster
speedup := float64(sequentialDuration) / float64(parallelDuration)
assert.True(t, speedup >= 2.0,
fmt.Sprintf("Expected at least 2x speedup, got %.2fx (sequential: %v, parallel: %v)",
speedup, sequentialDuration, parallelDuration))
t.Logf("Sequential: %v, Parallel: %v, Speedup: %.2fx",
sequentialDuration, parallelDuration, speedup)
}
func TestParallelConfirmErrorHandling(t *testing.T) {
assert := assert.New(t)
tests := []struct {
name string
errors []error
expectedRetry bool
expectedErrors int
}{
{
name: "No errors",
errors: []error{},
expectedRetry: false,
expectedErrors: 0,
},
{
name: "Retry errors",
errors: []error{
context.DeadlineExceeded,
context.Canceled,
},
expectedRetry: true,
expectedErrors: 2,
},
{
name: "Non-retry errors",
errors: []error{
errors.New("permanent error"),
errors.New("another error"),
},
expectedRetry: false,
expectedErrors: 2,
},
{
name: "Mixed errors",
errors: []error{
context.DeadlineExceeded,
errors.New("permanent error"),
},
expectedRetry: true,
expectedErrors: 2,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create mock results with specified errors
results := make([]*MockPublishResult, len(tt.errors))
for i, err := range tt.errors {
results[i] = &MockPublishResult{
delay: 1 * time.Millisecond,
err: err,
}
}
// Add some successful results
for i := 0; i < 5; i++ {
results = append(results, &MockPublishResult{
delay: 1 * time.Millisecond,
err: nil,
})
}
// Test parallel confirmation logic (mirrors the actual implementation)
var wg sync.WaitGroup
var hasRetryError bool
var mutex sync.Mutex
collectedErrors := []error{}
batchSize := 3
for i := 0; i < len(results); i += batchSize {
end := i + batchSize
if end > len(results) {
end = len(results)
}
for j := i; j < end; j++ {
wg.Add(1)
go func(result *MockPublishResult) {
defer wg.Done()
if _, err := result.Get(context.Background()); err != nil {
mutex.Lock()
collectedErrors = append(collectedErrors, err)
if err == context.DeadlineExceeded || err == context.Canceled {
hasRetryError = true
}
mutex.Unlock()
}
}(results[j])
}
wg.Wait()
}
// Validate results
assert.Equal(tt.expectedRetry, hasRetryError,
"hasRetryError should match expected value")
assert.Equal(tt.expectedErrors, len(collectedErrors),
"number of collected errors should match expected")
})
}
}
func TestConcurrencyLimits(t *testing.T) {
assert := assert.New(t)
numResults := 30
maxConcurrency := 5
// Create concurrency tracker
tracker := &ConcurrencyTracker{}
results := make([]*MockPublishResult, numResults)
for i := 0; i < numResults; i++ {
results[i] = &MockPublishResult{
delay: 50 * time.Millisecond,
err: nil,
concurrencyTracker: tracker,
}
}
// Test with concurrency limit
var wg sync.WaitGroup
batchSize := maxConcurrency
for i := 0; i < len(results); i += batchSize {
end := i + batchSize
if end > len(results) {
end = len(results)
}
for j := i; j < end; j++ {
wg.Add(1)
go func(result *MockPublishResult) {
defer wg.Done()
result.Get(context.Background())
}(results[j])
}
wg.Wait()
}
// Validate concurrency was limited
assert.True(tracker.maxActiveCalls <= int32(maxConcurrency),
fmt.Sprintf("Expected max concurrency %d, got %d", maxConcurrency, tracker.maxActiveCalls))
t.Logf("Max concurrent calls: %d (limit: %d)", tracker.maxActiveCalls, maxConcurrency)
}