test(api): fix flaky ListAll cancellation tests via happens-before sync#45
Open
jklaassenjc wants to merge 1 commit into
Open
test(api): fix flaky ListAll cancellation tests via happens-before sync#45jklaassenjc wants to merge 1 commit into
jklaassenjc wants to merge 1 commit into
Conversation
Pre-fix both TestV1Client_ListAll_ContextCancellation and its V2 sibling used a busy-spin on requestCount + immediate cancel() to try to cancel "after the first page." On a slow CI runner the second page's HTTP request could be issued and complete before the cancel propagated, and ListAll returned a normal result instead of context.Canceled. The PR #41 release run hit exactly this — a "just rerun" flake that recovered on retry. Replace the spin with a strict happens-before sync: the test server blocks the second-page response on a `cancelled` channel that the test goroutine closes only after cancel() has fired. The cancellation is therefore guaranteed to be observed by the HTTP layer before the second response lands, regardless of runner speed. Validated with go test -race -count=100 on both tests — 100/100 pass. Refs KLA-438 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
cancel()inTestV1Client_ListAll_ContextCancellationand the V2 sibling with a strict happens-before sync.cancelledchannel that the test goroutine closes only aftercancel()has fired — so the HTTP client is guaranteed to observe the cancellation before the second response lands.Why
PR #41's release run failed at
make testwith:A
gh run rerun --failedrecovered immediately, so the flake is real but rare — and the kind of flake that erodes confidence in CI ("just rerun" becomes a reflex). Same pattern existed in the V2 variant, fixed alongside per the ticket's acceptance criteria.Test plan
go test ./internal/api/ -race -count=100 -run "TestV(1|2)Client_ListAll_ContextCancellation"— 100/100 pass on bothgo test ./internal/api/ -race -count=1— full package cleango vet ./...cleanRefs KLA-438
🤖 Generated with Claude Code
Note
Low Risk
Test-only synchronization changes in
v1_test.goandv2_test.go; no runtime API client logic is modified.Overview
Fixes flaky
ListAllcontext-cancellation tests for V1 and V2 by replacing a busy-spin on request count with channel-based happens-before ordering.After the first page completes, the test goroutine waits on
firstReqServed, callscancel(), then closescancelled. The mock server blocks responses for page 2+ untilcancelledis closed, so pagination cannot finish successfully before cancellation is visible to the HTTP client.Comments reference KLA-438 and document why the old pattern could pass on slow CI. No production
ListAllbehavior changes—test-only.Reviewed by Cursor Bugbot for commit 5a70743. Bugbot is set up for automated code reviews on this repo. Configure here.