-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_test.go
116 lines (99 loc) · 2.28 KB
/
batch_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
package work
import (
"errors"
"strconv"
"testing"
)
func TestNewBatch(t *testing.T) {
b1 := NewBatch(100, func(i []interface{}) error {
return nil
})
if b1 == nil {
t.Fatal("new batch is nil")
}
}
func TestBatch_PushSingleItemBatch(t *testing.T) {
b1 := Batch{}
singleItemBatchCallCount := 0
b1.Init(1, func(i []interface{}) error {
singleItemBatchCallCount++
if len(i) != 1 {
return errors.New("size did not match for i = " + strconv.Itoa(singleItemBatchCallCount))
}
if iAsInt, ok := i[0].(int); !ok {
return errors.New("payload was not of correct type")
} else if iAsInt != singleItemBatchCallCount {
return errors.New("payload incorrect and likely arrived out of order")
}
return nil
})
if err := b1.Push(1); err != nil {
t.Fatal(err)
}
if err := b1.Push(2); err != nil {
t.Fatal(err)
}
// flush should yield no call
if err := b1.Flush(); err != nil {
t.Fatal(err)
}
if singleItemBatchCallCount != 2 {
t.Fail()
}
}
func TestBatch_Push(t *testing.T) {
b2 := Batch{}
itemBatchCallCount := 0
b2.Init(2, func(i []interface{}) error {
itemBatchCallCount++
if len(i) > 2 {
return errors.New("batch size too large for i = " + strconv.Itoa(itemBatchCallCount))
}
var i0, i1 int
var ok bool
// grab the first item
if i0, ok = i[0].(int); !ok {
return errors.New("payload was not of correct type")
}
// grab the second item, if it's there
if len(i) > 1 {
if i1, ok = i[1].(int); !ok {
return errors.New("payload was not of correct type")
}
} else {
if itemBatchCallCount != 3 {
return errors.New("single item batch occurred at the wrong time")
}
return nil
}
if i0 >= i1 {
return errors.New("payload was out of order")
}
if i0 > itemBatchCallCount * 2 || i1 > itemBatchCallCount * 2 {
return errors.New("payload was out of order - result escalated too quickly")
}
return nil
})
if err := b2.Push(1); err != nil {
t.Fatal(err)
}
if err := b2.Push(2); err != nil {
t.Fatal(err)
}
if err := b2.Push(3); err != nil {
t.Fatal(err)
}
if err := b2.Push(4); err != nil {
t.Fatal(err)
}
if err := b2.Push(4); err != nil {
t.Fatal(err)
}
// flush should yield a call with a single item
if err := b2.Flush(); err != nil {
t.Fatal(err)
}
if itemBatchCallCount != 3 {
t.Fail()
}
}