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

Function TestDo_rateLimit_abortSleepContextCancelled

github/github_test.go:2756–2789  ·  view source on GitHub ↗

Ensure sleep is aborted when the context is cancelled.

(t *testing.T)

Source from the content-addressed store, hash-verified

2754
2755// Ensure sleep is aborted when the context is cancelled.
2756func TestDo_rateLimit_abortSleepContextCancelled(t *testing.T) {
2757 t.Parallel()
2758 client, mux, _ := setup(t)
2759
2760 // We use a 1 minute reset time to ensure the sleep is not completed.
2761 reset := time.Now().UTC().Add(time.Minute)
2762 var requestCount atomic.Int32
2763 mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
2764 requestCount.Add(1)
2765 w.Header().Set(HeaderRateLimit, "60")
2766 w.Header().Set(HeaderRateRemaining, "0")
2767 w.Header().Set(HeaderRateUsed, "60")
2768 w.Header().Set(HeaderRateReset, fmt.Sprint(reset.Unix()))
2769 w.Header().Set(HeaderRateResource, "core")
2770 w.Header().Set("Content-Type", "application/json; charset=utf-8")
2771 w.WriteHeader(http.StatusForbidden)
2772 fmt.Fprintln(w, `{
2773 "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.)",
2774 "documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
2775}`)
2776 })
2777
2778 ctx, cancel := context.WithTimeout(t.Context(), 10*time.Millisecond)
2779 defer cancel()
2780
2781 req, _ := client.NewRequest(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), "GET", ".", nil)
2782 _, err := client.Do(req, nil)
2783 if !errors.Is(err, context.DeadlineExceeded) {
2784 t.Error("Expected context deadline exceeded error.")
2785 }
2786 if got, want := int(requestCount.Load()), 1; got != want {
2787 t.Errorf("Expected 1 requests, got %v", got)
2788 }
2789}
2790
2791// Ensure sleep is aborted when the context is cancelled on initial request.
2792func TestDo_rateLimit_abortSleepContextCancelledClientLimit(t *testing.T) {

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…