From 3e38bd76dc3298dfa0ca1793abc8580ae2cd42ea Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 7 Oct 2026 07:17:22 +0800 Subject: [PATCH] =?UTF-8?q?gateway=20tests:=20TestRestingKeyReadAgainInAnO?= =?UTF-8?q?rderedGroup=20lets=20the=20key=20be=20back=20already=20once=20t?= =?UTF-8?q?he=20relay=20has=20answered=20with=20its=20limit=20raised=20(re?= =?UTF-8?q?d=20on=20ubuntu=20CI=20at=20fb9d1a4c,=20run=2037540375720:=20"r?= =?UTF-8?q?esting:=20ko=20first,=20its=20rest=20")=20=E2=80=94=20sinc?= =?UTF-8?q?e=20ee2588c2=20a=20reading=20of=20the=20key's=20windows=20still?= =?UTF-8?q?=20out=20as=20it=20refused=20for=20quota=20is=20followed=20by?= =?UTF-8?q?=20another=20at=20once.=20When=20that=20second=20reading=20reac?= =?UTF-8?q?hes=20the=20relay=20after=20the=20test=20raises=20the=20limit,?= =?UTF-8?q?=20it=20lifts=20the=20rest=20before=20the=20test=20plans=20agai?= =?UTF-8?q?n,=20which=20is=20right;=20the=20test=20asked=20for=20the=20res?= =?UTF-8?q?t=20to=20still=20stand=20there.=20It=20now=20accepts=20ko=20fir?= =?UTF-8?q?st=20and=20not=20resting=20when=20a=20reading=20saw=20the=20rai?= =?UTF-8?q?sed=20limit,=20and=20still=20wants=20kx=20first=20and=20ko=20re?= =?UTF-8?q?sting=20otherwise.=20With=20the=20first=20reading=20held=20100?= =?UTF-8?q?=20ms=20and=20200=20ms=20between=20raising=20the=20limit=20and?= =?UTF-8?q?=20planning=20(a=20loaded=20CI=20runner),=20the=20old=20test=20?= =?UTF-8?q?fails=20as=20CI=20did=20and=20the=20new=20one=20passes.=20With?= =?UTF-8?q?=20ee2588c2's=20re-read=20taken=20out=20and=20the=20second=20re?= =?UTF-8?q?ading=20held=20300=20ms,=20the=20new=20test=20still=20fails=202?= =?UTF-8?q?=20of=202=20("still=20rests=20by=20window").=20go=20test=20-rac?= =?UTF-8?q?e=20-count=3D40=20-cpu=201,2,4=20-run=20'TestRestingKey|TestKey?= =?UTF-8?q?|Lone|Allowance'=20./internal/gateway=20and=20go=20vet=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/key_limits_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/gateway/key_limits_test.go b/internal/gateway/key_limits_test.go index 5715b787..22a00e57 100644 --- a/internal/gateway/key_limits_test.go +++ b/internal/gateway/key_limits_test.go @@ -273,11 +273,14 @@ func TestRestingKeyReadAgainInAnOrderedGroup(t *testing.T) { t.Cleanup(func() { keyAllowance = old }) week := time.Now().Add(72 * time.Hour).Truncate(time.Second).Format(time.RFC3339) var mu sync.Mutex - limit, reads := 800, 0 + limit, reads, raised := 800, 0, 0 relay := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mu.Lock() l := limit reads++ + if l > 800 { + raised++ + } mu.Unlock() fmt.Fprintf(w, `{"isValid":true,"mode":"quota_limited","status":"active","rate_limits":[{"window":"7d","limit":%d,"used":800,"remaining":%d,"reset_at":%q}]}`, l, l-800, week) })) @@ -324,8 +327,14 @@ func TestRestingKeyReadAgainInAnOrderedGroup(t *testing.T) { mu.Lock() limit = 1000 mu.Unlock() - if at, r := first(); at != "kx" || r == nil { - t.Fatalf("resting: %s first, its rest %v", at, r) + // a reading out as the key refused is followed by another at once, + // which may have seen the limit raised and brought it back already + at, r := first() + mu.Lock() + seen := raised + mu.Unlock() + if (at != "kx" || r == nil) && (at != "ko" || r != nil || seen == 0) { + t.Fatalf("resting: %s first, its rest %v, the raised limit read %d times", at, r, seen) } for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(10 * time.Millisecond) { r, ok := restOf(a.restKey())