-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflashflood_v2_test.go
More file actions
283 lines (232 loc) · 6.25 KB
/
flashflood_v2_test.go
File metadata and controls
283 lines (232 loc) · 6.25 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
package flashflood_test
import (
"fmt"
"testing"
"time"
flashflood "github.com/thisisdevelopment/flashflood/v2"
)
// TestBasicGenericUsage tests basic functionality with different types
func TestBasicGenericUsage(t *testing.T) {
// Test with strings
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 3,
Timeout: 100 * time.Millisecond,
})
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("Error getting channel: %v", err)
}
ff.Push("item1", "item2", "item3", "item4")
// Should receive items without type casting
for i := 0; i < 4; i++ {
select {
case item := <-ch:
// item is guaranteed to be string - no casting needed!
if len(item) == 0 {
t.Errorf("Received empty string")
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout waiting for item %d", i)
}
}
ff.Close()
}
// TestGenericIntUsage tests with integer type
func TestGenericIntUsage(t *testing.T) {
ff := flashflood.New[int](&flashflood.Opts{
BufferAmount: 2,
Timeout: 100 * time.Millisecond,
})
ch, _ := ff.GetChan()
ff.Push(1, 2, 3)
for i := 0; i < 3; i++ {
select {
case item := <-ch:
// item is guaranteed to be int
if item < 1 || item > 3 {
t.Errorf("Unexpected item value: %d", item)
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout waiting for item %d", i)
}
}
ff.Close()
}
// TestChunkedOutput tests the chunked output functionality
func TestChunkedOutput(t *testing.T) {
// Create FlashFlood that outputs chunks of strings
ff := flashflood.New[[]string](&flashflood.Opts{
BufferAmount: 10,
GateAmount: 3,
Timeout: 100 * time.Millisecond,
})
// Add the chunking transformation
ff.AddFunc(flashflood.FuncMergeChunkedElements[string]())
ch, _ := ff.GetChan()
// Push individual string slices (each wrapped for chunking)
ff.Push([]string{"item1"}, []string{"item2"}, []string{"item3"}, []string{"item4"}, []string{"item5"})
// First chunk should have 3 items
select {
case chunk := <-ch:
// chunk is []string - no casting needed!
if len(chunk) != 3 {
t.Errorf("Expected chunk of 3 items, got %d", len(chunk))
}
if chunk[0] != "item1" || chunk[1] != "item2" || chunk[2] != "item3" {
t.Errorf("Unexpected chunk contents: %v", chunk)
}
case <-time.After(200 * time.Millisecond):
t.Error("Timeout waiting for first chunk")
}
// Wait for timeout to flush remaining items
time.Sleep(150 * time.Millisecond)
// Second chunk should have 2 items (timeout flush)
select {
case chunk := <-ch:
if len(chunk) != 2 {
t.Errorf("Expected chunk of 2 items, got %d", len(chunk))
}
if chunk[0] != "item4" || chunk[1] != "item5" {
t.Errorf("Unexpected chunk contents: %v", chunk)
}
case <-time.After(100 * time.Millisecond):
t.Error("Timeout waiting for second chunk")
}
ff.Close()
}
// TestCustomStruct tests with custom struct type
func TestCustomStruct(t *testing.T) {
type Record struct {
ID int
Data string
}
ff := flashflood.New[Record](&flashflood.Opts{
BufferAmount: 2,
Timeout: 100 * time.Millisecond,
})
ch, _ := ff.GetChan()
ff.Push(Record{ID: 1, Data: "test1"}, Record{ID: 2, Data: "test2"})
for i := 0; i < 2; i++ {
select {
case record := <-ch:
// record is guaranteed to be Record type
if record.ID != i+1 {
t.Errorf("Expected ID %d, got %d", i+1, record.ID)
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout waiting for record %d", i)
}
}
ff.Close()
}
// TestByteSliceProcessing tests byte slice processing without special functions
func TestByteSliceProcessing(t *testing.T) {
ff := flashflood.New[[]byte](&flashflood.Opts{
BufferAmount: 2,
Timeout: 100 * time.Millisecond,
})
ch, _ := ff.GetChan()
ff.Push([]byte{1, 2, 3}, []byte{4, 5, 6})
for i := 0; i < 2; i++ {
select {
case byteSlice := <-ch:
// byteSlice is guaranteed to be []byte
if len(byteSlice) != 3 {
t.Errorf("Expected byte slice of length 3, got %d", len(byteSlice))
}
case <-time.After(200 * time.Millisecond):
t.Errorf("Timeout waiting for byte slice %d", i)
}
}
ff.Close()
}
// TestGateAmount tests gate mechanism with generics
func TestGateAmount(t *testing.T) {
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 2, // Small buffer so items overflow
GateAmount: 3, // Release exactly 3 items at a time
Timeout: 100 * time.Millisecond,
})
ch, _ := ff.GetChan()
// Push 5 items - buffer=2 so 3 will overflow and trigger gate mechanism
ff.Push("a", "b", "c", "d", "e")
// Collect all items
received := 0
items := make([]string, 0)
// Wait for immediate overflow drain
timeout := time.After(50 * time.Millisecond)
drainLoop:
for {
select {
case item := <-ch:
received++
items = append(items, item)
case <-timeout:
break drainLoop
}
}
// Wait for timeout to flush remaining items
time.Sleep(150 * time.Millisecond)
// Collect remaining items
for {
select {
case item := <-ch:
received++
items = append(items, item)
default:
goto done
}
}
done:
if received != 5 {
t.Errorf("Expected 5 total items, got %d: %v", received, items)
}
ff.Close()
}
// Example functions for godoc
// ExampleNew demonstrates basic generic usage
func ExampleNew() {
// Create a string buffer
ff := flashflood.New[string](&flashflood.Opts{
BufferAmount: 3,
Timeout: 100 * time.Millisecond,
})
ch, _ := ff.GetChan()
ff.Push("item1", "item2", "item3", "item4")
// Receive items - no type casting needed!
for i := 0; i < 4; i++ {
item := <-ch
fmt.Printf("Received: %s\n", item)
}
ff.Close()
// Output: Received: item1
// Received: item2
// Received: item3
// Received: item4
}
// ExampleFuncMergeChunkedElements demonstrates chunked output
func ExampleFuncMergeChunkedElements() {
// Create buffer that outputs chunks of strings
ff := flashflood.New[[]string](&flashflood.Opts{
BufferAmount: 10,
GateAmount: 3,
Timeout: 100 * time.Millisecond,
})
// FuncMergeChunkedElements is deprecated in v2
ch, _ := ff.GetChan()
ff.Push([]string{"a", "b", "c"}, []string{"d", "e"})
// Wait for timeout to ensure all items are processed
time.Sleep(150 * time.Millisecond)
for {
select {
case chunk := <-ch:
fmt.Printf("Chunk: %v\n", chunk)
default:
goto done
}
}
done:
ff.Close()
// Output: Chunk: [a b c]
// Chunk: [d e]
}