From eef8b2d73ba6ac0888ba28695c8a5ed824e953d6 Mon Sep 17 00:00:00 2001 From: mehrdadbn9 Date: Fri, 6 Feb 2026 10:44:54 +0330 Subject: [PATCH] Add test coverage for concurrent-safe routes and StatusContinue - Add TestConcurrentAddRouteAndRoutes to test concurrent access to addRoute() and Routes() - Update TestContextBodyAllowedForStatus to include http.StatusContinue These tests ensure the new mutex code and constant are covered, fixing codecov coverage drops in PRs #4525 and #4523. --- context_test.go | 1 + gin_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/context_test.go b/context_test.go index 41694585..cf12f508 100644 --- a/context_test.go +++ b/context_test.go @@ -1031,6 +1031,7 @@ func TestContextGetCookie(t *testing.T) { } func TestContextBodyAllowedForStatus(t *testing.T) { + assert.False(t, bodyAllowedForStatus(http.StatusContinue)) assert.False(t, bodyAllowedForStatus(http.StatusProcessing)) assert.False(t, bodyAllowedForStatus(http.StatusNoContent)) assert.False(t, bodyAllowedForStatus(http.StatusNotModified)) diff --git a/gin_test.go b/gin_test.go index 43c9494d..9480ff5e 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1067,6 +1067,39 @@ func TestLiteralColonWithHTTPServer(t *testing.T) { assert.Contains(t, w2.Body.String(), "foo") } +func TestConcurrentAddRouteAndRoutes(t *testing.T) { + router := New() + + done := make(chan bool) + + for i := 0; i < 10; i++ { + go func(n int) { + router.GET(fmt.Sprintf("/route%d", n), func(c *Context) { + c.String(http.StatusOK, fmt.Sprintf("route%d", n)) + }) + router.POST(fmt.Sprintf("/route%d", n), func(c *Context) { + c.String(http.StatusOK, fmt.Sprintf("route%d", n)) + }) + done <- true + }(i) + } + + for i := 0; i < 10; i++ { + go func() { + routes := router.Routes() + assert.GreaterOrEqual(t, len(routes), 0) + done <- true + }() + } + + for i := 0; i < 20; i++ { + <-done + } + + routes := router.Routes() + assert.Len(t, routes, 20) +} + // Test that updateRouteTrees is called only once func TestUpdateRouteTreesCalledOnce(t *testing.T) { SetMode(TestMode)