MCPcopy
hub / github.com/labstack/echo / TestConcurrentRouter_ConcurrentReads

Function TestConcurrentRouter_ConcurrentReads

router_concurrent_test.go:33–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

31}
32
33func 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

Callers

nothing calls this directly

Calls 6

NewConcurrentRouterFunction · 0.85
NewRouterFunction · 0.85
newContextFunction · 0.85
AddMethod · 0.65
RouteMethod · 0.65
RoutesMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…