-
Notifications
You must be signed in to change notification settings - Fork 15
/
batcher_test.go
184 lines (160 loc) · 5.18 KB
/
batcher_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
package nakadi
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)
// Check that publishing batcher is getting closed after usage
func TestBatchPublishAPI_Close(t *testing.T) {
client := New("https://example.com", nil)
t.Run("Test closing", func(t *testing.T) {
batcher := NewBatchPublishAPI(client, "test-event-type", nil, nil)
batcher.Close()
})
}
func TestBatchPublishAPI_Publish(t *testing.T) {
t.Run("Test that batching by max batch size is working", func(t *testing.T) {
const maxBatchSize = 4
batcher, mockAPI := setupTestBatchPublisher(24*time.Hour, maxBatchSize)
defer batcher.Close()
dataToPublish := make(map[string]struct{})
for index := 0; index < maxBatchSize*2; index++ {
dataToPublish[fmt.Sprintf("Some data %d", index)] = struct{}{}
}
mockAPI.On("Publish", mock.Anything).Twice().Return(nil)
finish := make(chan bool, 2*maxBatchSize)
for item := range dataToPublish {
go func(itemToPublish string) {
err := batcher.Publish(itemToPublish)
assert.NoError(t, err)
finish <- true
}(item)
}
for i := 0; i < len(dataToPublish); i++ {
<-finish
}
mockAPI.AssertNumberOfCalls(t, "Publish", 2)
assert.Equal(t, dataToPublish, mockAPI.dataPublished)
assert.Equal(t, []int{maxBatchSize, maxBatchSize}, mockAPI.batchSizes)
})
t.Run("Test that error is propagated to calling functions", func(t *testing.T) {
const maxBatchSize = 4
batcher, mockAPI := setupTestBatchPublisher(24*time.Hour, maxBatchSize)
defer batcher.Close()
dataToPublish := make(map[string]struct{}, maxBatchSize*2)
for index := 0; index < maxBatchSize*2; index++ {
dataToPublish[fmt.Sprintf("Some data %d", index)] = struct{}{}
}
mockAPI.On("Publish", mock.Anything).Once().Return(assert.AnError)
mockAPI.On("Publish", mock.Anything).Once().Return(nil)
errored := make(chan string, maxBatchSize*2)
succeeded := make(chan string, maxBatchSize*2)
for item := range dataToPublish {
go func(itemToPublish string) {
err := batcher.Publish(itemToPublish)
if err != nil {
errored <- itemToPublish
} else {
succeeded <- itemToPublish
}
}(item)
}
dataDiscarded := make(map[string]struct{})
dataPublished := make(map[string]struct{})
for i := 0; i < maxBatchSize; i++ {
dataDiscarded[<-errored] = struct{}{}
dataPublished[<-succeeded] = struct{}{}
}
mockAPI.AssertNumberOfCalls(t, "Publish", 2)
assert.Equal(t, dataDiscarded, mockAPI.dataDiscarded)
assert.Equal(t, dataPublished, mockAPI.dataPublished)
})
t.Run("Test that batching by time works", func(t *testing.T) {
const maxBatchSize = 4
const aggregationPeriod = time.Millisecond * 100
batcher, mockAPI := setupTestBatchPublisher(aggregationPeriod, maxBatchSize)
defer batcher.Close()
dataToPublish := make(map[string]struct{}, maxBatchSize*2)
for i := 0; i < maxBatchSize*2; i++ {
dataToPublish[fmt.Sprintf("Some data %d", i)] = struct{}{}
}
mockAPI.On("Publish", mock.Anything).Times(3).Return(nil)
finish := make(chan bool, 2*maxBatchSize)
counter := 0
for item := range dataToPublish {
go func(itemToPublish string) {
err := batcher.Publish(itemToPublish)
assert.NoError(t, err)
finish <- true
}(item)
if counter == (maxBatchSize - 2) {
time.Sleep(aggregationPeriod + time.Millisecond*100)
}
counter += 1
}
for i := 0; i < maxBatchSize*2; i++ {
<-finish
}
mockAPI.AssertNumberOfCalls(t, "Publish", 3)
assert.Equal(t, []int{maxBatchSize - 1, maxBatchSize, 1}, mockAPI.batchSizes)
})
t.Run("Test that slices publishing is propagated without waiting", func(t *testing.T) {
batcher, mockAPI := setupTestBatchPublisher(time.Hour*24, 2)
itemsToPublish := [][]string{
{"batch one"},
{"batch two"},
}
mockAPI.On("Publish", itemsToPublish[0]).Once().Return(nil)
mockAPI.On("Publish", itemsToPublish[1]).Once().Return(nil)
for _, items := range itemsToPublish {
err := batcher.Publish(items)
assert.NoError(t, err)
}
mockAPI.AssertNumberOfCalls(t, "Publish", 2)
})
}
type mockPublishAPI struct {
mock.Mock
batchSizes []int
dataPublished map[string]struct{}
dataDiscarded map[string]struct{}
}
func (m *mockPublishAPI) Publish(events interface{}) error {
e := m.Called(events).Get(0)
autoCollected, ok := events.([]interface{})
if !ok {
if e == nil {
return nil
}
return e.(error)
}
m.batchSizes = append(m.batchSizes, len(autoCollected))
if e == nil {
for _, item := range autoCollected {
m.dataPublished[item.(string)] = struct{}{}
}
return nil
}
for _, item := range autoCollected {
m.dataDiscarded[item.(string)] = struct{}{}
}
return e.(error)
}
func setupTestBatchPublisher(batchCollectionTimeout time.Duration, maxBatchSize int) (*BatchPublishAPI, *mockPublishAPI) {
api := &mockPublishAPI{
batchSizes: make([]int, 0),
dataPublished: make(map[string]struct{}),
dataDiscarded: make(map[string]struct{}),
}
result := BatchPublishAPI{
publishAPI: api,
maxBatchSize: maxBatchSize,
batchCollectionTimeout: batchCollectionTimeout,
eventsChannel: make(chan *eventToPublish, 1000),
dispatchFinished: make(chan int),
}
go result.dispatchThread()
return &result, api
}