-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathmanager_stress_test.go
364 lines (299 loc) · 9.09 KB
/
manager_stress_test.go
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package circuit
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestManagerCircuitCreationStress tests high-concurrency circuit creation
func TestManagerCircuitCreationStress(t *testing.T) {
mgr := Manager{}
// Create many circuits concurrently to test race conditions in creation
goroutines := 100
circuitsPerRoutine := 50
var wg sync.WaitGroup
uniqueCircuits := make(map[string]struct{})
var mu sync.Mutex
for g := 0; g < goroutines; g++ {
wg.Add(1)
go func(routineNum int) {
defer wg.Done()
for i := 0; i < circuitsPerRoutine; i++ {
// Some goroutines will try to create the same circuit names
circuitNum := i % (circuitsPerRoutine / 2)
circuitName := fmt.Sprintf("stress-circuit-%d", routineNum*1000+circuitNum)
// Try both creation methods
if i%2 == 0 {
c, err := mgr.CreateCircuit(circuitName)
if err == nil && c != nil {
mu.Lock()
uniqueCircuits[circuitName] = struct{}{}
mu.Unlock()
}
} else if mgr.GetCircuit(circuitName) == nil {
// We need to check if the circuit exists first
// Doesn't exist yet, so create it
func() {
defer func() {
// Recover from any panics
_ = recover()
}()
c := mgr.MustCreateCircuit(circuitName)
if c != nil {
mu.Lock()
uniqueCircuits[circuitName] = struct{}{}
mu.Unlock()
}
}()
}
// Verify we can get the circuit
c := mgr.GetCircuit(circuitName)
require.NotNil(t, c)
}
}(g)
}
wg.Wait()
t.Logf("Created %d unique circuits", len(uniqueCircuits))
// Verify all circuits can be used
for name := range uniqueCircuits {
c := mgr.GetCircuit(name)
require.NotNil(t, c)
err := c.Execute(context.Background(), func(ctx context.Context) error {
return nil
}, nil)
require.NoError(t, err)
}
}
// TestManagerConcurrentCircuitAccess tests concurrent access to the same circuits
func TestManagerConcurrentCircuitAccess(t *testing.T) {
mgr := Manager{}
// Create a fixed number of circuits
circuitCount := 20
for i := 0; i < circuitCount; i++ {
mgr.MustCreateCircuit(fmt.Sprintf("shared-circuit-%d", i))
}
// Access them concurrently
goroutines := 50
operationsPerRoutine := 1000
var wg sync.WaitGroup
var operationCounter int64
for g := 0; g < goroutines; g++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < operationsPerRoutine; i++ {
// Randomly access one of the circuits
circuitNum := i % circuitCount
circuitName := fmt.Sprintf("shared-circuit-%d", circuitNum)
c := mgr.GetCircuit(circuitName)
require.NotNil(t, c)
// Perform an operation
err := c.Execute(context.Background(), func(ctx context.Context) error {
// Mix success and failure
if i%7 == 0 {
return errors.New("random failure")
}
return nil
}, nil)
// Don't care about the error, just that we performed the operation
atomic.AddInt64(&operationCounter, 1)
_ = err
if i%11 == 0 {
// Occasionally check if exists
exists := mgr.GetCircuit(circuitName) != nil
require.True(t, exists)
// Also try a non-existent circuit
exists = mgr.GetCircuit("non-existent-circuit") != nil
require.False(t, exists)
}
}
}()
}
wg.Wait()
t.Logf("Performed %d operations on %d shared circuits", operationCounter, circuitCount)
require.Equal(t, int64(goroutines*operationsPerRoutine), operationCounter)
}
// TestManagerCircuitRunningMetrics tests that the manager correctly tracks running metrics
func TestManagerCircuitRunningMetrics(t *testing.T) {
mgr := Manager{}
// Create some circuits
circuitCount := 5
for i := 0; i < circuitCount; i++ {
mgr.MustCreateCircuit(fmt.Sprintf("metric-circuit-%d", i))
}
// Update metrics from multiple goroutines
goroutines := 20
updatesPerRoutine := 100
var wg sync.WaitGroup
// Run many goroutines that will put load on the circuits
for g := 0; g < goroutines; g++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for i := 0; i < updatesPerRoutine; i++ {
// Cycle through circuits
circuitNum := i % circuitCount
circuitName := fmt.Sprintf("metric-circuit-%d", circuitNum)
c := mgr.GetCircuit(circuitName)
// Different goroutines will hit circuits with different patterns
var err error
switch id % 4 {
case 0:
// Always successful
err = c.Execute(context.Background(), func(ctx context.Context) error {
return nil
}, nil)
case 1:
// Always error
err = c.Execute(context.Background(), func(ctx context.Context) error {
return errors.New("failure")
}, nil)
case 2:
// Slow execution (possible timeout)
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*5)
err = c.Execute(ctx, func(ctx context.Context) error {
select {
case <-time.After(time.Millisecond * 10):
return nil
case <-ctx.Done():
return ctx.Err()
}
}, nil)
cancel()
case 3:
// Mix of success/failure
shouldFail := i%2 == 0
err = c.Execute(context.Background(), func(ctx context.Context) error {
if shouldFail {
return errors.New("conditional failure")
}
return nil
}, nil)
}
// Don't assert on err, we're just generating metrics
_ = err
}
}(g)
}
wg.Wait()
// Get the manager's metrics - just validate it works
metrics := mgr.Var()
require.NotNil(t, metrics)
}
// TestManagerConcurrentFactoryConfiguration tests the manager's handling of
// circuit creation with concurrent configuration factories
func TestManagerConcurrentFactoryConfiguration(t *testing.T) {
// Create factories that vary the configuration
factoryCount := 5
factories := make([]CommandPropertiesConstructor, factoryCount)
for i := 0; i < factoryCount; i++ {
timeoutValue := time.Millisecond * time.Duration(20*(i+1))
factories[i] = func(circuitName string) Config {
return Config{
Execution: ExecutionConfig{
Timeout: timeoutValue,
},
}
}
}
mgr := Manager{
DefaultCircuitProperties: factories,
}
// Create circuits concurrently
goroutines := 30
circuitsPerRoutine := 10
var wg sync.WaitGroup
var mu sync.Mutex
circuitTimeouts := make(map[string]time.Duration)
for g := 0; g < goroutines; g++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for i := 0; i < circuitsPerRoutine; i++ {
circuitName := fmt.Sprintf("factory-circuit-%d-%d", id, i)
// Create the circuit - this should apply all factories
c := mgr.MustCreateCircuit(circuitName)
// Check its timeout
timeout := c.Config().Execution.Timeout
mu.Lock()
circuitTimeouts[circuitName] = timeout
mu.Unlock()
// Execute it
ctx, cancel := context.WithTimeout(context.Background(), timeout*2)
err := c.Execute(ctx, func(ctx context.Context) error {
sleepTime := timeout / 2 // Should finish in time
time.Sleep(sleepTime)
return nil
}, nil)
cancel()
require.NoError(t, err)
}
}(g)
}
wg.Wait()
t.Logf("Created %d circuits with factories", len(circuitTimeouts))
require.Equal(t, goroutines*circuitsPerRoutine, len(circuitTimeouts))
}
// TestRaceOnParallelCircuitControlPlane tests for race conditions
// from goroutines performing control plane operations like circuit creation and deletion
// while other goroutines perform data plane operations
func TestRaceOnParallelCircuitControlPlane(t *testing.T) {
mgr := Manager{}
// Use atomic operations to coordinate goroutines
var controlPlaneOps, dataPlaneOps int64
var running int32 = 1
// Control plane goroutine that creates circuits
go func() {
for atomic.LoadInt32(&running) == 1 {
// Create circuit
circuitName := fmt.Sprintf("test-circuit-%d", atomic.LoadInt64(&controlPlaneOps))
_, _ = mgr.CreateCircuit(circuitName)
// Let it be used for a bit
time.Sleep(time.Microsecond)
// We don't have a delete API, but we can stop using this circuit
// and create new ones to stress the manager
atomic.AddInt64(&controlPlaneOps, 1)
}
}()
// Data plane goroutines
dataPlaneCount := 5
var wg sync.WaitGroup
for i := 0; i < dataPlaneCount; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for atomic.LoadInt32(&running) == 1 {
// Get all circuits
allCircuits := mgr.AllCircuits()
// Try to use each one
for _, c := range allCircuits {
// Just try to use it, don't care about result
_ = c.Execute(context.Background(), func(ctx context.Context) error {
return nil
}, nil)
}
// Check if a specific circuit exists
circuitNum := atomic.LoadInt64(&dataPlaneOps) % 100
circuitName := fmt.Sprintf("test-circuit-%d", circuitNum)
c := mgr.GetCircuit(circuitName)
if c != nil {
// Try to use it
_ = c.Execute(context.Background(), func(ctx context.Context) error {
return nil
}, nil)
}
atomic.AddInt64(&dataPlaneOps, 1)
}
}()
}
// Let it run for a short time
time.Sleep(time.Millisecond * 500)
atomic.StoreInt32(&running, 0)
wg.Wait()
t.Logf("Performed %d control plane operations and %d data plane operations",
atomic.LoadInt64(&controlPlaneOps), atomic.LoadInt64(&dataPlaneOps))
}