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

Function TestDo_rateLimit_sleepUntilClientResetLimit

github/github_test.go:2724–2753  ·  view source on GitHub ↗

Ensure a network call is not made when it's known that API rate limit is still exceeded.

(t *testing.T)

Source from the content-addressed store, hash-verified

2722
2723// Ensure a network call is not made when it's known that API rate limit is still exceeded.
2724func TestDo_rateLimit_sleepUntilClientResetLimit(t *testing.T) {
2725 t.Parallel()
2726 client, mux, _ := setup(t)
2727
2728 reset := time.Now().UTC().Add(time.Second)
2729 client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
2730 var requestCount atomic.Int32
2731 mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
2732 requestCount.Add(1)
2733 w.Header().Set(HeaderRateLimit, "5000")
2734 w.Header().Set(HeaderRateRemaining, "5000")
2735 w.Header().Set(HeaderRateUsed, "0")
2736 w.Header().Set(HeaderRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
2737 w.Header().Set(HeaderRateResource, "core")
2738 w.Header().Set("Content-Type", "application/json; charset=utf-8")
2739 w.WriteHeader(http.StatusOK)
2740 fmt.Fprintln(w, `{}`)
2741 })
2742 req, _ := client.NewRequest(context.WithValue(t.Context(), SleepUntilPrimaryRateLimitResetWhenRateLimited, true), "GET", ".", nil)
2743 resp, err := client.Do(req, nil)
2744 if err != nil {
2745 t.Errorf("Do returned unexpected error: %v", err)
2746 }
2747 if got, want := resp.StatusCode, http.StatusOK; got != want {
2748 t.Errorf("Response status code = %v, want %v", got, want)
2749 }
2750 if got, want := int(requestCount.Load()), 1; got != want {
2751 t.Errorf("Expected 1 request, got %v", got)
2752 }
2753}
2754
2755// Ensure sleep is aborted when the context is cancelled.
2756func TestDo_rateLimit_abortSleepContextCancelled(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…