(t *testing.T)
| 181 | } |
| 182 | |
| 183 | func TestRateLimitedTransport(t *testing.T) { |
| 184 | var requestCount atomic.Int32 |
| 185 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 186 | requestCount.Add(1) |
| 187 | w.WriteHeader(http.StatusOK) |
| 188 | })) |
| 189 | defer ts.Close() |
| 190 | |
| 191 | transport := &rateLimitedTransport{ |
| 192 | transport: http.DefaultTransport, |
| 193 | limiter: rate.NewLimiter(10, 5), |
| 194 | } |
| 195 | client := &http.Client{Transport: transport} |
| 196 | |
| 197 | // Make 10 requests |
| 198 | start := time.Now() |
| 199 | numRequests := 10 |
| 200 | for i := range numRequests { |
| 201 | req, _ := http.NewRequest("GET", ts.URL, nil) |
| 202 | resp, err := client.Do(req) |
| 203 | if err != nil { |
| 204 | t.Fatalf("Request %d failed: %v", i, err) |
| 205 | } |
| 206 | resp.Body.Close() |
| 207 | } |
| 208 | elapsed := time.Since(start) |
| 209 | |
| 210 | // Burst of 5, then 5 more at 10 req/s = 500ms minimum |
| 211 | if elapsed < 500*time.Millisecond { |
| 212 | t.Errorf("Rate limit not enforced: 10 requests took %v, expected >= 500ms", elapsed) |
| 213 | } |
| 214 | |
| 215 | assert.Equal(t, int(requestCount.Load()), 10, "request count mismatch") |
| 216 | } |
| 217 | |
| 218 | func TestHTTPError(t *testing.T) { |
| 219 | t.Run("IsConflict returns true for 409", func(t *testing.T) { |
nothing calls this directly
no test coverage detected