Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/bridge/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (c *Client) doWithBackoff(
headers.Set("User-Agent", "IVO/"+c.Version)
url := c.BaseURL.JoinPath(path).String()

err := wait.ExponentialBackoff(c.Backoff, func() (bool, error) {
err := wait.ExponentialBackoffWithContext(ctx, c.Backoff, func() (bool, error) {
// NOTE: The [net/http] package treats an empty [bytes.Reader] the same as nil.
request, err := http.NewRequestWithContext(ctx, method, url, bytes.NewReader(body))

Expand Down
21 changes: 21 additions & 0 deletions internal/bridge/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,27 @@ func TestClientDoWithBackoff(t *testing.T) {
assert.Assert(t, requests > 0, "expected multiple requests")
})

t.Run("CancellationDuringBackoff", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
t.Cleanup(server.Close)

client := NewClient(server.URL, "")
client.Backoff.Duration = 3 * time.Second // nolint:staticcheck
client.Backoff.Jitter = 0 // nolint:staticcheck
client.Backoff.Steps = 2 // nolint:staticcheck

ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
t.Cleanup(cancel)

started := time.Now()
_, err := client.doWithBackoff(ctx, "POST", "/any", nil, nil) //nolint:bodyclose
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.Assert(t, time.Since(started) < time.Second,
"cancellation must interrupt the backoff delay")
})

t.Run("Cancellation", func(t *testing.T) {
requests := 0
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down