Skip to content

Commit 4e5486e

Browse files
authored
fix: Synchronize requestCount in rate limit tests (#4124)
1 parent 8f1e513 commit 4e5486e

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

github/github_test.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"strconv"
2222
"strings"
23+
"sync/atomic"
2324
"testing"
2425
"testing/synctest"
2526
"time"
@@ -1694,9 +1695,9 @@ func TestDo_rateLimit_sleepUntilResponseResetLimitRetryOnce(t *testing.T) {
16941695

16951696
reset := time.Now().UTC().Add(time.Second)
16961697

1697-
requestCount := 0
1698+
var requestCount atomic.Int32
16981699
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1699-
requestCount++
1700+
requestCount.Add(1)
17001701
w.Header().Set(HeaderRateLimit, "60")
17011702
w.Header().Set(HeaderRateRemaining, "0")
17021703
w.Header().Set(HeaderRateUsed, "60")
@@ -1715,7 +1716,7 @@ func TestDo_rateLimit_sleepUntilResponseResetLimitRetryOnce(t *testing.T) {
17151716
if err == nil {
17161717
t.Error("Expected error to be returned.")
17171718
}
1718-
if got, want := requestCount, 2; got != want {
1719+
if got, want := int(requestCount.Load()), 2; got != want {
17191720
t.Errorf("Expected 2 requests, got %v", got)
17201721
}
17211722
}
@@ -1727,9 +1728,9 @@ func TestDo_rateLimit_sleepUntilClientResetLimit(t *testing.T) {
17271728

17281729
reset := time.Now().UTC().Add(time.Second)
17291730
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
1730-
requestCount := 0
1731+
var requestCount atomic.Int32
17311732
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1732-
requestCount++
1733+
requestCount.Add(1)
17331734
w.Header().Set(HeaderRateLimit, "5000")
17341735
w.Header().Set(HeaderRateRemaining, "5000")
17351736
w.Header().Set(HeaderRateUsed, "0")
@@ -1747,7 +1748,7 @@ func TestDo_rateLimit_sleepUntilClientResetLimit(t *testing.T) {
17471748
if got, want := resp.StatusCode, http.StatusOK; got != want {
17481749
t.Errorf("Response status code = %v, want %v", got, want)
17491750
}
1750-
if got, want := requestCount, 1; got != want {
1751+
if got, want := int(requestCount.Load()), 1; got != want {
17511752
t.Errorf("Expected 1 request, got %v", got)
17521753
}
17531754
}
@@ -1759,9 +1760,9 @@ func TestDo_rateLimit_abortSleepContextCancelled(t *testing.T) {
17591760

17601761
// We use a 1 minute reset time to ensure the sleep is not completed.
17611762
reset := time.Now().UTC().Add(time.Minute)
1762-
requestCount := 0
1763+
var requestCount atomic.Int32
17631764
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1764-
requestCount++
1765+
requestCount.Add(1)
17651766
w.Header().Set(HeaderRateLimit, "60")
17661767
w.Header().Set(HeaderRateRemaining, "0")
17671768
w.Header().Set(HeaderRateUsed, "60")
@@ -1782,7 +1783,7 @@ func TestDo_rateLimit_abortSleepContextCancelled(t *testing.T) {
17821783
if !errors.Is(err, context.DeadlineExceeded) {
17831784
t.Error("Expected context deadline exceeded error.")
17841785
}
1785-
if got, want := requestCount, 1; got != want {
1786+
if got, want := int(requestCount.Load()), 1; got != want {
17861787
t.Errorf("Expected 1 requests, got %v", got)
17871788
}
17881789
}
@@ -1794,9 +1795,9 @@ func TestDo_rateLimit_abortSleepContextCancelledClientLimit(t *testing.T) {
17941795

17951796
reset := time.Now().UTC().Add(time.Minute)
17961797
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
1797-
requestCount := 0
1798+
var requestCount atomic.Int32
17981799
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
1799-
requestCount++
1800+
requestCount.Add(1)
18001801
w.Header().Set(HeaderRateLimit, "5000")
18011802
w.Header().Set(HeaderRateRemaining, "5000")
18021803
w.Header().Set(HeaderRateUsed, "0")
@@ -1817,7 +1818,7 @@ func TestDo_rateLimit_abortSleepContextCancelledClientLimit(t *testing.T) {
18171818
if got, wantSuffix := rateLimitError.Message, "Context cancelled while waiting for rate limit to reset until"; !strings.HasPrefix(got, wantSuffix) {
18181819
t.Errorf("Expected request to be prevented because context cancellation, got: %v.", got)
18191820
}
1820-
if got, want := requestCount, 0; got != want {
1821+
if got, want := int(requestCount.Load()), 0; got != want {
18211822
t.Errorf("Expected 1 requests, got %v", got)
18221823
}
18231824
}
@@ -2047,9 +2048,9 @@ func TestDo_rateLimit_disableRateLimitCheck(t *testing.T) {
20472048

20482049
reset := time.Now().UTC().Add(60 * time.Second)
20492050
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
2050-
requestCount := 0
2051+
var requestCount atomic.Int32
20512052
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
2052-
requestCount++
2053+
requestCount.Add(1)
20532054
w.Header().Set(HeaderRateLimit, "5000")
20542055
w.Header().Set(HeaderRateRemaining, "5000")
20552056
w.Header().Set(HeaderRateUsed, "0")
@@ -2067,7 +2068,7 @@ func TestDo_rateLimit_disableRateLimitCheck(t *testing.T) {
20672068
if got, want := resp.StatusCode, http.StatusOK; got != want {
20682069
t.Errorf("Response status code = %v, want %v", got, want)
20692070
}
2070-
if got, want := requestCount, 1; got != want {
2071+
if got, want := int(requestCount.Load()), 1; got != want {
20712072
t.Errorf("Expected 1 request, got %v", got)
20722073
}
20732074
if got, want := client.rateLimits[CoreCategory].Remaining, 0; got != want {
@@ -2082,9 +2083,9 @@ func TestDo_rateLimit_bypassRateLimitCheck(t *testing.T) {
20822083

20832084
reset := time.Now().UTC().Add(60 * time.Second)
20842085
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
2085-
requestCount := 0
2086+
var requestCount atomic.Int32
20862087
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
2087-
requestCount++
2088+
requestCount.Add(1)
20882089
w.Header().Set(HeaderRateLimit, "5000")
20892090
w.Header().Set(HeaderRateRemaining, "5000")
20902091
w.Header().Set(HeaderRateUsed, "0")
@@ -2102,7 +2103,7 @@ func TestDo_rateLimit_bypassRateLimitCheck(t *testing.T) {
21022103
if got, want := resp.StatusCode, http.StatusOK; got != want {
21032104
t.Errorf("Response status code = %v, want %v", got, want)
21042105
}
2105-
if got, want := requestCount, 1; got != want {
2106+
if got, want := int(requestCount.Load()), 1; got != want {
21062107
t.Errorf("Expected 1 request, got %v", got)
21072108
}
21082109
if got, want := client.rateLimits[CoreCategory].Remaining, 5000; got != want {

0 commit comments

Comments
 (0)