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.
This commit is contained in:
mehrdadbn9 2026-02-06 10:44:54 +03:30
parent e0b6dc6b05
commit eef8b2d73b
2 changed files with 34 additions and 0 deletions

View File

@ -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))

View File

@ -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)