mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 01:38:12 +08:00
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:
parent
e0b6dc6b05
commit
eef8b2d73b
@ -1031,6 +1031,7 @@ func TestContextGetCookie(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestContextBodyAllowedForStatus(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.StatusProcessing))
|
||||||
assert.False(t, bodyAllowedForStatus(http.StatusNoContent))
|
assert.False(t, bodyAllowedForStatus(http.StatusNoContent))
|
||||||
assert.False(t, bodyAllowedForStatus(http.StatusNotModified))
|
assert.False(t, bodyAllowedForStatus(http.StatusNotModified))
|
||||||
|
|||||||
33
gin_test.go
33
gin_test.go
@ -1067,6 +1067,39 @@ func TestLiteralColonWithHTTPServer(t *testing.T) {
|
|||||||
assert.Contains(t, w2.Body.String(), "foo")
|
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
|
// Test that updateRouteTrees is called only once
|
||||||
func TestUpdateRouteTreesCalledOnce(t *testing.T) {
|
func TestUpdateRouteTreesCalledOnce(t *testing.T) {
|
||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user