-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflashflood_bugs01_test.go
More file actions
167 lines (136 loc) · 4.21 KB
/
flashflood_bugs01_test.go
File metadata and controls
167 lines (136 loc) · 4.21 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
package flashflood_test
import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"
"github.com/thisisdevelopment/flashflood/v2"
)
// TestObj and getTestObjs are defined in flashflood__01_test.go
// TestLargeWithGate confirmation of bug I found on one of my implementations
func TestLargeWithGateAndFuncScenario1(t *testing.T) {
ff := flashflood.New[[]TestObj](&flashflood.Opts{
BufferAmount: 256,
Timeout: time.Duration(250 * time.Millisecond),
FlushTimeout: time.Duration(500 * time.Millisecond),
FlushEnabled: true,
GateAmount: 64,
TickerTime: time.Duration(50 * time.Millisecond),
})
ff.AddFunc(flashflood.FuncMergeChunkedElements[TestObj]())
// Default to smaller dataset for CI-friendly behavior
// Only use large dataset when running with -test.long or similar custom flag
amount := 10000 // Default: CI-friendly small dataset
timeoutDuration := 10000 * time.Millisecond // Default: shorter timeout
// Check for environment variable to run long tests
if longTests := os.Getenv("LONG_TESTS"); longTests == "true" {
amount = 100000 // Full scale for thorough testing
timeoutDuration = 30000 * time.Millisecond
}
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
defer cancel()
cnt := 0
run := true
wg := &sync.WaitGroup{}
wg.Add(amount)
errChan := make(chan error)
go func(run *bool, errChan *chan error) {
for *run {
select {
case v := <-ch:
// spew.Dump(v)
// panic(nil)
cnt += len(v) // v is []TestObj chunk, count all items in it
// fmt.Println("LEN IS ", len(v), cnt)
wg.Add(len(v) * -1) // Decrement by number of items in chunk
if cnt == amount {
*run = false
close(*errChan)
break
}
case <-ctx.Done():
*errChan <- fmt.Errorf("expected: counter of %d ; got counter of %d", amount, cnt)
}
}
}(&run, &errChan)
objs := getTestObjs(amount)
for _, o := range objs {
ff.Push([]TestObj{o}) // Push single-element slice for chunked processing
// time.Sleep(1 * time.Millisecond)
}
select {
case chanErr := <-errChan:
if chanErr != nil {
t.Fatal(chanErr)
}
}
wg.Wait()
}
func TestLargeWithGateAndFuncScenario2(t *testing.T) {
ff := flashflood.New[[]TestObj](&flashflood.Opts{
BufferAmount: 256,
Timeout: time.Duration(250 * time.Millisecond),
FlushTimeout: 500 * time.Millisecond,
FlushEnabled: true,
GateAmount: 64,
TickerTime: time.Duration(50 * time.Millisecond),
DisableRingUntilChanActive: true,
})
ff.AddFunc(flashflood.FuncMergeChunkedElements[TestObj]())
// Default to smaller dataset for CI-friendly behavior
// Only use large dataset when running with LONG_TESTS=true environment variable
amount := 10000 // Default: CI-friendly small dataset
timeoutDuration := 10000 * time.Millisecond // Default: shorter timeout
// Check for environment variable to run long tests
if longTests := os.Getenv("LONG_TESTS"); longTests == "true" {
amount = 100000 // Full scale for thorough testing
timeoutDuration = 30000 * time.Millisecond
}
objs := getTestObjs(amount)
for _, o := range objs {
ff.Push([]TestObj{o}) // Push single-element slice for chunked processing
}
ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
defer cancel()
cnt := 0
run := true
wg := &sync.WaitGroup{}
wg.Add(amount)
errChan := make(chan error)
ch, err := ff.GetChan()
if err != nil {
t.Fatalf("could not get channel: %v", err)
}
go func(run *bool, errChan *chan error) {
for *run {
select {
case v := <-ch:
// spew.Dump(v)
// panic(nil)
cnt += len(v) // v is []TestObj chunk, count all items in it
// fmt.Println("LEN IS ", len(v), cnt)
wg.Add(len(v) * -1) // Decrement by number of items in chunk
if cnt == amount {
*run = false
close(*errChan)
break
}
case <-ctx.Done():
*errChan <- fmt.Errorf("expected: counter of %d ; got counter of %d", amount, cnt)
}
}
}(&run, &errChan)
select {
case chanErr := <-errChan:
if chanErr != nil {
t.Fatal(chanErr)
}
}
wg.Wait()
}