MCPcopy Index your code
hub / github.com/google/go-github / TestDo_rateLimit_sleepUntilResponseResetLimit

Function TestDo_rateLimit_sleepUntilResponseResetLimit

github/github_test.go:2647–2688  ·  view source on GitHub ↗

Ensure sleeps until the rate limit is reset when the client is rate limited.

(t *testing.T)

Source from the content-addressed store, hash-verified

2645
2646// Ensure sleeps until the rate limit is reset when the client is rate limited.
2647func TestDo_rateLimit_sleepUntilResponseResetLimit(t *testing.T) {
2648 t.Parallel()
2649 client, mux, _ := setup(t)
2650
2651 reset := time.Now().UTC().Add(time.Second)
2652
2653 firstRequest := true
2654 mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
2655 if firstRequest {
2656 firstRequest = false
2657 w.Header().Set(HeaderRateLimit, "60")
2658 w.Header().Set(HeaderRateRemaining, "0")
2659 w.Header().Set(HeaderRateUsed, "60")
2660 w.Header().Set(HeaderRateReset, fmt.Sprint(reset.Unix()))
2661 w.Header().Set(HeaderRateResource, "core")
2662 w.Header().Set("Content-Type", "application/json; charset=utf-8")
2663 w.WriteHeader(http.StatusForbidden)
2664 fmt.Fprintln(w, `{
2665 "message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
2666 "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
2667}`)
2668 return
2669 }
2670 w.Header().Set(HeaderRateLimit, "5000")
2671 w.Header().Set(HeaderRateRemaining, "5000")
2672 w.Header().Set(HeaderRateUsed, "0")
2673 w.Header().Set(HeaderRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
2674 w.Header().Set(HeaderRateResource, "core")
2675 w.Header().Set("Content-Type", "application/json; charset=utf-8")
2676 w.WriteHeader(http.StatusOK)
2677 fmt.Fprintln(w, `{}`)
2678 })
2679
2680 req, _ := client.NewRequest(context.WithValue(t.Context(), SleepUntilPrimaryRateLimitResetWhenRateLimited, true), "GET", ".", nil)
2681 resp, err := client.Do(req, nil)
2682 if err != nil {
2683 t.Errorf("Do returned unexpected error: %v", err)
2684 }
2685 if got, want := resp.StatusCode, http.StatusOK; got != want {
2686 t.Errorf("Response status code = %v, want %v", got, want)
2687 }
2688}
2689
2690// Ensure tries to sleep until the rate limit is reset when the client is rate limited, but only once.
2691func TestDo_rateLimit_sleepUntilResponseResetLimitRetryOnce(t *testing.T) {

Callers

nothing calls this directly

Calls 4

AddMethod · 0.80
NewRequestMethod · 0.80
setupFunction · 0.70
DoMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…