(t *testing.T)
| 31 | } |
| 32 | |
| 33 | func TestConcurrentRouter_ConcurrentReads(t *testing.T) { |
| 34 | router := NewConcurrentRouter(NewRouter(RouterConfig{})) |
| 35 | |
| 36 | testPaths := []string{"/route1", "/route2", "/route3", "/route4", "/route5"} |
| 37 | for _, path := range testPaths { |
| 38 | _, err := router.Add(Route{ |
| 39 | Method: http.MethodGet, |
| 40 | Path: path, |
| 41 | Handler: handlerFunc, |
| 42 | }) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Launch 10 goroutines for concurrent reads |
| 49 | var wg sync.WaitGroup |
| 50 | var routeCallCount atomic.Int64 |
| 51 | var routesCallCount atomic.Int64 |
| 52 | |
| 53 | numGoroutines := 10 |
| 54 | routeCallsPerGoroutine := 50 |
| 55 | routesCallsPerGoroutine := 20 |
| 56 | |
| 57 | for i := range numGoroutines { |
| 58 | wg.Add(1) |
| 59 | go func(goroutineID int) { |
| 60 | defer wg.Done() |
| 61 | |
| 62 | // Call Route() 50 times |
| 63 | for j := range routeCallsPerGoroutine { |
| 64 | path := testPaths[j%len(testPaths)] |
| 65 | req := httptest.NewRequest(http.MethodGet, path, nil) |
| 66 | rec := httptest.NewRecorder() |
| 67 | c := newContext(req, rec, nil) |
| 68 | |
| 69 | handler := router.Route(c) |
| 70 | if handler != nil { |
| 71 | routeCallCount.Add(1) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Call Routes() 20 times |
| 76 | for range routesCallsPerGoroutine { |
| 77 | routes := router.Routes() |
| 78 | if len(routes) == 5 { |
| 79 | routesCallCount.Add(1) |
| 80 | } |
| 81 | } |
| 82 | }(i) |
| 83 | } |
| 84 | |
| 85 | wg.Wait() |
| 86 | |
| 87 | // Verify all operations succeeded |
| 88 | expectedRouteCalls := int64(numGoroutines * routeCallsPerGoroutine) |
| 89 | expectedRoutesCalls := int64(numGoroutines * routesCallsPerGoroutine) |
| 90 |
nothing calls this directly
no test coverage detected
searching dependent graphs…