From f6e2d48a42f4640725be3bc92aaaf25ee20b6896 Mon Sep 17 00:00:00 2001 From: Maartje Eyskens Date: Fri, 6 Nov 2020 17:16:56 +0100 Subject: [PATCH] Add tests Signed-off-by: Maartje Eyskens --- pkg/acme/util/util.go | 2 +- pkg/acme/util/util_test.go | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 pkg/acme/util/util_test.go diff --git a/pkg/acme/util/util.go b/pkg/acme/util/util.go index 66b1e3a78..9373c932a 100644 --- a/pkg/acme/util/util.go +++ b/pkg/acme/util/util.go @@ -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 { diff --git a/pkg/acme/util/util_test.go b/pkg/acme/util/util_test.go new file mode 100644 index 000000000..1a5813718 --- /dev/null +++ b/pkg/acme/util/util_test.go @@ -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) + } + }) + } +}