-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions_test.go
186 lines (177 loc) · 3.68 KB
/
options_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
package gomdb
import (
"errors"
"testing"
"time"
)
func Test_PollingStrategies(t *testing.T) {
t.Parallel()
testcases := []struct {
name string
strategy PollingStrategy
retrieved []int64
expected int64
delays []time.Duration
}{
{
name: "constant polling",
strategy: ConstantPolling(time.Second)(),
retrieved: []int64{0, 1, 2},
expected: 2,
delays: []time.Duration{time.Second, time.Second, 0},
},
{
name: "exponential polling",
strategy: ExpBackoffPolling(time.Second, 10*time.Second, 2)(),
retrieved: []int64{2, 1, 0, 0, 0, 0, 0, 0},
expected: 2,
delays: []time.Duration{
0,
time.Second,
time.Second,
2 * time.Second,
4 * time.Second,
8 * time.Second,
10 * time.Second,
10 * time.Second,
},
},
{
name: "dynamic polling",
strategy: DynamicPolling(0.75, 50*time.Millisecond, 100*time.Millisecond, time.Second)(),
retrieved: []int64{90, 50, 60, 70, 80, 75, 100},
expected: 100,
delays: []time.Duration{
100 * time.Millisecond,
150 * time.Millisecond,
200 * time.Millisecond,
250 * time.Millisecond,
200 * time.Millisecond,
200 * time.Millisecond,
0,
},
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
for i, r := range tc.retrieved {
d := tc.strategy(r, tc.expected)
if d != tc.delays[i] {
t.Fatalf("on retreived %v/%v expected delay %s, actual %s", r, tc.expected, tc.delays[i], d)
}
}
})
}
}
func Test_categoryConfig_validate(t *testing.T) {
testcases := []struct {
name string
config categoryConfig
expErr error
}{
{
name: "invalid stream position",
config: categoryConfig{
position: -1,
batchSize: 1,
},
expErr: ErrInvalidReadPosition,
},
{
name: "invalid batch size",
config: categoryConfig{
position: 0,
batchSize: 0,
},
expErr: ErrInvalidReadBatchSize,
},
{
name: "negative consumer member index",
config: categoryConfig{
position: 0,
batchSize: 1,
consumerGroupMember: -1,
},
expErr: ErrInvalidConsumerGroupMember,
},
{
name: "consumer member index out of range",
config: categoryConfig{
position: 0,
batchSize: 1,
consumerGroupMember: 1,
consumerGroupSize: 1,
},
expErr: ErrInvalidConsumerGroupMember,
},
{
name: "negative consumer group size",
config: categoryConfig{
position: 0,
batchSize: 1,
consumerGroupSize: -1,
},
expErr: ErrInvalidConsumerGroupSize,
},
{
name: "valid",
config: categoryConfig{
position: 0,
batchSize: 1,
consumerGroupMember: 1,
consumerGroupSize: 2,
},
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := tc.config.validate()
if !errors.Is(err, tc.expErr) {
t.Fatalf("expected %v, actual %v", tc.expErr, err)
}
})
}
}
func Test_streamConfig_validate(t *testing.T) {
testcases := []struct {
name string
config streamConfig
expErr error
}{
{
name: "invalid stream version",
config: streamConfig{
version: -1,
batchSize: 1,
},
expErr: ErrInvalidReadStreamVersion,
},
{
name: "invalid batch size",
config: streamConfig{
version: 0,
batchSize: 0,
},
expErr: ErrInvalidReadBatchSize,
},
{
name: "valid",
config: streamConfig{
version: 0,
batchSize: 1,
},
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
err := tc.config.validate()
if !errors.Is(err, tc.expErr) {
t.Fatalf("expected %v, actual %v", tc.expErr, err)
}
})
}
}