Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix infinitely app.Test #1997

Merged
merged 7 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
}
}