Skip to content

Commit

Permalink
Add test to reproduce race for timeout middleware (labstack#1761)
Browse files Browse the repository at this point in the history
Run with `go test -race -count 1 -timeout 10s -run ^TestTimeoutDataRace$ github.com/labstack/echo/v4/middleware`
  • Loading branch information
lammel committed Feb 7, 2021
1 parent 045c282 commit 416526e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions middleware/timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ package middleware
import (
"context"
"errors"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strings"
"testing"
"time"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

func TestTimeoutSkipper(t *testing.T) {
Expand Down Expand Up @@ -175,3 +176,28 @@ func TestTimeoutTestRequestClone(t *testing.T) {
assert.NoError(t, err)

}

// TestTimeoutDataRace shows a data race occurs if the handle func executes for same duration as specified timeout.
func TestTimeoutDataRace(t *testing.T) {
t.Parallel()
const timeout = 10 * time.Millisecond
mw := TimeoutWithConfig(TimeoutConfig{
Timeout: timeout,
})

req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()

e := echo.New()
c := e.NewContext(req, rec)

const helloWorld = "Hello, World!"
err := mw(func(c echo.Context) error {
time.Sleep(timeout)
return c.String(http.StatusOK, helloWorld)
})(c)

assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, helloWorld, rec.Body.String())
}

0 comments on commit 416526e

Please sign in to comment.