test(pricefeedwatcher): assert only one watch loop is started - #4032
Merged
Conversation
The reflect-based cancel func comparison this replaces could never fail: reflect.Value.Pointer returns a code pointer, and every cancelWatch comes from context.WithCancel, so all of them compared equal regardless of how many loops ran. Each watch loop polls the price feed once per period, so a duplicate loop is observable as a duplicate FetchPriceData call. synctest makes the hour long period instant and the goroutine scheduling deterministic. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
j0sh
approved these changes
Aug 21, 2026
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.
Follow-up to #4031, targeting
ja/golang-1.27so it lands with the Go 1.27 bump.#4031 removes the
reflect-based cancel func comparison fromTestPriceFeedWatcher_Subscribe. That removal is correct, but it leaves the "only 1 watch loop gets started" comment describing something nothing checks. This adds a test that actually checks it.Why the old assertion could not work
It compared
reflect.ValueOf(w.cancelWatch).Pointer()across subscriptions. For a func value that returns the address of the compiled function body, not the identity of the closure. EverycancelWatchcomes fromcontext.WithCancel, so all of them share one code address:Two unrelated cancel funcs, guaranteed equal. The assertion passed whether
ensureWatchLockedassigned once or five times, so it could not detect the bug it was written for.reflect's docs say as much: for aFuncthe result "is not necessarily enough to identify a single function uniquely".What this asserts instead
A duplicate watch loop is observable: each loop gets its own ticker and polls
w.priceFeedonce perpriceUpdatePeriod. So N loops means NFetchPriceDatacalls per tick.priceUpdatePeriodis 1 hour, which is why this was not testable before.testing/synctest(already used elsewhere in #4031) makes the hour instant, putsnewTruncatedTickeron a deterministic boundary, andsynctest.Wait()blocks until every bubble goroutine is idle. No sleeps, no flake.Verified both directions
if w.cancelWatch != nil { return }guard inensureWatchLockedremoved: fails withExpected number of calls (1) does not match the actual number of calls (5).🤖 Generated with Claude Code