Skip to content

Commit

Permalink
fix infinitely app.Test (gofiber#1997)
Browse files Browse the repository at this point in the history
* diff

* better code

* err message

* code style

* up

* fix type error

* use time.Timer
  • Loading branch information
trim21 committed Aug 15, 2022
1 parent eb6838e commit 231ffc6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app.go
Expand Up @@ -845,7 +845,15 @@ func (app *App) Test(req *http.Request, msTimeout ...int) (resp *http.Response,
// Serve conn to server
channel := make(chan error)
go func() {
var returned bool
defer func() {
if !returned {
channel <- fmt.Errorf("runtime.Goexit() called in handler or server panic")
}
}()

channel <- app.server.ServeConn(conn)
returned = true
}()

// Wait for callback
Expand Down
33 changes: 33 additions & 0 deletions app_test.go
Expand Up @@ -16,6 +16,7 @@ import (
"net/http/httptest"
"reflect"
"regexp"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1527,3 +1528,35 @@ func Test_App_UseMountedErrorHandlerForBestPrefixMatch(t *testing.T) {
utils.AssertEqual(t, nil, err, "iotuil.ReadAll()")
utils.AssertEqual(t, "hi, i'm a custom sub sub fiber error", string(b), "Third fiber Response body")
}

func Test_App_Test_no_timeout_infinitely(t *testing.T) {
var err error
c := make(chan int)

go func() {
defer func() { c <- 0 }()
app := New()
app.Get("/", func(c *Ctx) error {
runtime.Goexit()
return nil
})

req := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
_, err = app.Test(req, -1)
}()

tk := time.NewTimer(5 * time.Second)
defer tk.Stop()

select {
case <-tk.C:
t.Error("hanging test")
t.FailNow()
case <-c:
}

if err == nil {
t.Error("unexpected success request")
t.FailNow()
}
}

0 comments on commit 231ffc6

Please sign in to comment.