/
aggregator_test.go
97 lines (78 loc) · 1.89 KB
/
aggregator_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
package channelx
import (
"errors"
"sync"
"testing"
"time"
)
func TestAggregator_Basic(t *testing.T) {
batchProcess := func(items []interface{}) error {
t.Logf("handler %d items", len(items))
return nil
}
aggregator := NewAggregator(batchProcess)
aggregator.Start()
for i := 0; i < 1000; i++ {
aggregator.TryEnqueue(i)
}
aggregator.SafeStop()
}
func TestAggregator_Complex(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(100)
batchProcess := func(items []interface{}) error {
defer wg.Add(-len(items))
time.Sleep(20 * time.Millisecond)
if len(items) != 4 {
return errors.New("len(items) != 4")
}
return nil
}
errorHandler := func(err error, items []interface{}, batchProcessFunc BatchProcessFunc, aggregator *Aggregator) {
if err == nil {
t.FailNow()
}
t.Logf("Receive error, item size is %d", len(items))
}
aggregator := NewAggregator(batchProcess, func(option AggregatorOption) AggregatorOption {
option.BatchSize = 4
option.Workers = 2
option.LingerTime = 8 * time.Millisecond
option.Logger = NewConsoleLogger()
option.ErrorHandler = errorHandler
return option
})
aggregator.Start()
for i := 0; i < 100; i++ {
for !aggregator.TryEnqueue(i) {
time.Sleep(10 * time.Millisecond)
}
}
aggregator.SafeStop()
wg.Wait()
}
func TestAggregator_LingerTimeOut(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(100)
batchProcess := func(items []interface{}) error {
defer wg.Add(-len(items))
if len(items) != 4 {
t.Log("linger time out")
}
return nil
}
aggregator := NewAggregator(batchProcess, func(option AggregatorOption) AggregatorOption {
option.BatchSize = 4
option.Workers = 1
option.LingerTime = 150 * time.Millisecond
option.Logger = NewConsoleLogger()
return option
})
aggregator.Start()
for i := 0; i < 100; i++ {
aggregator.TryEnqueue(i)
time.Sleep(100 * time.Millisecond)
}
aggregator.SafeStop()
wg.Wait()
}