Skip to content

Commit

Permalink
Add readiness check to serverStart()
Browse files Browse the repository at this point in the history
  • Loading branch information
gaby committed Mar 3, 2024
1 parent e3db3b5 commit e84e5ef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
55 changes: 34 additions & 21 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"github.com/valyala/fasthttp/fasthttputil"
)

func startServer(app *App, ln *fasthttputil.InmemoryListener) {
func startServer(t *testing.T, app *App, ln *fasthttputil.InmemoryListener) {
t.Helper()
go func() {
err := app.Listener(ln, ListenConfig{
DisableStartupMessage: true,
Expand All @@ -34,7 +35,19 @@ func startServer(app *App, ln *fasthttputil.InmemoryListener) {
}
}()

time.Sleep(2 * time.Second)
// Server readiness check
for i := 0; i < 10; i++ {
conn, err := ln.Dial()
if err == nil {
conn.Close() //nolint:errcheck // We don't care about the error here
break
}
// Wait a bit before retrying
time.Sleep(100 * time.Millisecond)
if i == 9 {
t.Fatalf("Server did not become ready in time: %v", err)
}
}
}

func Test_Client_Invalid_URL(t *testing.T) {
Expand All @@ -47,7 +60,7 @@ func Test_Client_Invalid_URL(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

a := Get("http://example.com\r\n\r\nGET /\r\n\r\n")

Expand Down Expand Up @@ -81,7 +94,7 @@ func Test_Client_Get(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
a := Get("http://example.com")
Expand All @@ -106,7 +119,7 @@ func Test_Client_Head(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
a := Head("http://example.com")
Expand All @@ -132,7 +145,7 @@ func Test_Client_Post(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
args := AcquireArgs()
Expand Down Expand Up @@ -164,7 +177,7 @@ func Test_Client_Put(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
args := AcquireArgs()
Expand Down Expand Up @@ -196,7 +209,7 @@ func Test_Client_Patch(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
args := AcquireArgs()
Expand Down Expand Up @@ -229,7 +242,7 @@ func Test_Client_Delete(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
args := AcquireArgs()
Expand Down Expand Up @@ -258,7 +271,7 @@ func Test_Client_UserAgent(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

t.Run("default", func(t *testing.T) {
for i := 0; i < 5; i++ {
Expand Down Expand Up @@ -401,7 +414,7 @@ func Test_Client_Agent_Host(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

a := Get("http://1.1.1.1:8080").
Host("example.com").
Expand Down Expand Up @@ -496,7 +509,7 @@ func Test_Client_Agent_Custom_Response(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
a := AcquireAgent()
Expand Down Expand Up @@ -532,7 +545,7 @@ func Test_Client_Agent_Dest(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

t.Run("small dest", func(t *testing.T) {
dest := []byte("de")
Expand Down Expand Up @@ -604,7 +617,7 @@ func Test_Client_Agent_RetryIf(t *testing.T) {
app := New()

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

a := Post("http://example.com").
RetryIf(func(_ *Request) bool {
Expand Down Expand Up @@ -735,7 +748,7 @@ func Test_Client_Agent_MultipartForm(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

args := AcquireArgs()

Expand Down Expand Up @@ -805,7 +818,7 @@ func Test_Client_Agent_MultipartForm_SendFiles(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

for i := 0; i < 5; i++ {
ff := AcquireFormFile()
Expand Down Expand Up @@ -912,7 +925,7 @@ func Test_Client_Agent_Timeout(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

a := Get("http://example.com").
Timeout(time.Millisecond * 50)
Expand All @@ -936,7 +949,7 @@ func Test_Client_Agent_Reuse(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

a := Get("http://example.com").
Reuse()
Expand Down Expand Up @@ -1046,7 +1059,7 @@ func Test_Client_Agent_MaxRedirectsCount(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

t.Run("success", func(t *testing.T) {
a := Get("http://example.com?foo").
Expand Down Expand Up @@ -1089,7 +1102,7 @@ func Test_Client_Agent_Struct(t *testing.T) {
})

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

t.Run("success", func(t *testing.T) {
a := Get("http://example.com")
Expand Down Expand Up @@ -1175,7 +1188,7 @@ func testAgent(t *testing.T, handler Handler, wrapAgent func(agent *Agent), exce
app.Get("/", handler)

// Wait for server to start
startServer(app, ln)
startServer(t, app, ln)

c := 1
if len(count) > 0 {
Expand Down
16 changes: 15 additions & 1 deletion listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,27 @@ func Test_Listen_Graceful_Shutdown(t *testing.T) {
})
}()

// Server readiness check
for i := 0; i < 10; i++ {
conn, err := ln.Dial()
if err == nil {
conn.Close() //nolint:errcheck // ignore error
break
}
// Wait a bit before retrying
time.Sleep(100 * time.Millisecond)
if i == 9 {
t.Fatalf("Server did not become ready in time: %v", err)
}
}

testCases := []struct {
Time time.Duration
ExpectedBody string
ExpectedStatusCode int
ExceptedErrsLen int
}{
{Time: 500 * time.Millisecond, ExpectedBody: "example.com", ExpectedStatusCode: StatusOK, ExceptedErrsLen: 0},
{Time: 100 * time.Millisecond, ExpectedBody: "example.com", ExpectedStatusCode: StatusOK, ExceptedErrsLen: 0},
{Time: 5 * time.Second, ExpectedBody: "", ExpectedStatusCode: 0, ExceptedErrsLen: 1},
}

Expand Down
2 changes: 1 addition & 1 deletion middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func startServer(app *fiber.App, ln net.Listener) {
}
}()

time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
}

func createProxyTestServer(t *testing.T, handler fiber.Handler) (*fiber.App, string) {
Expand Down

0 comments on commit e84e5ef

Please sign in to comment.