Add tests

Signed-off-by: Maartje Eyskens <maartje@eyskens.me>
This commit is contained in:
Maartje Eyskens 2020-11-06 17:16:56 +01:00
parent 7d90fae6e4
commit f6e2d48a42
2 changed files with 73 additions and 1 deletions

View File

@ -31,7 +31,7 @@ func RetryBackoff(n int, r *http.Request, resp *http.Response) time.Duration {
// However, we can not use the request body in here as it is closed already.
// So we're using it's status code instead: 400
if resp.StatusCode == http.StatusBadRequest {
// don't retry more than 5 times, if we get 5 nonce mismatches something is quite wrong
// don't retry more than 6 times, if we get 6 nonce mismatches something is quite wrong
if n > 5 {
return -1
} else if n < 1 {

View File

@ -0,0 +1,72 @@
package util
import (
"net/http"
"testing"
"time"
)
func TestRetryBackoff(t *testing.T) {
type args struct {
n int
r *http.Request
resp *http.Response
}
tests := []struct {
name string
args args
validdateOutput func(time.Duration) bool
}{
{
name: "Do not retry a non 400 error",
args: args{
n: 0,
r: &http.Request{},
resp: &http.Response{StatusCode: http.StatusUnauthorized},
},
validdateOutput: func(duration time.Duration) bool {
return duration == -1
},
},
{
name: "Retry a 400 error when the first time",
args: args{
n: 0,
r: &http.Request{},
resp: &http.Response{StatusCode: http.StatusBadRequest},
},
validdateOutput: func(duration time.Duration) bool {
return duration > 0
},
},
{
name: "Retry a 400 error when when less than 6 times",
args: args{
n: 5,
r: &http.Request{},
resp: &http.Response{StatusCode: http.StatusBadRequest},
},
validdateOutput: func(duration time.Duration) bool {
return duration > 0
},
},
{
name: "Do not retry a 400 error after 6 tries",
args: args{
n: 6,
r: &http.Request{},
resp: &http.Response{StatusCode: http.StatusBadRequest},
},
validdateOutput: func(duration time.Duration) bool {
return duration == -1
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RetryBackoff(tt.args.n, tt.args.r, tt.args.resp); !tt.validdateOutput(got) {
t.Errorf("RetryBackoff() = %v which is not valid according to the validdateOutput()", got)
}
})
}
}