@@ -418,3 +418,39 @@ func TestPaginationPopulatePageValuesKeyset(t *testing.T) {
418418 }
419419 }
420420}
421+
422+ func TestExponentialBackoffLogic (t * testing.T ) {
423+ // Can't use the default `setup` because it disabled the backoff
424+ mux := http .NewServeMux ()
425+ server := httptest .NewServer (mux )
426+ t .Cleanup (server .Close )
427+ client , err := NewClient ("" ,
428+ WithBaseURL (server .URL ),
429+ )
430+ if err != nil {
431+ t .Fatalf ("Failed to create client: %v" , err )
432+ }
433+
434+ // Create a method that returns 429
435+ mux .HandleFunc ("/api/v4/projects/1" , func (w http.ResponseWriter , r * http.Request ) {
436+ testMethod (t , r , http .MethodGet )
437+ w .WriteHeader (http .StatusTooManyRequests )
438+ })
439+
440+ // Measure the time at the start of the test
441+ start := time .Now ()
442+
443+ // Send a request (which will get a bunch of 429s)
444+ // None of the responses matter, so ignore them all
445+ _ , resp , _ := client .Projects .GetProject (1 , nil )
446+ end := time .Now ()
447+
448+ // The test should run for _at least_ 3,200 milliseconds
449+ duration := float64 (end .Sub (start ))
450+ if duration < float64 (3200 * time .Millisecond ) {
451+ t .Fatal ("Wait was shorter than expected. Expected a minimum of 5 retries taking 3200 milliseconds, got:" , duration )
452+ }
453+ if resp .StatusCode != 429 {
454+ t .Fatal ("Expected to get a 429 code given the server is hard-coded to return this. Received instead:" , resp .StatusCode )
455+ }
456+ }
0 commit comments