-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from Pix4D/log-retry-reason
GitHub: log retry reason
- Loading branch information
Showing
5 changed files
with
121 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package github | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func TestCheckForRetrySuccess(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
res httpResponse | ||
waitTime time.Duration | ||
maxSleepTime time.Duration | ||
jitter time.Duration | ||
wantSleep time.Duration | ||
wantReason string | ||
} | ||
|
||
run := func(t *testing.T, tc testCase) { | ||
sleep, reason, err := checkForRetry(tc.res, tc.waitTime, tc.maxSleepTime, tc.jitter) | ||
|
||
assert.NilError(t, err) | ||
assert.Equal(t, sleep, tc.wantSleep) | ||
assert.Equal(t, reason, tc.wantReason) | ||
} | ||
|
||
serverDate := time.Date(2001, time.April, 30, 13, 0, 0, 0, time.UTC) | ||
const maxSleepTime = 15 * time.Minute | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "status OK: sleep==0", | ||
res: httpResponse{statusCode: http.StatusOK}, | ||
wantSleep: 0 * time.Second, | ||
}, | ||
{ | ||
name: "non retryable status code: sleep==0", | ||
res: httpResponse{statusCode: http.StatusTeapot}, | ||
wantSleep: 0 * time.Second, | ||
}, | ||
{ | ||
name: "retryable status code: sleep==waitTime", | ||
res: httpResponse{statusCode: http.StatusInternalServerError}, | ||
waitTime: 42 * time.Second, | ||
wantSleep: 42 * time.Second, | ||
wantReason: "Internal Server Error", | ||
}, | ||
{ | ||
name: "rate limited, would sleep too long: sleep==0", | ||
res: httpResponse{ | ||
statusCode: http.StatusForbidden, | ||
date: serverDate, | ||
rateLimitReset: serverDate.Add(30 * time.Minute), | ||
}, | ||
maxSleepTime: maxSleepTime, | ||
wantSleep: 0 * time.Second, | ||
}, | ||
{ | ||
name: "rate limited, would sleep a bit, adding also the jitter", | ||
res: httpResponse{ | ||
statusCode: http.StatusForbidden, | ||
date: serverDate, | ||
rateLimitReset: serverDate.Add(5 * time.Minute), | ||
}, | ||
maxSleepTime: maxSleepTime, | ||
jitter: 8 * time.Second, | ||
wantSleep: 5*time.Minute + 8*time.Second, | ||
wantReason: "rate limited", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { run(t, tc) }) | ||
} | ||
} | ||
|
||
// BUG: sleeptime + jitter might cause a failure; test sleepTime > maxSleepTime should | ||
// be done before? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters