-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmicrobatch_test.go
207 lines (162 loc) · 4.73 KB
/
microbatch_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
package microbatch_test
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/audipasuatmadi/go-microbatch"
"github.com/stretchr/testify/assert"
)
func Test_ReadsDataWhenReachesBufferSize(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
m, err := microbatch.New[int32](context.Background(), microbatch.Config[int32]{Strategy: µbatch.SizeBasedStrategy[int32]{MaxSize: 10}})
if err != nil {
t.Fatal(err)
}
dummyData := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
go m.Start()
// the reader is in another thread... reading
go func(m *microbatch.Microbatch[int32]) {
var allData = []int32{}
result := <-m.ResultBatch
for _, event := range result {
allData = append(allData, event.Payload)
}
assert.Equal(t, 10, len(allData))
assert.Equal(t, []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, allData)
wg.Done()
}(m)
for _, temperature := range dummyData {
m.Add(context.Background(), temperature)
}
wg.Wait()
}
func Test_ReadsDataWhenReachesBufferSizeAndFillsTheRemainingData(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
m, err := microbatch.New[int32](context.Background(), microbatch.Config[int32]{})
if err != nil {
t.Fatal(err)
}
dummyData := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
go m.Start()
// the reader is in another thread... reading
go func(m *microbatch.Microbatch[int32]) {
var allData = []int32{}
result := <-m.ResultBatch
for _, event := range result {
allData = append(allData, event.Payload)
}
fmt.Println(allData)
assert.Equal(t, 5, len(allData))
assert.Equal(t, []int32{1, 2, 3, 4, 5}, allData)
allData = []int32{}
result = <-m.ResultBatch
for _, event := range result {
allData = append(allData, event.Payload)
}
assert.Equal(t, 5, len(allData))
assert.Equal(t, []int32{6, 7, 8, 9, 10}, allData)
wg.Done()
}(m)
for _, temperature := range dummyData {
m.Add(context.Background(), temperature)
}
wg.Wait()
}
func Test_ReadsDataManually(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
duration := 100000 * time.Hour
ctx, cancel := context.WithCancel(context.Background())
m, err := microbatch.New[int32](ctx, microbatch.Config[int32]{
Strategy: µbatch.SizeBasedStrategy[int32]{MaxSize: 100},
BatchTimeout: &duration,
})
if err != nil {
t.Fatal(err)
}
dummyData := []int32{1, 2, 3, 4, 5}
go m.Start()
go func(m *microbatch.Microbatch[int32]) {
var allData = []int32{}
result := <-m.ResultBatch
for _, event := range result {
allData = append(allData, event.Payload)
}
fmt.Println(allData)
assert.Equal(t, 5, len(allData))
assert.Equal(t, []int32{1, 2, 3, 4, 5}, allData)
wg.Done()
}(m)
for _, temperature := range dummyData {
m.Add(ctx, temperature)
}
// manually flush the data
cancel()
wg.Wait()
}
// func Test_ThreadSafeOnTheMicrobatch(t *testing.T) {
// var wg sync.WaitGroup
// wg.Add(1)
// m := microbatch.New[int32](microbatch.NewParams{
// MaxSize: int32(5),
// FlushInterval: time.Hour,
// })
// dummyData := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// // the reader is in another thread... reading
// go func(m microbatch.Microbatch[int32]) {
// dataset1 := m.ReadData(context.Background())
// assert.Equal(t, 5, len(dataset1))
// dataset2 := m.ReadData(context.Background())
// assert.Equal(t, 5, len(dataset2))
// var mappedNums map[int32]bool = make(map[int32]bool)
// for _, v := range dataset1 {
// mappedNums[v] = true
// }
// for _, v := range dataset2 {
// mappedNums[v] = true
// }
// for _, v := range mappedNums {
// if v == false {
// assert.Fail(t, "the dataset is not correct")
// }
// }
// wg.Done()
// }(m)
// for _, temperature := range dummyData {
// go m.Add(context.Background(), temperature)
// }
// wg.Wait()
// }
// func Test_ReadsDataWhenReachesTheFlushInterval(t *testing.T) {
// var wg sync.WaitGroup
// wg.Add(1)
// m := microbatch.New[int32](microbatch.NewParams{
// MaxSize: int32(10),
// FlushInterval: 1000 * time.Millisecond,
// })
// dummyData := []int32{1, 2, 3, 4, 5}
// dummyData2 := []int32{6, 7, 8, 9, 10}
// // the reader is in another thread... reading
// go func(m microbatch.Microbatch[int32]) {
// allData := m.ReadData(context.Background())
// assert.Equal(t, 5, len(allData))
// assert.Equal(t, []int32{1, 2, 3, 4, 5}, allData)
// // The next read it should got the remaining data
// allData = m.ReadData(context.Background())
// assert.Equal(t, 5, len(allData))
// assert.Equal(t, []int32{6, 7, 8, 9, 10}, allData)
// wg.Done()
// }(m)
// for _, temperature := range dummyData {
// m.Add(context.Background(), temperature)
// }
// time.Sleep(1010 * time.Millisecond)
// for _, temperature := range dummyData2 {
// m.Add(context.Background(), temperature)
// }
// wg.Wait()
// }