|
| 1 | +package retryhttp |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "sync/atomic" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// server answers each request with the next status in codes (the last one |
| 14 | +// repeats), counting the requests, and records the waits Get asks for. |
| 15 | +func server(t *testing.T, header http.Header, codes ...int) (url string, hits *atomic.Int32, waits *[]time.Duration) { |
| 16 | + t.Helper() |
| 17 | + hits = new(atomic.Int32) |
| 18 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 19 | + n := int(hits.Add(1)) - 1 |
| 20 | + for k, v := range header { |
| 21 | + w.Header()[k] = v |
| 22 | + } |
| 23 | + w.WriteHeader(codes[min(n, len(codes)-1)]) |
| 24 | + })) |
| 25 | + t.Cleanup(srv.Close) |
| 26 | + waits = new([]time.Duration) |
| 27 | + saved := Sleep |
| 28 | + t.Cleanup(func() { Sleep = saved }) |
| 29 | + Sleep = func(_ context.Context, d time.Duration) error { |
| 30 | + *waits = append(*waits, d) |
| 31 | + return nil |
| 32 | + } |
| 33 | + return srv.URL, hits, waits |
| 34 | +} |
| 35 | + |
| 36 | +func get(ctx context.Context, url string) (*http.Response, error) { |
| 37 | + return Get(ctx, http.DefaultClient, "test lookup", func(ctx context.Context) (*http.Request, error) { |
| 38 | + return http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func TestGetRetriesBusyAnswers(t *testing.T) { |
| 43 | + url, hits, waits := server(t, nil, http.StatusServiceUnavailable, http.StatusBadGateway, http.StatusOK) |
| 44 | + resp, err := get(context.Background(), url) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("Get: %v", err) |
| 47 | + } |
| 48 | + _ = resp.Body.Close() |
| 49 | + if hits.Load() != 3 || len(*waits) != 2 || (*waits)[0] != backoff[0] || (*waits)[1] != backoff[1] { |
| 50 | + t.Errorf("%d requests, waits %v; want 3 requests with waits %v", hits.Load(), *waits, backoff) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestGetGivesUpAfterRetries(t *testing.T) { |
| 55 | + url, hits, _ := server(t, nil, http.StatusTooManyRequests) |
| 56 | + if _, err := get(context.Background(), url); err == nil || !strings.Contains(err.Error(), "429") || !strings.HasPrefix(err.Error(), "test lookup: ") { |
| 57 | + t.Errorf("err = %v, want the last 429 named", err) |
| 58 | + } |
| 59 | + if hits.Load() != Retries+1 { |
| 60 | + t.Errorf("%d requests, want %d", hits.Load(), Retries+1) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestGetDoesNotRetryOtherStatuses(t *testing.T) { |
| 65 | + url, hits, _ := server(t, nil, http.StatusNotFound, http.StatusOK) |
| 66 | + if _, err := get(context.Background(), url); err == nil || !strings.Contains(err.Error(), "404") { |
| 67 | + t.Errorf("err = %v, want the 404", err) |
| 68 | + } |
| 69 | + if hits.Load() != 1 { |
| 70 | + t.Errorf("a 404 was retried: %d requests", hits.Load()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestGetHonoursRetryAfter(t *testing.T) { |
| 75 | + url, _, waits := server(t, http.Header{"Retry-After": {"7"}}, http.StatusTooManyRequests, http.StatusOK) |
| 76 | + resp, err := get(context.Background(), url) |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + _ = resp.Body.Close() |
| 81 | + if len(*waits) != 1 || (*waits)[0] != 7*time.Second { |
| 82 | + t.Errorf("waits = %v, want [7s]", *waits) |
| 83 | + } |
| 84 | + |
| 85 | + url, hits, waits := server(t, http.Header{"Retry-After": {"3600"}}, http.StatusTooManyRequests, http.StatusOK) |
| 86 | + resp, err = get(context.Background(), url) |
| 87 | + if err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + _ = resp.Body.Close() |
| 91 | + if hits.Load() != 2 || (*waits)[0] != maxWait { |
| 92 | + t.Errorf("a long Retry-After: waits %v, want capped at %v", *waits, maxWait) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestGetStopsAtTheDeadline(t *testing.T) { |
| 97 | + url, hits, _ := server(t, nil, http.StatusServiceUnavailable, http.StatusOK) |
| 98 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) // shorter than the first backoff |
| 99 | + defer cancel() |
| 100 | + if _, err := get(ctx, url); err == nil { |
| 101 | + t.Error("Get retried past its deadline") |
| 102 | + } |
| 103 | + if hits.Load() != 1 { |
| 104 | + t.Errorf("%d requests, want 1: the retry could not finish in time", hits.Load()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestRetryAfter(t *testing.T) { |
| 109 | + now := time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC) |
| 110 | + for v, want := range map[string]time.Duration{ |
| 111 | + "": 0, |
| 112 | + "12": 12 * time.Second, |
| 113 | + "-3": 0, |
| 114 | + "soon": 0, |
| 115 | + "Fri, 13 Nov 2026 12:00:10 GMT": 10 * time.Second, |
| 116 | + } { |
| 117 | + if got := retryAfter(v, now); got != want { |
| 118 | + t.Errorf("retryAfter(%q) = %v, want %v", v, got, want) |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments