Skip to content

Commit

Permalink
proxy/backendtest: fix flaky TestServerShouldCloseWhenAllRequestsAreF…
Browse files Browse the repository at this point in the history
…ulfilled (#2928)

Wait for test requests to complete before closing the recorder.

Signed-off-by: Alexander Yastrebov <[email protected]>
  • Loading branch information
AlexanderYastrebov authored Feb 12, 2024
1 parent 1599987 commit 7475c6e
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions proxy/backendtest/backendrecorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@ package backendtest
import (
"io"
"net/http"
"strconv"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)

func TestServerShouldCloseWhenAllRequestsAreFulfilled(t *testing.T) {
expectedRequests := 4
recorder := NewBackendRecorder(10 * time.Millisecond)

var g errgroup.Group
for i := 0; i < expectedRequests; i++ {
go func(counter int) {
resp, err := http.Get(recorder.GetURL() + "/" + strconv.Itoa(counter))
g.Go(func() error {
resp, err := http.Get(recorder.GetURL())
if err != nil {
t.Error(err)
return
return err
}
if _, err := io.Copy(io.Discard, resp.Body); err != nil {
return err
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}(i)
return resp.Body.Close()
})
}
require.NoError(t, g.Wait())
recorder.Done()
servedRequests := len(recorder.GetRequests())
if servedRequests != expectedRequests {
t.Errorf("number of requests served does not match with the expected. Expected %d but got %d", expectedRequests, servedRequests)
}

assert.Equal(t, expectedRequests, len(recorder.GetRequests()))
}

0 comments on commit 7475c6e

Please sign in to comment.