-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathrouter_concurrent_test.go
More file actions
378 lines (322 loc) · 9.82 KB
/
router_concurrent_test.go
File metadata and controls
378 lines (322 loc) · 9.82 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package echo
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConcurrentRouter_Remove(t *testing.T) {
router := NewConcurrentRouter(NewRouter(RouterConfig{}))
_, err := router.Add(Route{
Method: http.MethodGet,
Path: "/initial1",
Handler: handlerFunc,
})
assert.NoError(t, err)
assert.Equal(t, len(router.Routes()), 1)
err = router.Remove(http.MethodGet, "/initial1")
assert.NoError(t, err)
assert.Equal(t, len(router.Routes()), 0)
}
func TestConcurrentRouter_ConcurrentReads(t *testing.T) {
router := NewConcurrentRouter(NewRouter(RouterConfig{}))
testPaths := []string{"/route1", "/route2", "/route3", "/route4", "/route5"}
for _, path := range testPaths {
_, err := router.Add(Route{
Method: http.MethodGet,
Path: path,
Handler: handlerFunc,
})
if err != nil {
t.Fatal(err)
}
}
// Launch 10 goroutines for concurrent reads
var wg sync.WaitGroup
var routeCallCount atomic.Int64
var routesCallCount atomic.Int64
numGoroutines := 10
routeCallsPerGoroutine := 50
routesCallsPerGoroutine := 20
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
// Call Route() 50 times
for j := 0; j < routeCallsPerGoroutine; j++ {
path := testPaths[j%len(testPaths)]
req := httptest.NewRequest(http.MethodGet, path, nil)
rec := httptest.NewRecorder()
c := newContext(req, rec, nil)
handler := router.Route(c)
if handler != nil {
routeCallCount.Add(1)
}
}
// Call Routes() 20 times
for j := 0; j < routesCallsPerGoroutine; j++ {
routes := router.Routes()
if len(routes) == 5 {
routesCallCount.Add(1)
}
}
}(i)
}
wg.Wait()
// Verify all operations succeeded
expectedRouteCalls := int64(numGoroutines * routeCallsPerGoroutine)
expectedRoutesCalls := int64(numGoroutines * routesCallsPerGoroutine)
assert.Equal(t, expectedRouteCalls, routeCallCount.Load(), "all Route() calls should succeed")
assert.Equal(t, expectedRoutesCalls, routesCallCount.Load(), "all Routes() calls should succeed")
}
func TestConcurrentRouter_ConcurrentWrites(t *testing.T) {
router := NewConcurrentRouter(NewRouter(RouterConfig{}))
_, _ = router.Add(Route{Method: http.MethodGet, Path: "/initial1", Handler: handlerFunc})
_, _ = router.Add(Route{Method: http.MethodGet, Path: "/initial2", Handler: handlerFunc})
// Launch 5 goroutines, each adds 10 unique routes
var wg sync.WaitGroup
var addCount atomic.Int64
numGoroutines := 5
addsPerGoroutine := 10
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for j := 0; j < addsPerGoroutine; j++ {
path := fmt.Sprintf("/route-g%d-n%d", goroutineID, j)
_, err := router.Add(Route{
Method: http.MethodGet,
Path: path,
Handler: handlerFunc,
})
if err == nil {
addCount.Add(1)
}
}
}(i)
}
wg.Wait()
// Verify final route count
expectedAdds := int64(numGoroutines * addsPerGoroutine)
assert.Equal(t, expectedAdds, addCount.Load(), "all Add() calls should succeed")
expectedTotal := 2 + int(expectedAdds) // 2 initial + 50 added
assert.Len(t, router.Routes(), expectedTotal, "route count mismatch")
// Verify all routes are accessible
allRoutes := router.Routes()
assert.Len(t, allRoutes, expectedTotal)
}
func TestConcurrentRouter_ConcurrentReadWrite(t *testing.T) {
router := NewConcurrentRouter(NewRouter(RouterConfig{}))
initialPaths := []string{"/read1", "/read2", "/read3"}
for _, path := range initialPaths {
_, err := router.Add(Route{Method: http.MethodGet, Path: path, Handler: handlerFunc})
if err != nil {
t.Fatal(err)
}
}
var wg sync.WaitGroup
var routeCallCount atomic.Int64
var addCallCount atomic.Int64
var routesCallCount atomic.Int64
// Launch 4 reader goroutines: call Route() 100 times each
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 100; j++ {
path := initialPaths[j%len(initialPaths)]
req := httptest.NewRequest(http.MethodGet, path, nil)
rec := httptest.NewRecorder()
c := newContext(req, rec, nil)
handler := router.Route(c)
if handler != nil {
routeCallCount.Add(1)
}
}
}()
}
// Launch 2 writer goroutines: call Add() 20 times each
for i := 0; i < 2; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for j := 0; j < 20; j++ {
path := fmt.Sprintf("/write-g%d-n%d", goroutineID, j)
_, err := router.Add(Route{
Method: http.MethodGet,
Path: path,
Handler: handlerFunc,
})
if err == nil {
addCallCount.Add(1)
}
}
}(i)
}
// Launch 2 inspector goroutines: call Routes() 50 times each
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 50; j++ {
routes := router.Routes()
if routes != nil {
routesCallCount.Add(1)
}
}
}()
}
wg.Wait()
// Verify all operations succeeded
assert.Equal(t, int64(400), routeCallCount.Load(), "all Route() calls should succeed")
assert.Equal(t, int64(40), addCallCount.Load(), "all Add() calls should succeed")
assert.Equal(t, int64(100), routesCallCount.Load(), "all Routes() calls should succeed")
// Verify final route count
expectedTotal := 3 + 40 // 3 initial + 40 added
assert.Len(t, router.Routes(), expectedTotal, "route count mismatch")
}
// TestConcurrentRouter_RoutesIterationDuringModification verifies that iterating over
// Routes() while Add/Remove operations are happening doesn't cause data races.
// This test specifically validates that Routes() returns a copy, not a reference.
func TestConcurrentRouter_RoutesIterationDuringModification(t *testing.T) {
router := NewConcurrentRouter(NewRouter(RouterConfig{}))
// Add initial routes
for i := 0; i < 10; i++ {
_, err := router.Add(Route{
Method: http.MethodGet,
Path: fmt.Sprintf("/initial-%d", i),
Handler: handlerFunc,
})
if err != nil {
t.Fatal(err)
}
}
var wg sync.WaitGroup
var iterationCount atomic.Int64
var addRemoveCount atomic.Int64
// Launch 3 goroutines that iterate over Routes() and access each element
for i := 0; i < 3; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for j := 0; j < 100; j++ {
routes := router.Routes()
// Actually iterate and access the route data
// This would cause a data race if Routes() returned a direct reference
for _, route := range routes {
_ = route.Method // Read the method
_ = route.Path // Read the path
_ = route.Name // Read the name
if len(route.Parameters) > 0 {
_ = route.Parameters[0] // Read parameters if present
}
}
iterationCount.Add(1)
}
}(i)
}
// Launch 2 goroutines that continuously Add routes
for i := 0; i < 2; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for j := 0; j < 30; j++ {
path := fmt.Sprintf("/add-g%d-n%d", goroutineID, j)
_, err := router.Add(Route{
Method: http.MethodPost,
Path: path,
Handler: handlerFunc,
})
if err == nil {
addRemoveCount.Add(1)
}
}
}(i)
}
wg.Wait()
// Verify operations completed
assert.Equal(t, int64(300), iterationCount.Load(), "all iterations should complete")
assert.Equal(t, int64(60), addRemoveCount.Load(), "all add operations should succeed")
// Verify final state
finalRoutes := router.Routes()
assert.Len(t, finalRoutes, 70, "should have 10 initial + 60 added routes")
}
// TestConcurrentRouter_ParametersNoRace verifies that accessing RouteInfo.Parameters
// while routes are being added concurrently doesn't cause data races.
// This test validates that Routes() deep-copies RouteInfo, not just the Routes slice.
func TestConcurrentRouter_ParametersNoRace(t *testing.T) {
router := NewConcurrentRouter(NewRouter(RouterConfig{}))
// Add routes with parameters
_, err := router.Add(Route{
Method: http.MethodGet,
Path: "/users/:id/:name",
Handler: handlerFunc,
})
assert.NoError(t, err)
_, err = router.Add(Route{
Method: http.MethodPost,
Path: "/posts/:postId/comments/:commentId",
Handler: handlerFunc,
})
assert.NoError(t, err)
var wg sync.WaitGroup
var paramsAccessCount atomic.Int64
var addCount atomic.Int64
// Launch 3 goroutines that read Parameters repeatedly
for i := 0; i < 3; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for j := 0; j < 100; j++ {
routes := router.Routes()
// Actually access the Parameters slice data
// This would cause a data race if Parameters weren't deep-copied
for _, r := range routes {
for _, p := range r.Parameters {
_ = len(p) // Read parameter name length
if len(p) > 0 { // Read first character
_ = p[0]
}
}
paramsAccessCount.Add(int64(len(r.Parameters)))
}
}
}(i)
}
// Launch 2 goroutines that add routes with parameters concurrently
for i := 0; i < 2; i++ {
wg.Add(1)
go func(goroutineID int) {
defer wg.Done()
for j := 0; j < 20; j++ {
path := fmt.Sprintf("/api/:v%d/resource/:id", goroutineID*100+j)
_, err := router.Add(Route{
Method: http.MethodPost,
Path: path,
Handler: handlerFunc,
})
if err == nil {
addCount.Add(1)
}
}
}(i)
}
wg.Wait()
// Verify operations completed
assert.Equal(t, int64(40), addCount.Load(), "all add operations should succeed")
assert.Greater(t, paramsAccessCount.Load(), int64(0), "should have accessed parameters")
// Verify final state
finalRoutes := router.Routes()
assert.Len(t, finalRoutes, 42, "should have 2 initial + 40 added routes")
// Verify we can still safely access Parameters after concurrent operations
for _, route := range finalRoutes {
for _, param := range route.Parameters {
assert.NotEmpty(t, param, "parameter name should not be empty")
}
}
}