-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbatcher_integration_test.go
More file actions
343 lines (281 loc) · 8.3 KB
/
batcher_integration_test.go
File metadata and controls
343 lines (281 loc) · 8.3 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
336
337
338
339
340
341
342
343
package batcher
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
// TestIntegratedOptimizations tests multiple optimizations working together.
func TestIntegratedOptimizations(t *testing.T) { //nolint:gocognit,gocyclo // Test requires complex scenarios
// Combined test of WithPool and WithDeduplication
t.Run("PoolWithDeduplication", func(t *testing.T) {
processedItems := make(map[int]int)
var mu sync.Mutex
processBatch := func(batch []*testItem) {
mu.Lock()
defer mu.Unlock()
for _, item := range batch {
processedItems[item.ID]++
}
}
// Create a batcher with pool
poolBatcher := NewWithPool[testItem](50, 20*time.Millisecond, func(batch []*testItem) {
// Then pass to dedup batcher
dedupBatcher := NewWithDeduplication[testItem](50, 20*time.Millisecond, processBatch, false)
for _, item := range batch {
dedupBatcher.Put(item)
}
dedupBatcher.Trigger()
time.Sleep(30 * time.Millisecond)
}, true)
// Add items with duplicates
for i := 0; i < 100; i++ {
poolBatcher.Put(&testItem{ID: i % 20}) // Only 20 unique items
if i%5 == 0 {
poolBatcher.Put(&testItem{ID: i % 20}) // Add duplicate
}
}
// Wait for processing
time.Sleep(200 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
if len(processedItems) != 20 {
t.Errorf("Expected 20 unique items, got %d", len(processedItems))
}
})
// Test all features together
t.Run("AllFeaturesCombined", func(t *testing.T) {
// Track metrics
totalProcessed := atomic.Int32{}
batchCount := atomic.Int32{}
// Use map to track processed items
processedItems := make(map[int]bool)
var mu sync.Mutex
processBatch := func(batch []*testItem) {
batchCount.Add(1)
mu.Lock()
defer mu.Unlock()
for _, item := range batch {
totalProcessed.Add(1)
processedItems[item.ID] = true
}
}
// Use both pool and deduplication features
batcher := NewWithDeduplicationAndPool[testItem](100, 30*time.Millisecond, processBatch, true)
// Concurrent writers
var wg sync.WaitGroup
for g := 0; g < 10; g++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for i := 0; i < 100; i++ {
// Mix of unique and duplicate items
batcher.Put(&testItem{ID: goroutineID*100 + i})
if i%3 == 0 {
batcher.Put(&testItem{ID: goroutineID*100 + i}) // Duplicate
}
}
}(g)
}
wg.Wait()
time.Sleep(100 * time.Millisecond)
mu.Lock()
uniqueCount := len(processedItems)
mu.Unlock()
t.Logf("Total processed: %d, Unique: %d, Batches: %d",
totalProcessed.Load(), uniqueCount, batchCount.Load())
if uniqueCount != 1000 {
t.Errorf("Expected 1000 unique items, got %d", uniqueCount)
}
})
}
// TestLongRunningStability tests the implementations over extended periods.
func TestLongRunningStability(t *testing.T) { //nolint:gocognit,gocyclo // Test requires complex scenarios
if testing.Short() {
t.Skip("Skipping long-running test in short mode")
}
t.Run("ExtendedProcessing", func(t *testing.T) {
// Track memory usage
var startMem, endMem runtime.MemStats
runtime.ReadMemStats(&startMem)
processCount := atomic.Int64{}
errorCount := atomic.Int32{}
processBatch := func(batch []*testItem) {
processCount.Add(int64(len(batch)))
// Simulate some processing
time.Sleep(time.Millisecond)
}
// Create batcher
batcher := NewWithPool[testItem](100, 50*time.Millisecond, processBatch, true)
// Run for extended period
done := make(chan bool)
go func() {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for i := 0; i < 100; i++ { // 10 seconds total
select {
case <-ticker.C:
// Add batch of items
for j := 0; j < 1000; j++ {
batcher.Put(&testItem{ID: i*1000 + j})
}
case <-done:
return
}
}
}()
// Monitor for 10 seconds
time.Sleep(10 * time.Second)
close(done)
// Final processing
time.Sleep(200 * time.Millisecond)
runtime.ReadMemStats(&endMem)
// Check results
expectedMin := int64(90000) // At least 90% of items
if processCount.Load() < expectedMin {
t.Errorf("Expected at least %d items processed, got %d",
expectedMin, processCount.Load())
}
// Check memory growth
memGrowth := endMem.HeapAlloc - startMem.HeapAlloc
t.Logf("Memory growth: %d bytes, Errors: %d",
memGrowth, errorCount.Load())
if errorCount.Load() > 0 {
t.Errorf("Encountered %d errors during processing", errorCount.Load())
}
})
// Test graceful shutdown
t.Run("GracefulShutdown", func(t *testing.T) {
itemsAdded := atomic.Int32{}
itemsProcessed := atomic.Int32{}
processBatch := func(batch []*testItem) {
itemsProcessed.Add(int32(len(batch))) //nolint:gosec // Test code with controlled input
time.Sleep(10 * time.Millisecond) // Slow processing
}
batcher := New[testItem](50, 100*time.Millisecond, processBatch, true)
// Add items continuously
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
default:
batcher.Put(&testItem{ID: int(itemsAdded.Load())})
itemsAdded.Add(1)
time.Sleep(time.Millisecond)
}
}
}()
// Run for a bit
time.Sleep(500 * time.Millisecond)
// Stop adding items
close(done)
// Trigger final batch
batcher.Trigger()
// Wait for processing to complete
time.Sleep(200 * time.Millisecond)
// Most items should be processed
processRate := float64(itemsProcessed.Load()) / float64(itemsAdded.Load())
t.Logf("Added: %d, Processed: %d, Rate: %.2f",
itemsAdded.Load(), itemsProcessed.Load(), processRate)
if processRate < 0.9 {
t.Errorf("Processing rate too low: %.2f", processRate)
}
})
// Test panic recovery
t.Run("PanicRecovery", func(t *testing.T) {
panicCount := atomic.Int32{}
successCount := atomic.Int32{}
processBatch := func(batch []*testItem) {
defer func() {
if r := recover(); r != nil {
panicCount.Add(1)
}
}()
// Panic on certain conditions - check if any item in batch matches
for _, item := range batch {
if item.ID == 99 || item.ID == 199 {
panic("simulated panic")
}
}
successCount.Add(1)
}
batcher := New[testItem](10, 50*time.Millisecond, processBatch, true)
// Add items that will trigger panics
for i := 0; i < 200; i++ {
batcher.Put(&testItem{ID: i})
}
// Trigger to ensure all items are processed
batcher.Trigger()
// Wait for processing
time.Sleep(500 * time.Millisecond)
t.Logf("Panics: %d, Successful batches: %d",
panicCount.Load(), successCount.Load())
// Should have both panics and successes
if panicCount.Load() == 0 {
t.Error("Expected some panics to be triggered")
}
if successCount.Load() == 0 {
t.Error("Expected some successful batches")
}
})
}
// TestResourceCleanup verifies proper resource cleanup.
func TestResourceCleanup(t *testing.T) { //nolint:gocognit // Test requires complex scenarios
t.Run("MultipleInstanceLifecycle", func(t *testing.T) {
// Create and destroy multiple instances
for i := 0; i < 10; i++ {
processBatch := func(_ []*testItem) {
time.Sleep(time.Millisecond)
}
// Create different types of batchers
b1 := New[testItem](100, 50*time.Millisecond, processBatch, true)
b2 := NewWithPool[testItem](100, 50*time.Millisecond, processBatch, true)
b3 := NewWithDeduplication[testItem](100, 50*time.Millisecond, processBatch, true)
// Use them
for j := 0; j < 100; j++ {
b1.Put(&testItem{ID: j})
b2.Put(&testItem{ID: j})
b3.Put(&testItem{ID: j})
}
// Trigger and wait
b1.Trigger()
b2.Trigger()
b3.Trigger()
time.Sleep(100 * time.Millisecond)
}
// Force GC and check for leaks
runtime.GC()
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
t.Logf("Final heap alloc: %d MB", m.HeapAlloc/1024/1024)
})
t.Run("TimePartitionedMapCleanup", func(_ *testing.T) {
maps := make([]*TimePartitionedMap[int, string], 10)
// Create multiple maps
for i := 0; i < 10; i++ {
m := NewTimePartitionedMap[int, string](50*time.Millisecond, 5)
defer m.Close()
maps[i] = m
// Add data
for j := 0; j < 1000; j++ {
m.Set(j, fmt.Sprintf("value_%d_%d", i, j))
}
}
// Close all maps
for _, m := range maps {
m.Close()
}
// Verify cleanup
time.Sleep(100 * time.Millisecond)
// Try to use after close (should not panic)
for _, m := range maps {
m.Set(999, "after_close")
_, _ = m.Get(999)
}
})
}