Skip to content

Commit 32ae9ad

Browse files
Add test which counts number of retries.
Signed-off-by: Spencer Schrock <sschrock@google.com>
1 parent 786f3db commit 32ae9ad

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

signing/signing_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,71 @@ func TestProcessSignature(t *testing.T) {
128128
}
129129
}
130130

131+
//nolint:paralleltest // we are using t.Setenv
132+
func TestProcessSignature_retries(t *testing.T) {
133+
tests := []struct {
134+
name string
135+
nFailures int
136+
wantNRequests int
137+
wantErr bool
138+
}{
139+
{
140+
name: "succeeds immediately",
141+
nFailures: 0,
142+
wantNRequests: 1,
143+
wantErr: false,
144+
},
145+
{
146+
name: "one retry",
147+
nFailures: 1,
148+
wantNRequests: 2,
149+
wantErr: false,
150+
},
151+
{
152+
// limit corresponds to backoffs set in test body
153+
name: "retry limit exceeded",
154+
nFailures: 4,
155+
wantNRequests: 3,
156+
wantErr: true,
157+
},
158+
}
159+
// use smaller backoffs for the test so they run faster
160+
setBackoffs(t, []time.Duration{0, time.Millisecond, 2 * time.Millisecond})
161+
for _, tt := range tests {
162+
t.Run(tt.name, func(t *testing.T) {
163+
var jsonPayload []byte
164+
repoName := "ossf-tests/scorecard-action"
165+
repoRef := "refs/heads/main"
166+
//nolint:gosec // dummy credentials
167+
accessToken := "ghs_foo"
168+
var nRequests int
169+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
170+
nRequests++
171+
status := http.StatusCreated
172+
if tt.nFailures > 0 {
173+
status = http.StatusBadRequest
174+
tt.nFailures--
175+
}
176+
w.WriteHeader(status)
177+
}))
178+
t.Setenv(options.EnvInputInternalPublishBaseURL, server.URL)
179+
t.Cleanup(server.Close)
180+
181+
s, err := New(accessToken)
182+
if err != nil {
183+
t.Fatalf("Unexpected error New: %v", err)
184+
}
185+
err = s.ProcessSignature(jsonPayload, repoName, repoRef)
186+
if (err != nil) != tt.wantErr {
187+
t.Errorf("ProcessSignature() error: %v, wantErr: %v", err, tt.wantErr)
188+
}
189+
if nRequests != tt.wantNRequests {
190+
t.Errorf("ProcessSignature() made %d requests, wanted %d", nRequests, tt.wantNRequests)
191+
}
192+
})
193+
}
194+
}
195+
131196
// temporarily sets the backoffs for a given test.
132197
func setBackoffs(t *testing.T, newBackoffs []time.Duration) {
133198
t.Helper()

0 commit comments

Comments
 (0)