|
@@ -12,6 +12,8 @@ import (
|
|
|
"net/http"
|
|
"net/http"
|
|
|
"net/http/httptest"
|
|
"net/http/httptest"
|
|
|
"reflect"
|
|
"reflect"
|
|
|
|
|
+ "strconv"
|
|
|
|
|
+ "sync/atomic"
|
|
|
"testing"
|
|
"testing"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
@@ -488,6 +490,43 @@ func TestEngineHandleContext(t *testing.T) {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func TestEngineHandleContextManyReEntries(t *testing.T) {
|
|
|
|
|
+ expectValue := 10000
|
|
|
|
|
+
|
|
|
|
|
+ var handlerCounter, middlewareCounter int64
|
|
|
|
|
+
|
|
|
|
|
+ r := New()
|
|
|
|
|
+ r.Use(func(c *Context) {
|
|
|
|
|
+ atomic.AddInt64(&middlewareCounter, 1)
|
|
|
|
|
+ })
|
|
|
|
|
+ r.GET("/:count", func(c *Context) {
|
|
|
|
|
+ countStr := c.Param("count")
|
|
|
|
|
+ count, err := strconv.Atoi(countStr)
|
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
|
+
|
|
|
|
|
+ n, err := c.Writer.Write([]byte("."))
|
|
|
|
|
+ assert.NoError(t, err)
|
|
|
|
|
+ assert.Equal(t, 1, n)
|
|
|
|
|
+
|
|
|
|
|
+ switch {
|
|
|
|
|
+ case count > 0:
|
|
|
|
|
+ c.Request.URL.Path = "/" + strconv.Itoa(count-1)
|
|
|
|
|
+ r.HandleContext(c)
|
|
|
|
|
+ }
|
|
|
|
|
+ }, func(c *Context) {
|
|
|
|
|
+ atomic.AddInt64(&handlerCounter, 1)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ assert.NotPanics(t, func() {
|
|
|
|
|
+ w := performRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value
|
|
|
|
|
+ assert.Equal(t, 200, w.Code)
|
|
|
|
|
+ assert.Equal(t, expectValue, w.Body.Len())
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ assert.Equal(t, int64(expectValue), handlerCounter)
|
|
|
|
|
+ assert.Equal(t, int64(expectValue), middlewareCounter)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
|
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
|
|
for _, gotRoute := range gotRoutes {
|
|
for _, gotRoute := range gotRoutes {
|
|
|
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|
|
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|