(t *testing.T)
| 200 | } |
| 201 | |
| 202 | func TestThrottleRetryAfter(t *testing.T) { |
| 203 | r := chi.NewRouter() |
| 204 | retryAfterFn := func(ctxDone bool) time.Duration { return time.Hour } |
| 205 | |
| 206 | r.Use(ThrottleWithOpts(ThrottleOpts{ |
| 207 | Limit: 5, |
| 208 | BacklogLimit: 0, |
| 209 | RetryAfterFn: retryAfterFn, |
| 210 | })) |
| 211 | |
| 212 | r.Get("/", func(w http.ResponseWriter, r *http.Request) { |
| 213 | time.Sleep(time.Second * 1) // Expensive operation. |
| 214 | w.WriteHeader(http.StatusOK) |
| 215 | w.Write([]byte("ok")) |
| 216 | }) |
| 217 | |
| 218 | server := httptest.NewServer(r) |
| 219 | defer server.Close() |
| 220 | client := http.Client{} |
| 221 | |
| 222 | type result struct { |
| 223 | status int |
| 224 | header http.Header |
| 225 | } |
| 226 | |
| 227 | var wg sync.WaitGroup |
| 228 | totalRequests := 10 |
| 229 | resultsCh := make(chan result, totalRequests) |
| 230 | |
| 231 | for i := 0; i < totalRequests; i++ { |
| 232 | wg.Add(1) |
| 233 | go func() { |
| 234 | defer wg.Done() |
| 235 | res, _ := client.Get(server.URL) |
| 236 | resultsCh <- result{status: res.StatusCode, header: res.Header} |
| 237 | }() |
| 238 | } |
| 239 | |
| 240 | wg.Wait() |
| 241 | close(resultsCh) |
| 242 | |
| 243 | count200 := 0 |
| 244 | count429 := 0 |
| 245 | for res := range resultsCh { |
| 246 | switch res.status { |
| 247 | case http.StatusOK: |
| 248 | count200++ |
| 249 | continue |
| 250 | case http.StatusTooManyRequests: |
| 251 | count429++ |
| 252 | assertEqual(t, "3600", res.header.Get("Retry-After")) |
| 253 | continue |
| 254 | default: |
| 255 | t.Fatalf("Unexpected status code: %d", res.status) |
| 256 | continue |
| 257 | } |
| 258 | } |
| 259 |
nothing calls this directly
no test coverage detected