From b31cf984a5037909ddf352c146e04ffb3b852973 Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Tue, 12 Dec 2023 19:46:58 +0200 Subject: [PATCH] [#3930] replaced the default 100ms api tests timeout in favor of new ApiScenario.Timeout field --- CHANGELOG.md | 6 +++++- apis/realtime_test.go | 4 ++++ tests/api.go | 19 ++++++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 137bbcfa..d583cb41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ -## v0.20.1 +## v0.20.1-WIP - Removed the blank current time entry from the logs chart as it was causing confusion when used with custom time ranges. +- Removed the default Go API tests timeout and added a new`ApiScenario.Timeout` option ([#3930](https://github.com/pocketbase/pocketbase/issues/3930)). + A negative or zero value means no tests timeout. + If a single API test that takes more than 3s to complete it will have a log message visible when the test fails or when [`go test -v`] flag is used. + ## v0.20.0 diff --git a/apis/realtime_test.go b/apis/realtime_test.go index ef4be345..38cd070e 100644 --- a/apis/realtime_test.go +++ b/apis/realtime_test.go @@ -5,6 +5,7 @@ import ( "net/http" "strings" "testing" + "time" "github.com/labstack/echo/v5" "github.com/pocketbase/dbx" @@ -22,6 +23,7 @@ func TestRealtimeConnect(t *testing.T) { { Method: http.MethodGet, Url: "/api/realtime", + Timeout: 100 * time.Millisecond, ExpectedStatus: 200, ExpectedContent: []string{ `id:`, @@ -44,6 +46,7 @@ func TestRealtimeConnect(t *testing.T) { Name: "PB_CONNECT interrupt", Method: http.MethodGet, Url: "/api/realtime", + Timeout: 100 * time.Millisecond, ExpectedStatus: 200, ExpectedEvents: map[string]int{ "OnRealtimeConnectRequest": 1, @@ -68,6 +71,7 @@ func TestRealtimeConnect(t *testing.T) { Name: "Skipping/ignoring messages", Method: http.MethodGet, Url: "/api/realtime", + Timeout: 100 * time.Millisecond, ExpectedStatus: 200, ExpectedEvents: map[string]int{ "OnRealtimeConnectRequest": 1, diff --git a/tests/api.go b/tests/api.go index 057ea4c3..4e82b849 100644 --- a/tests/api.go +++ b/tests/api.go @@ -29,6 +29,11 @@ type ApiScenario struct { // to ensure that all fired non-awaited go routines have finished Delay time.Duration + // Timeout specifies how long to wait before cancelling the request context. + // + // A zero or negative value means that there will be no timeout. + Timeout time.Duration + // expectations // --- ExpectedStatus int @@ -90,9 +95,17 @@ func (scenario *ApiScenario) test(t *testing.T) { // add middleware to timeout long-running requests (eg. keep-alive routes) e.Pre(func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { - ctx, cancelFunc := context.WithTimeout(c.Request().Context(), 100*time.Millisecond) - defer cancelFunc() - c.SetRequest(c.Request().Clone(ctx)) + slowTimer := time.AfterFunc(3*time.Second, func() { + t.Logf("[WARN] Long running test %q", scenario.Name) + }) + defer slowTimer.Stop() + + if scenario.Timeout > 0 { + ctx, cancelFunc := context.WithTimeout(c.Request().Context(), scenario.Timeout) + defer cancelFunc() + c.SetRequest(c.Request().Clone(ctx)) + } + return next(c) } })