1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-24 17:07:00 +02:00

[#3930] replaced the default 100ms api tests timeout in favor of new ApiScenario.Timeout field

This commit is contained in:
Gani Georgiev 2023-12-12 19:46:58 +02:00
parent 8671debc35
commit b31cf984a5
3 changed files with 25 additions and 4 deletions

View File

@ -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 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 ## v0.20.0

View File

@ -5,6 +5,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
"time"
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5"
"github.com/pocketbase/dbx" "github.com/pocketbase/dbx"
@ -22,6 +23,7 @@ func TestRealtimeConnect(t *testing.T) {
{ {
Method: http.MethodGet, Method: http.MethodGet,
Url: "/api/realtime", Url: "/api/realtime",
Timeout: 100 * time.Millisecond,
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedContent: []string{ ExpectedContent: []string{
`id:`, `id:`,
@ -44,6 +46,7 @@ func TestRealtimeConnect(t *testing.T) {
Name: "PB_CONNECT interrupt", Name: "PB_CONNECT interrupt",
Method: http.MethodGet, Method: http.MethodGet,
Url: "/api/realtime", Url: "/api/realtime",
Timeout: 100 * time.Millisecond,
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedEvents: map[string]int{ ExpectedEvents: map[string]int{
"OnRealtimeConnectRequest": 1, "OnRealtimeConnectRequest": 1,
@ -68,6 +71,7 @@ func TestRealtimeConnect(t *testing.T) {
Name: "Skipping/ignoring messages", Name: "Skipping/ignoring messages",
Method: http.MethodGet, Method: http.MethodGet,
Url: "/api/realtime", Url: "/api/realtime",
Timeout: 100 * time.Millisecond,
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedEvents: map[string]int{ ExpectedEvents: map[string]int{
"OnRealtimeConnectRequest": 1, "OnRealtimeConnectRequest": 1,

View File

@ -29,6 +29,11 @@ type ApiScenario struct {
// to ensure that all fired non-awaited go routines have finished // to ensure that all fired non-awaited go routines have finished
Delay time.Duration 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 // expectations
// --- // ---
ExpectedStatus int ExpectedStatus int
@ -90,9 +95,17 @@ func (scenario *ApiScenario) test(t *testing.T) {
// add middleware to timeout long-running requests (eg. keep-alive routes) // add middleware to timeout long-running requests (eg. keep-alive routes)
e.Pre(func(next echo.HandlerFunc) echo.HandlerFunc { e.Pre(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
ctx, cancelFunc := context.WithTimeout(c.Request().Context(), 100*time.Millisecond) slowTimer := time.AfterFunc(3*time.Second, func() {
defer cancelFunc() t.Logf("[WARN] Long running test %q", scenario.Name)
c.SetRequest(c.Request().Clone(ctx)) })
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) return next(c)
} }
}) })