-
Notifications
You must be signed in to change notification settings - Fork 0
/
batcher_test.go
66 lines (58 loc) · 1.22 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
package batcher
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
)
func TestBatcher_Emit(t *testing.T) {
var (
iterations = 100
itemsCount = 112
)
for it := 0; it < iterations; it++ {
tp := newTestableProcessFn[string]()
b := NewBatcher[string](
Process(tp.process),
MaxSize[string](itemsCount),
Emit[string](
OnSizeReached(3),
Every(10*time.Millisecond),
),
)
b.Start(context.TODO())
for i := 0; i < itemsCount; i++ {
b.Accumulate(fmt.Sprintf("%d", i))
}
b.Terminate()
require.NoError(t, b.Wait())
processedItems := 0
for _, c := range tp.Calls {
processedItems += len(c)
}
assert.Equal(t, itemsCount, processedItems, tp.Calls)
}
}
func BenchmarkNewBatcher(b *testing.B) {
var itemsCount = 10000
tp := newTestableProcessFn[int]()
for i := 0; i < b.N; i++ {
batcher := NewBatcher[int](
Process(tp.process),
MaxSize[int](itemsCount),
Emit[int](
OnSizeReached(100),
Every(100*time.Millisecond),
),
)
b.ReportAllocs()
batcher.Start(context.TODO())
for i := 0; i < itemsCount; i++ {
batcher.Accumulate(i)
}
batcher.Terminate()
require.NoError(b, batcher.Wait())
}
}