Skip to content

fix: make testMixedModeMayTargetAnyPeer deterministic via an internal randomInt seam (#817) - #1048

Open
IG-ABHINAV wants to merge 1 commit into
crazy-goat:masterfrom
IG-ABHINAV:fix/flaky-mixed-mode-817
Open

IG-ABHINAV wants to merge 1 commit into
crazy-goat:masterfrom
IG-ABHINAV:fix/flaky-mixed-mode-817

Conversation

@IG-ABHINAV

Copy link
Copy Markdown

What

Makes testMixedModeMayTargetAnyPeer deterministic by adding an internal test seam to RegionContextFactory::resolveTarget() — an optional ?Closure(int, int): int $randomInt = null parameter that defaults to the existing random_int(...) pick — and having the test drive that seam with a sequence that visits every candidate.

Why

Issue #817: the test looped 20 random draws and asserted all three peers (stores 3, 4, 5) appeared. With three candidates the chance at least one peer is missed is ~0.09% per run, and it has already failed CI on PR #807 (Failed asserting that an array contains 4), costing a rerun on an unrelated PR and eroding trust in the suite. Randomized coverage should not be a pass/fail condition.

How

  • src/Client/Region/RegionContextFactory.php
    • resolveTarget() gains a trailing ?Closure $randomInt = null parameter, documented in the phpdoc as an internal/test-only seam (issue testMixedModeMayTargetAnyPeer is flaky: 20 random draws must hit all three peers #817).
    • Candidate selection becomes $randomInt(0, count($candidates) - 1) when the seam is supplied, otherwise the unchanged random_int(0, count($candidates) - 1). All production call sites (positional, up to $storeAvailable) are unaffected and keep the historical random behaviour by default.
  • tests/Unit/Region/ReplicaReadTargetResolutionTest.php
    • testMixedModeMayTargetAnyPeer now injects a counter-driven closure cycling through every candidate index, so each of stores 3, 4, 5 is hit on a fixed schedule. The per-draw assertion that every pick is in {3, 4, 5} was added; the three assertContains checks are now guaranteed rather than probabilistic.
    • New testMixedModeUsesRandomPickWhenNoSeamIsSupplied locks in the default path: with no seam, 64 unseeded draws still reach all three peers (miss probability ~1.6e-11 — orders of magnitude below the flake being fixed), proving the default was not pinned to one index.

Verification

D:/gptmp/php/php.exe -l src/Client/Region/RegionContextFactory.php
  -> No syntax errors detected
D:/gptmp/php/php.exe -l tests/Unit/Region/ReplicaReadTargetResolutionTest.php
  -> No syntax errors detected

D:/gptmp/php/php.exe vendor/bin/phpunit --no-coverage tests/Unit/Region/ReplicaReadTargetResolutionTest.php   (run 1)
  -> OK (17 tests, 119 assertions)
... run 2
  -> OK (17 tests, 119 assertions)
D:/gptmp/php/php.exe vendor/bin/phpunit --no-coverage --order-by=random tests/Unit/Region/ReplicaReadTargetResolutionTest.php   (run 3)
  -> OK (17 tests, 119 assertions)
... --order-by=random run 4
  -> OK (17 tests, 119 assertions)

composer test:unit   (with change)
  -> Tests: 3058, Assertions: 93446, Failures: 33, Warnings: 1, Skipped: 2
composer test:unit   (clean origin/master baseline, via git stash)
  -> Tests: 3057, Assertions: 93423, Failures: 33, Warnings: 1, Skipped: 2
  -> failing-test list is byte-identical before/after (diff of "✘" lines: IDENTICAL_FAILURES)
  -> the 33 are pre-existing Windows-only docs/docker guard failures
     (e.g. file_get_contents(D:\gptmp\tikvphp/D:\gptmp\tikvphp/README.md)), unrelated to this change.
     The +1 test / +23 assertions delta is exactly the new test.

composer test:grpc   (with change)
  -> Tests: 390, Errors: 1, Failures: 3, Skipped: 153
composer test:grpc   (baseline)
  -> Tests: 390, Errors: 1, Failures: 3, Skipped: 153   (identical — pre-existing, needs ext-grpc/docker)

Duplicate check: git log origin/master --oneline -20 | grep -F '#817' → no hits.

Judgment calls

  • Chose the seam (issue's preferred fix) over the weaker "assert only membership" alternative, since only the seam keeps proving "any peer can be targeted".
  • Kept the 20-iteration loop so the test shape stays recognizable; it is now a fixed schedule, not a gamble.
  • Added the no-seam regression test instead of relying on code review to notice the default path — the issue's DoD explicitly requires default behaviour to be unchanged.
  • phpcs reports only Generic.Files.LineEndings on both files locally: core.autocrlf=true makes the worktree CRLF while the index/CI checkout is LF (baseline fails identically); committed blobs are i/lf.

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.

1 participant