-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipeline_stress_test.go
More file actions
284 lines (229 loc) · 6.59 KB
/
Copy pathpipeline_stress_test.go
File metadata and controls
284 lines (229 loc) · 6.59 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
package pipeline
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
)
// TestPipelineStress tests the pipeline under extreme load
func TestPipelineStress(t *testing.T) {
if testing.Short() {
t.Skip("Skipping stress test in short mode")
}
// Create a pipeline with multiple nodes
config := DefaultConfig()
config.Workers = 16
config.BufferSize = 1000
processed := atomic.Int64{}
node1 := NewNode[int, int]("node1",
func(ctx context.Context, i int) (int, error) {
return i * 2, nil
}, config)
node2 := NewNode[int, int]("node2",
func(ctx context.Context, i int) (int, error) {
return i + 10, nil
}, config)
node3 := NewNode[int, int]("node3",
func(ctx context.Context, i int) (int, error) {
processed.Add(1)
return i, nil
}, config)
// Create branching pipeline
node1.Connect(node2)
node1.Connect(node3)
node2.Connect(node3)
p, err := NewPipeline(node1)
if err != nil {
t.Fatalf("Failed to create pipeline: %v", err)
}
if err := p.Start(context.Background()); err != nil {
t.Fatalf("Failed to start pipeline: %v", err)
}
// Send a massive number of jobs concurrently
const numJobs = 10000
const numSenders = 100
start := time.Now()
var wg sync.WaitGroup
errors := atomic.Int32{}
for s := 0; s < numSenders; s++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < numJobs/numSenders; i++ {
if err := p.Send(i); err != nil {
errors.Add(1)
}
}
}()
}
wg.Wait()
elapsed := time.Since(start)
// Give time for processing to complete
time.Sleep(100 * time.Millisecond)
// Stop pipeline
if err := p.Stop(10 * time.Second); err != nil {
t.Errorf("Failed to stop pipeline: %v", err)
}
// Verify results
if errors.Load() > 0 {
t.Errorf("Had %d errors during stress test", errors.Load())
}
// Each job goes through 2 paths, so we expect more processed than sent
t.Logf("Stress test: Sent %d jobs, processed %d jobs in %v (%.0f jobs/sec)",
numJobs, processed.Load(), elapsed, float64(numJobs)/elapsed.Seconds())
if processed.Load() < int64(numJobs) {
t.Errorf("Expected at least %d processed jobs, got %d", numJobs, processed.Load())
}
}
// TestPipelineMemoryLeak tests for memory leaks
func TestPipelineMemoryLeak(t *testing.T) {
if testing.Short() {
t.Skip("Skipping memory leak test in short mode")
}
// Create and destroy pipelines repeatedly
for i := 0; i < 100; i++ {
node := NewNode[int, int]("test",
func(ctx context.Context, i int) (int, error) {
return i * 2, nil
})
p, err := NewPipeline(node)
if err != nil {
t.Fatalf("Failed to create pipeline: %v", err)
}
if err := p.Start(context.Background()); err != nil {
t.Fatalf("Failed to start pipeline: %v", err)
}
// Send some jobs
for j := 0; j < 10; j++ {
p.Send(j)
}
// Stop and ensure cleanup
if err := p.Stop(time.Second); err != nil {
t.Errorf("Failed to stop pipeline: %v", err)
}
}
// If we get here without running out of memory, test passes
t.Log("Memory leak test completed successfully")
}
// TestPipelineEdgeCases tests various edge cases
func TestPipelineEdgeCases(t *testing.T) {
t.Run("SendBeforeStart", func(t *testing.T) {
node := NewNode[string, string]("test",
func(ctx context.Context, s string) (string, error) {
return s, nil
})
p, _ := NewPipeline(node)
// Try to send before starting
err := p.Send("test")
if err == nil {
t.Error("Expected error when sending before start")
}
})
t.Run("DoubleStart", func(t *testing.T) {
node := NewNode[string, string]("test",
func(ctx context.Context, s string) (string, error) {
return s, nil
})
p, _ := NewPipeline(node)
// Start twice - second call should be no-op due to sync.Once
err1 := p.Start(context.Background())
err2 := p.Start(context.Background())
if err1 != nil {
t.Errorf("First start failed: %v", err1)
}
if err2 != nil {
t.Errorf("Second start should be no-op but got error: %v", err2)
}
p.Stop(time.Second)
})
t.Run("DoubleStop", func(t *testing.T) {
node := NewNode[string, string]("test",
func(ctx context.Context, s string) (string, error) {
return s, nil
})
p, _ := NewPipeline(node)
p.Start(context.Background())
// Stop twice - second call should be no-op due to sync.Once
err1 := p.Stop(time.Second)
err2 := p.Stop(time.Second)
if err1 != nil {
t.Errorf("First stop failed: %v", err1)
}
if err2 != nil {
t.Errorf("Second stop should be no-op but got error: %v", err2)
}
})
t.Run("SendAfterStop", func(t *testing.T) {
node := NewNode[string, string]("test",
func(ctx context.Context, s string) (string, error) {
return s, nil
})
p, _ := NewPipeline(node)
p.Start(context.Background())
p.Stop(time.Second)
// Try to send after stopping
err := p.Send("test")
if err != ErrPipelineStopped {
t.Errorf("Expected ErrPipelineStopped, got %v", err)
}
})
t.Run("NilProcessor", func(t *testing.T) {
// Should panic when creating node with nil processor
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for nil processor")
} else {
t.Logf("Got expected panic: %v", r)
}
}()
NewNode[string, string]("test", nil)
})
t.Run("LargeData", func(t *testing.T) {
// Test with large data to ensure no buffer overflows
type LargeStruct struct {
Data [1024 * 1024]byte // 1MB
}
node := NewNode[LargeStruct, LargeStruct]("large",
func(ctx context.Context, data LargeStruct) (LargeStruct, error) {
return data, nil
})
p, _ := NewPipeline(node)
p.Start(context.Background())
large := LargeStruct{}
err := p.Send(large)
if err != nil {
t.Errorf("Failed to process large data: %v", err)
}
p.Stop(time.Second)
})
}
// TestNodeMetrics tests metrics collection accuracy
func TestNodeMetrics(t *testing.T) {
config := DefaultConfig()
config.Workers = 1 // Single worker for predictable metrics
node := NewNode[int, int]("metrics",
func(ctx context.Context, i int) (int, error) {
time.Sleep(10 * time.Millisecond)
return i * 2, nil
}, config)
p, _ := NewPipeline(node)
p.Start(context.Background())
const numJobs = 10
for i := 0; i < numJobs; i++ {
if err := p.Send(i); err != nil {
t.Errorf("Failed to send job %d: %v", i, err)
}
}
// Wait for processing to complete
time.Sleep(100 * time.Millisecond)
p.Stop(time.Second)
metrics := node.Metrics()
if metrics.ProcessedCount.Load() != int64(numJobs) {
t.Errorf("Expected %d processed jobs, got %d", numJobs, metrics.ProcessedCount.Load())
}
avgLatency := metrics.GetAverageLatency()
if avgLatency < 10*time.Millisecond || avgLatency > 20*time.Millisecond {
t.Errorf("Unexpected average latency: %v", avgLatency)
}
}