Skip to content

Commit

Permalink
🚀 v3 Feature: Make app.Test accept a time.Duration timeout (#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Mar 5, 2024
1 parent 79b24ea commit 370cc8b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
14 changes: 7 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -914,11 +914,11 @@ func (app *App) Hooks() *Hooks {

// Test is used for internal debugging by passing a *http.Request.
// Timeout is optional and defaults to 1s, -1 will disable it completely.
func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error) {
func (app *App) Test(req *http.Request, timeout ...time.Duration) (*http.Response, error) {
// Set timeout
timeout := 1000
if len(msTimeout) > 0 {
timeout = msTimeout[0]
to := 1 * time.Second
if len(timeout) > 0 {
to = timeout[0]
}

// Add Content-Length if not provided with body
Expand Down Expand Up @@ -957,12 +957,12 @@ func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error
}()

// Wait for callback
if timeout >= 0 {
if to >= 0 {
// With timeout
select {
case err = <-channel:
case <-time.After(time.Duration(timeout) * time.Millisecond):
return nil, fmt.Errorf("test: timeout error %vms", timeout)
case <-time.After(to):
return nil, fmt.Errorf("test: timeout error after %s", to)
}
} else {
// Without timeout
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ func Test_Test_Timeout(t *testing.T) {
return nil
})

_, err = app.Test(httptest.NewRequest(MethodGet, "/timeout", nil), 20)
_, err = app.Test(httptest.NewRequest(MethodGet, "/timeout", nil), 20*time.Millisecond)
require.Error(t, err, "app.Test(req)")
}

Expand Down
3 changes: 2 additions & 1 deletion middleware/compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"testing"
"time"

"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -118,7 +119,7 @@ func Test_Compress_Brotli(t *testing.T) {
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
req.Header.Set("Accept-Encoding", "br")

resp, err := app.Test(req, 10000)
resp, err := app.Test(req, 10*time.Second)
require.NoError(t, err, "app.Test(req)")
require.Equal(t, 200, resp.StatusCode, "Status code")
require.Equal(t, "br", resp.Header.Get(fiber.HeaderContentEncoding))
Expand Down
2 changes: 1 addition & 1 deletion middleware/idempotency/idempotency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Test_Idempotency(t *testing.T) {
if idempotencyKey != "" {
req.Header.Set("X-Idempotency-Key", idempotencyKey)
}
resp, err := app.Test(req, 3*int(lifetime.Milliseconds()))
resp, err := app.Test(req, 3*lifetime)
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func Test_Logger_WithLatency(t *testing.T) {
sleepDuration = 1 * tu.div

// Create a new HTTP request to the test route
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), int(2*time.Second))
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)

Expand Down Expand Up @@ -300,7 +300,7 @@ func Test_Logger_WithLatency_DefaultFormat(t *testing.T) {
sleepDuration = 1 * tu.div

// Create a new HTTP request to the test route
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), int(2*time.Second))
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)

Expand Down
5 changes: 3 additions & 2 deletions middleware/pprof/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http/httptest"
"testing"
"time"

"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -105,7 +106,7 @@ func Test_Pprof_Subs(t *testing.T) {
if sub == "profile" {
target += "?seconds=1"
}
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5000)
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5*time.Second)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
})
Expand Down Expand Up @@ -133,7 +134,7 @@ func Test_Pprof_Subs_WithPrefix(t *testing.T) {
if sub == "profile" {
target += "?seconds=1"
}
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5000)
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5*time.Second)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
})
Expand Down
16 changes: 8 additions & 8 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Test_Proxy(t *testing.T) {
return c.SendStatus(fiber.StatusTeapot)
})

resp, err := target.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
resp, err := target.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusTeapot, resp.StatusCode)

Expand Down Expand Up @@ -319,7 +319,7 @@ func Test_Proxy_Timeout_Slow_Server(t *testing.T) {
Timeout: 600 * time.Millisecond,
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)

Expand All @@ -343,7 +343,7 @@ func Test_Proxy_With_Timeout(t *testing.T) {
Timeout: 100 * time.Millisecond,
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusInternalServerError, resp.StatusCode)

Expand Down Expand Up @@ -502,8 +502,8 @@ func Test_Proxy_DoTimeout_Timeout(t *testing.T) {
return DoTimeout(c, "http://"+addr, time.Second)
})

_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), int((1*time.Second)/time.Millisecond))
require.Equal(t, errors.New("test: timeout error 1000ms"), err1)
_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), 1*time.Second)
require.Equal(t, errors.New("test: timeout error after 1s"), err1)
}

// go test -race -run Test_Proxy_DoDeadline_RestoreOriginalURL
Expand Down Expand Up @@ -542,8 +542,8 @@ func Test_Proxy_DoDeadline_PastDeadline(t *testing.T) {
return DoDeadline(c, "http://"+addr, time.Now().Add(time.Second))
})

_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), int((1*time.Second)/time.Millisecond))
require.Equal(t, errors.New("test: timeout error 1000ms"), err1)
_, err1 := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", nil), 1*time.Second)
require.Equal(t, errors.New("test: timeout error after 1s"), err1)
}

// go test -race -run Test_Proxy_Do_HTTP_Prefix_URL
Expand Down Expand Up @@ -645,7 +645,7 @@ func Test_ProxyBalancer_Custom_Client(t *testing.T) {
return c.SendStatus(fiber.StatusTeapot)
})

resp, err := target.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2000)
resp, err := target.Test(httptest.NewRequest(fiber.MethodGet, "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusTeapot, resp.StatusCode)

Expand Down

1 comment on commit 370cc8b

@ReneWerner87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 370cc8b Previous: d2b19e2 Ratio
Benchmark_Middleware_Favicon 206.6 ns/op 12 B/op 4 allocs/op 90.01 ns/op 3 B/op 1 allocs/op 2.30

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.