Skip to content

Commit

Permalink
fix: increase code coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <[email protected]>
  • Loading branch information
greut committed Sep 15, 2020
1 parent bd0d2c8 commit 9f2d72d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 22 deletions.
6 changes: 4 additions & 2 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ outter:
cancel()

multiErr.Push(request.Context().Err().Error())
c.reportError(request, err)
c.reportError(request, request.Context().Err())

// If the request context has already been cancelled, don't retry
break outter
Expand All @@ -187,12 +187,14 @@ outter:
select {
case <-ctx.Done():
cancel()

continue

case <-request.Context().Done():
cancel()

multiErr.Push(request.Context().Err().Error())
c.reportError(request, err)
c.reportError(request, request.Context().Err())

// If the request context has already been cancelled, don't retry
break outter
Expand Down
72 changes: 52 additions & 20 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ func TestHTTPClientGetReturnsErrorOnFailure(t *testing.T) {
}

func TestHTTPClientDontRetryWhenContextIsCancelled(t *testing.T) {
count := 0
noOfRetries := 3
// Set a huge backoffInterval that we won't have to wait anyway
backoffInterval := 1 * time.Hour
Expand All @@ -431,27 +430,60 @@ func TestHTTPClientDontRetryWhenContextIsCancelled(t *testing.T) {
WithRetrier(heimdall.NewRetrier(heimdall.NewConstantBackoff(backoffInterval, maximumJitterInterval))),
)

ctx, cancel := context.WithCancel(context.Background())

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{ "response": "something went wrong" }`))
count++
// Cancel the context after the first call
cancel()
tt := []struct {
Title string
CancelTimeout time.Duration
NotNilResponse bool
}{
{
Title: "Cancel directly",
CancelTimeout: 0 * time.Millisecond,
NotNilResponse: false,
},
{
Title: "Cancel afterwards",
CancelTimeout: 10 * time.Millisecond,
NotNilResponse: true,
},
}

server := httptest.NewServer(http.HandlerFunc(dummyHandler))
defer server.Close()

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
require.NoError(t, err)

response, err := client.Do(req.WithContext(ctx))
require.Error(t, err, "should have failed to make request")
require.Nil(t, response)

assert.Equal(t, 1, count)
for _, test := range tt {
test := test
t.Run(test.Title, func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithCancel(context.Background())

dummyHandler := func(w http.ResponseWriter, r *http.Request) {
if test.CancelTimeout == 0 {
cancel()
} else {
go func() {
time.Sleep(test.CancelTimeout)
cancel()
}()
}

w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(`{ "response": "something went wrong" }`))
}

server := httptest.NewServer(http.HandlerFunc(dummyHandler))
defer server.Close()

req, err := http.NewRequest(http.MethodGet, server.URL, nil)
require.NoError(t, err)

response, err := client.Do(req.WithContext(ctx))
require.Error(t, err, "should have failed to make request")

if test.NotNilResponse {
require.NotNil(t, response)
} else {
require.Nil(t, response)
}
})
}
}

func TestPluginMethodsCalled(t *testing.T) {
Expand Down

0 comments on commit 9f2d72d

Please sign in to comment.