-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcbfunc_test.go
More file actions
335 lines (273 loc) · 7.88 KB
/
cbfunc_test.go
File metadata and controls
335 lines (273 loc) · 7.88 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package flashflood_test
import (
"reflect"
"testing"
"time"
"github.com/thisisdevelopment/flashflood/v2"
)
func TestFuncPassThrough(t *testing.T) {
// Test with string type
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 5,
GateAmount: 3,
Timeout: 100 * time.Millisecond,
})
// Add FuncPassThrough
ff.AddFunc(flashflood.FuncPassThrough[string]())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push elements that should trigger gate
ff.Push("item1", "item2", "item3")
// Collect results
var results []string
for i := 0; i < 3; i++ {
select {
case item := <-ch:
results = append(results, item)
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout waiting for item %d", i+1)
}
}
// Verify PassThrough doesn't modify the items
expected := []string{"item1", "item2", "item3"}
if !reflect.DeepEqual(results, expected) {
t.Fatalf("expected %v, got %v", expected, results)
}
ff.Close()
}
func TestFuncPassThroughWithInt(t *testing.T) {
// Test with int type to verify generics work
ff := flashflood.New[int](&flashflood.Opts{
BufferAmount: 5,
GateAmount: 2,
Timeout: 100 * time.Millisecond,
})
// Add FuncPassThrough
ff.AddFunc(flashflood.FuncPassThrough[int]())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push elements that should trigger gate
ff.Push(10, 20)
// Collect results
var results []int
for i := 0; i < 2; i++ {
select {
case item := <-ch:
results = append(results, item)
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout waiting for item %d", i+1)
}
}
// Verify PassThrough doesn't modify the items
expected := []int{10, 20}
if !reflect.DeepEqual(results, expected) {
t.Fatalf("expected %v, got %v", expected, results)
}
ff.Close()
}
func TestFuncFlatten(t *testing.T) {
// Test FuncFlatten with byte slices -> bytes
// Create a FlashFlood[byte] (individual bytes)
ff := flashflood.New[byte](&flashflood.Opts{
BufferAmount: 10,
GateAmount: 4,
Timeout: 100 * time.Millisecond,
})
// Add FuncFlatten - this is for processing individual bytes
ff.AddFunc(flashflood.FuncFlatten[[]byte, byte]())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push individual bytes to trigger gate
ff.Push(0x01, 0x02, 0x03, 0x04)
// Collect results
var results []byte
for i := 0; i < 4; i++ {
select {
case item := <-ch:
results = append(results, item)
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout waiting for byte %d", i+1)
}
}
// Verify FuncFlatten passes through individual bytes unchanged
expected := []byte{0x01, 0x02, 0x03, 0x04}
if !reflect.DeepEqual(results, expected) {
t.Fatalf("expected %v, got %v", expected, results)
}
ff.Close()
}
func TestFuncFlattenWithStrings(t *testing.T) {
// Test FuncFlatten with string slices -> strings
// Create a FlashFlood[string] (individual strings)
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 8,
GateAmount: 3,
Timeout: 100 * time.Millisecond,
})
// Add FuncFlatten - this processes individual strings
ff.AddFunc(flashflood.FuncFlatten[[]string, string]())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push individual strings to trigger gate
ff.Push("hello", "world", "test")
// Collect results
var results []string
for i := 0; i < 3; i++ {
select {
case item := <-ch:
results = append(results, item)
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout waiting for string %d", i+1)
}
}
// Verify FuncFlatten passes through individual strings unchanged
expected := []string{"hello", "world", "test"}
if !reflect.DeepEqual(results, expected) {
t.Fatalf("expected %v, got %v", expected, results)
}
ff.Close()
}
// Test all callback functions together in a comprehensive example
func TestAllCallbackFunctions(t *testing.T) {
t.Run("FuncMergeChunkedElements", func(t *testing.T) {
ff := flashflood.New[[]string](&flashflood.Opts{
BufferAmount: 10,
GateAmount: 3,
Timeout: 100 * time.Millisecond,
})
ff.AddFunc(flashflood.FuncMergeChunkedElements[string]())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push individual slices
ff.Push([]string{"item1"}, []string{"item2"}, []string{"item3"})
// Should get one merged chunk
select {
case result := <-ch:
expected := []string{"item1", "item2", "item3"}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("expected %v, got %v", expected, result)
}
case <-time.After(200 * time.Millisecond):
t.Fatal("timeout waiting for merged chunk")
}
ff.Close()
})
t.Run("FuncMergeBytes", func(t *testing.T) {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 10,
GateAmount: 2,
Timeout: 100 * time.Millisecond,
})
ff.AddFunc(flashflood.FuncMergeBytes())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push byte slices
ff.Push([]byte{0x01, 0x02}, []byte{0x03, 0x04})
// Should get one merged byte slice
select {
case result := <-ch:
expected := []byte{0x01, 0x02, 0x03, 0x04}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("expected %v, got %v", expected, result)
}
case <-time.After(200 * time.Millisecond):
t.Fatal("timeout waiting for merged bytes")
}
ff.Close()
})
t.Run("FuncReturnIndividualBytes", func(t *testing.T) {
ff := flashflood.New[byte](&flashflood.Opts{
BufferAmount: 10,
GateAmount: 3,
Timeout: 100 * time.Millisecond,
})
ff.AddFunc(flashflood.FuncReturnIndividualBytes())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push individual bytes
ff.Push(0x01, 0x02, 0x03)
// Should get individual bytes back
var results []byte
for i := 0; i < 3; i++ {
select {
case result := <-ch:
results = append(results, result)
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout waiting for byte %d", i+1)
}
}
expected := []byte{0x01, 0x02, 0x03}
if !reflect.DeepEqual(results, expected) {
t.Fatalf("expected %v, got %v", expected, results)
}
ff.Close()
})
}
// Test edge cases for callback functions
func TestCallbackFunctionEdgeCases(t *testing.T) {
t.Run("FuncPassThrough with empty input", func(t *testing.T) {
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 5,
GateAmount: 1,
Timeout: 50 * time.Millisecond,
})
ff.AddFunc(flashflood.FuncPassThrough[string]())
// Don't get channel to avoid draining
// Just test that the function doesn't panic with empty input
// This test verifies the function can be created and added without issues
if ff.Count() != 0 {
t.Fatal("buffer should be empty initially")
}
ff.Close()
})
t.Run("FuncFlatten with complex type", func(t *testing.T) {
// Test with custom struct to ensure generics work with complex types
type TestStruct struct {
ID int
Name string
}
ff := flashflood.New[TestStruct](&flashflood.Opts{
BufferAmount: 5,
GateAmount: 2,
Timeout: 100 * time.Millisecond,
})
ff.AddFunc(flashflood.FuncFlatten[[]TestStruct, TestStruct]())
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
// Push structs
struct1 := TestStruct{ID: 1, Name: "test1"}
struct2 := TestStruct{ID: 2, Name: "test2"}
ff.Push(struct1, struct2)
// Should get structs back unchanged
var results []TestStruct
for i := 0; i < 2; i++ {
select {
case result := <-ch:
results = append(results, result)
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout waiting for struct %d", i+1)
}
}
expected := []TestStruct{struct1, struct2}
if !reflect.DeepEqual(results, expected) {
t.Fatalf("expected %v, got %v", expected, results)
}
ff.Close()
})
}