Skip to content

Commit be5d0ad

Browse files
committed
Cleanup BackOff logic
1 parent ef85e2d commit be5d0ad

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

pkg/client/util/http_backoff.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
)
99

1010
// This is a custom Backoff that enforces the Max wait duration.
11-
// If the sleep is greater we refuse to sleep at all
11+
// If the sleep is greater we only sleep for the max duration.
1212
func HTTPBackOff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
1313
sleep := retryablehttp.DefaultBackoff(min, max, attemptNum, resp)
14-
if sleep.Abs() <= max {
15-
return sleep.Abs()
14+
if sleep.Abs() >= max {
15+
return max.Abs()
1616
}
1717

18-
return max.Abs()
18+
return sleep.Abs()
1919
}

pkg/client/util/http_backoff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestHTTPBackOff(t *testing.T) {
3333
attemptNum: 10,
3434
resp: &http.Response{
3535
StatusCode: http.StatusTooManyRequests,
36-
Header: http.Header{"Retry-After": []string{"3600"}}},
36+
Header: http.Header{"Retry-After": []string{"484289h20m8s"}}},
3737
expSleep: 500 * time.Millisecond,
3838
},
3939
{

pkg/controller/options/options_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import (
55
"regexp"
66
"testing"
77

8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
811
"github.com/jetstack/version-checker/pkg/api"
912
)
1013

@@ -135,20 +138,16 @@ func TestBuild(t *testing.T) {
135138
for name, test := range tests {
136139
t.Run(name, func(t *testing.T) {
137140
options, err := New(test.annotations).Options(test.containerName)
141+
138142
if len(test.expErr) > 0 {
139-
if err == nil || err.Error() != test.expErr {
140-
t.Errorf("unexpected error, exp=%s got=%v",
141-
test.expErr, err)
142-
}
143-
} else if err != nil {
144-
t.Errorf("unexpected error, exp=%s got=%v",
145-
test.expErr, err)
146-
}
143+
assert.Error(t, err)
144+
assert.Equal(t, test.expErr, err.Error())
147145

148-
if !reflect.DeepEqual(test.expOptions, options) {
149-
t.Errorf("unexpected options for %s=%v exp=%#+v got=%#+v",
150-
test.containerName, test.annotations, test.expOptions, options)
146+
} else {
147+
require.NoError(t, err)
151148
}
149+
150+
assert.Exactly(t, test.expOptions, options)
152151
})
153152
}
154153
}

0 commit comments

Comments
 (0)