Skip to content

test: fix flaky retry-after ordering assertion in ratelimit tests - #936

Merged
xperimental merged 1 commit into
observatorium:mainfrom
IshwarKanse:fix-flaky-retry-after-test
Sep 28, 2026
Merged

xperimental merged 1 commit into
observatorium:mainfrom
IshwarKanse:fix-flaky-retry-after-test

Conversation

@IshwarKanse

Copy link
Copy Markdown
Contributor

Summary

  • launchTestRequests in ratelimit/http_test.go fired requests from goroutines staggered only by a 1ms time.Sleep, then stored each result by launch index. The shared rate limiter's getAndSetNextRetryAfterValue (ratelimit/http.go) doubles the Retry-After value deterministically in server-arrival order under a mutex, not launch order, so a 1ms stagger doesn't guarantee the two stay in sync under CI load.
  • This caused TestWithSharedRateLimiter/one_rate_limiter_with_additional_retry_after_logic to occasionally fail with something like unexpected Retry-After header values: wanted [ 1 4 8 16], got [ 1 4 16 8]. Observed on CircleCI (ci/circleci: test, build 13935) on fix: preserve empty traces/resourceSpans arrays in RBAC-filtered trace responses #934, unrelated to that PR's actual changes.
  • Since these subtests assert a strict, deterministic Retry-After progression, true concurrency isn't needed to exercise that behavior. launchTestRequests now sends each request synchronously and waits for its response before firing the next one, so launch order and server-arrival order can never diverge.

Test plan

  • go build ./ratelimit/...
  • go vet ./ratelimit/...
  • gofmt -l ratelimit/http_test.go (no output)
  • go test ./ratelimit/... -run TestWithSharedRateLimiter -count=20 -race — all pass
  • go test ./ratelimit/... -count=10 -race — full package, all pass

🤖 Generated with Claude Code

@xperimental xperimental left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay ... I tried to find out why the test code used parallel requests in the first place, but I was not able to. Running the commit where the tests were originally added with a lot of parallelism also yielded some failures, so I'm assuming that it was always flaky. I still have not found out why it was using parallel requests though.

Left a few comments.

Comment thread ratelimit/http_test.go Outdated
Comment thread ratelimit/http_test.go Outdated
Comment thread ratelimit/http_test.go Outdated
IshwarKanse added a commit to IshwarKanse/api that referenced this pull request Sep 28, 2026
Address review comments from @xperimental on PR observatorium#936:
- Since requests are no longer sent concurrently, accumulate
  gotOKs/gotTooManyRequests/gotHeaders directly in the loop instead
  of writing to an intermediate slice and iterating again.
- Use the range-over-int form (for range reqNum) instead of a
  C-style counting loop.
- Check the error from res.Body.Close() and fail the test on it,
  consistent with how the Get error is handled.
@IshwarKanse
IshwarKanse force-pushed the fix-flaky-retry-after-test branch from 7e48b66 to 03fb62d Compare September 28, 2026 02:05
launchTestRequests fired requests from goroutines staggered only by a
1ms sleep, then stored each result by launch index. The shared rate
limiter assigns Retry-After values in server-arrival order under a
mutex, so a 1ms stagger doesn't guarantee arrival order matches launch
order under CI load. This let
TestWithSharedRateLimiter/one_rate_limiter_with_additional_retry_after_logic
flake when requests arrived out of order (observed on CircleCI,
ci/circleci: test, build 13935, on PR observatorium#934).

Since the affected subtests assert a strict, deterministic Retry-After
progression, true concurrency isn't needed: send each request
synchronously, waiting for its response before firing the next, and
accumulate results directly instead of through an intermediate slice.
@IshwarKanse
IshwarKanse force-pushed the fix-flaky-retry-after-test branch from 03fb62d to 5c6d995 Compare September 28, 2026 03:00
@xperimental
xperimental merged commit 08aad9a into observatorium:main Sep 28, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants