edge tests: gate bucket checks fire their attempts at once (#1348)
The new tunnel test and the linked-request test both spent a ten-token bucket with eleven logins in a row. Under -race each refused password's bcrypt took long enough for the bucket to refill a token (one per six seconds), so the eleventh got 401 instead of 429 on CI. The attempts now run concurrently; the bucket is spent before bcrypt runs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+45
−16
internal/edge/linkpin_test.go+23 −9internal/edge/tunnel_client_addr_test.go+22 −7
Intent
Human
<task-notification> <task-id>bao61gxxu</task-id> <tool-use-id>toolu_012oNUPytFkXr5jVn6d1YcNL</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/92b455b5-5f98-4e74-88b4-5388c3c18f62/tasks/bao61gxxu.output</output-file> <status>completed</status> <summary>Background command "Wait for Astra's check and then CI" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/edge/linkpin_test.go b/internal/edge/linkpin_test.go| index c1775ae79..fd3758fd4 100644 |
| --- a/internal/edge/linkpin_test.go |
| +++ b/internal/edge/linkpin_test.go |
| @@ -471,15 +471,29 @@ func TestLinkPinForwardedFallback(t *testing.T) { |
| } |
| wrong := url.Values{"password": {"nope-nope-nope"}, "next": {"/"}}.Encode() |
| login := func(chain string) int { return serve("POST", gate.LoginPath, chain, wrong).Code } |
| - // Ten malformed or missing chains from one peer share the peer's bucket. |
| - bad := []string{"198.51.100.7, invalid", "", "-", "x", "invalid, 198.51.100.8"} |
| - for i := 0; i < 10; i++ { |
| - if code := login(bad[i%len(bad)]); code != http.StatusUnauthorized { |
| - t.Fatalf("attempt %d: %d, want 401", i+1, code) |
| - } |
| - } |
| - if code := login("198.51.100.7, also-invalid"); code != http.StatusTooManyRequests { |
| - t.Fatalf("an 11th malformed attempt from the peer: %d, want 429 (the peer's bucket)", code) |
| + // Eleven malformed or missing chains from one peer share the peer's |
| + // bucket of ten: exactly one is refused. They run at once: the bucket |
| + // refills a token every six seconds, and eleven bcrypts in a row under |
| + // -race take long enough to refill one (#1348). The bucket is spent |
| + // before bcrypt runs, so concurrent attempts reach it together. |
| + bad := []string{"198.51.100.7, invalid", "", "-", "x", "invalid, 198.51.100.8", "198.51.100.7, also-invalid"} |
| + codes := make(chan int, 11) |
| + var wg sync.WaitGroup |
| + for i := 0; i < 11; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + codes <- login(bad[i%len(bad)]) |
| + }() |
| + } |
| + wg.Wait() |
| + close(codes) |
| + count := map[int]int{} |
| + for c := range codes { |
| + count[c]++ |
| + } |
| + if count[http.StatusUnauthorized] != 10 || count[http.StatusTooManyRequests] != 1 { |
| + t.Fatalf("eleven malformed attempts from the peer answered %v, want ten 401s and one 429 (the peer's bucket)", count) |
| } |
| if code := login("198.51.100.9"); code != http.StatusUnauthorized { |
| t.Fatalf("a well-formed visitor through the same peer: %d, want its own 401", code) |
diff --git a/internal/edge/tunnel_client_addr_test.go b/internal/edge/tunnel_client_addr_test.go| index 34992685e..45ed50545 100644 |
| --- a/internal/edge/tunnel_client_addr_test.go |
| +++ b/internal/edge/tunnel_client_addr_test.go |
| @@ -233,14 +233,29 @@ func TestTunnelledForwardedFallback(t *testing.T) { |
| } |
| wrong := url.Values{"password": {"nope-nope-nope"}, "next": {"/"}}.Encode() |
| login := func(chain string) int { return serve("POST", gate.LoginPath, chain, wrong).Code } |
| - // Ten attempts from one visitor the hub saw spend that visitor's bucket. |
| - for i := 0; i < 10; i++ { |
| - if code := login("198.51.100.7"); code != http.StatusUnauthorized { |
| - t.Fatalf("attempt %d: %d, want 401", i+1, code) |
| - } |
| + // Eleven attempts from one visitor the hub saw share that visitor's |
| + // bucket of ten: exactly one is refused. They run at once because the |
| + // bucket refills a token every six seconds and each refused password |
| + // costs a bcrypt, which under -race takes long enough to refill; the |
| + // bucket is spent before bcrypt runs (gate.Admit), so concurrent |
| + // attempts reach it within milliseconds. |
| + codes := make(chan int, 11) |
| + var wg sync.WaitGroup |
| + for i := 0; i < 11; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + codes <- login("198.51.100.7") |
| + }() |
| + } |
| + wg.Wait() |
| + close(codes) |
| + count := map[int]int{} |
| + for c := range codes { |
| + count[c]++ |
| } |
| - if code := login("198.51.100.7"); code != http.StatusTooManyRequests { |
| - t.Fatalf("an 11th attempt from the same visitor: %d, want 429", code) |
| + if count[http.StatusUnauthorized] != 10 || count[http.StatusTooManyRequests] != 1 { |
| + t.Fatalf("eleven attempts from one visitor answered %v, want ten 401s and one 429", count) |
| } |
| // Another visitor through the same hub has its own bucket. |
| if code := login("198.51.100.9"); code != http.StatusUnauthorized { |