Skip to content

Commit

Permalink
Fix tests (#1552)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdubbelboer committed May 7, 2023
1 parent 1dcf562 commit fa72f3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
6 changes: 4 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2838,8 +2838,6 @@ func TestHostClientMaxConnWaitTimeoutSuccess(t *testing.T) {
}

func TestHostClientMaxConnWaitTimeoutError(t *testing.T) {
t.Parallel()

var (
emptyBodyCount uint8
ln = fasthttputil.NewInmemoryListener()
Expand Down Expand Up @@ -2903,6 +2901,8 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) {
}
wg.Wait()

time.Sleep(time.Millisecond * 100)

// Prevent a race condition with the conns cleaner that might still be running.
c.connsLock.Lock()
defer c.connsLock.Unlock()
Expand Down Expand Up @@ -3090,6 +3090,8 @@ func TestHostClientErrConnPoolStrategyNotImpl(t *testing.T) {
}

func Test_AddMissingPort(t *testing.T) {
t.Parallel()

type args struct {
addr string
isTLS bool
Expand Down
12 changes: 0 additions & 12 deletions fasthttputil/inmemory_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
)

func TestInmemoryListener(t *testing.T) {
t.Parallel()

ln := NewInmemoryListener()

ch := make(chan struct{})
Expand Down Expand Up @@ -158,16 +156,12 @@ func testInmemoryListenerHTTPSingle(t *testing.T, client *http.Client, content s
}

func TestInmemoryListenerHTTPSingle(t *testing.T) {
t.Parallel()

testInmemoryListenerHTTP(t, func(t *testing.T, client *http.Client) {
testInmemoryListenerHTTPSingle(t, client, "request")
})
}

func TestInmemoryListenerHTTPSerial(t *testing.T) {
t.Parallel()

testInmemoryListenerHTTP(t, func(t *testing.T, client *http.Client) {
for i := 0; i < 10; i++ {
testInmemoryListenerHTTPSingle(t, client, fmt.Sprintf("request_%d", i))
Expand All @@ -176,8 +170,6 @@ func TestInmemoryListenerHTTPSerial(t *testing.T) {
}

func TestInmemoryListenerHTTPConcurrent(t *testing.T) {
t.Parallel()

testInmemoryListenerHTTP(t, func(t *testing.T, client *http.Client) {
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
Expand All @@ -203,8 +195,6 @@ func acceptLoop(ln net.Listener) {
}

func TestInmemoryListenerAddrDefault(t *testing.T) {
t.Parallel()

ln := NewInmemoryListener()

verifyAddr(t, ln.Addr(), inmemoryAddr(0))
Expand Down Expand Up @@ -244,8 +234,6 @@ func verifyAddr(t *testing.T, got, expected net.Addr) {
}

func TestInmemoryListenerAddrCustom(t *testing.T) {
t.Parallel()

ln := NewInmemoryListener()

listenerAddr := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 12345}
Expand Down
10 changes: 9 additions & 1 deletion http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3008,6 +3008,8 @@ func TestResponseBodyStream(t *testing.T) {
}
})
t.Run("http client", func(t *testing.T) {
t.Parallel()

server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
if request.URL.Query().Get("chunked") == "true" {
for x := 0; x < 10; x++ {
Expand All @@ -3020,9 +3022,11 @@ func TestResponseBodyStream(t *testing.T) {
writer.Write([]byte(`hello world`)) //nolint:errcheck
}))

defer server.Close()
t.Cleanup(server.Close)

t.Run("normal request", func(t *testing.T) {
t.Parallel()

client := Client{StreamResponseBody: true}
resp := AcquireResponse()
request := AcquireRequest()
Expand All @@ -3041,6 +3045,8 @@ func TestResponseBodyStream(t *testing.T) {
})

t.Run("limit response body size size", func(t *testing.T) {
t.Parallel()

client := Client{StreamResponseBody: true, MaxResponseBodySize: 20}
resp := AcquireResponse()
request := AcquireRequest()
Expand All @@ -3061,6 +3067,8 @@ func TestResponseBodyStream(t *testing.T) {
})

t.Run("chunked", func(t *testing.T) {
t.Parallel()

client := Client{StreamResponseBody: true}
resp := AcquireResponse()
request := AcquireRequest()
Expand Down
19 changes: 10 additions & 9 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3632,21 +3632,22 @@ func TestShutdownCloseIdleConns(t *testing.T) {
}

func TestShutdownWithContext(t *testing.T) {
t.Parallel()

done := make(chan struct{})
ln := fasthttputil.NewInmemoryListener()
s := &Server{
Handler: func(ctx *RequestCtx) {
time.Sleep(5 * time.Second)
time.Sleep(4 * time.Second)
ctx.Success("aaa/bbb", []byte("real response"))
close(done)
},
}
go func() {
if err := s.Serve(ln); err != nil {
t.Errorf("unexpected error: %v", err)
}
}()
time.Sleep(1 * time.Second)

time.Sleep(1 * time.Millisecond * 500)
go func() {
conn, err := ln.Dial()
if err != nil {
Expand All @@ -3660,8 +3661,8 @@ func TestShutdownWithContext(t *testing.T) {
verifyResponse(t, br, StatusOK, "aaa/bbb", "real response")
}()

time.Sleep(1 * time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
time.Sleep(1 * time.Millisecond * 500)
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
shutdownErr := make(chan error)
go func() {
Expand All @@ -3677,9 +3678,10 @@ func TestShutdownWithContext(t *testing.T) {
t.Fatalf("unexpected err %v. Expecting %v", err, context.DeadlineExceeded)
}
}
if atomic.LoadInt32(&s.open) != 1 {
t.Fatalf("unexpected open connection num: %#v. Expecting %#v", atomic.LoadInt32(&s.open), 1)
if o := atomic.LoadInt32(&s.open); o != 1 {
t.Fatalf("unexpected open connection num: %#v. Expecting %#v", o, 1)
}
<-done
}

func TestMultipleServe(t *testing.T) {
Expand Down Expand Up @@ -3859,7 +3861,6 @@ func TestStreamRequestBodyExceedMaxSize(t *testing.T) {
}

func TestStreamBodyRequestContentLength(t *testing.T) {
t.Parallel()
content := strings.Repeat("1", 1<<15) // 32K
contentLength := len(content)

Expand Down

0 comments on commit fa72f3c

Please sign in to comment.