Skip to content

Harden timing-sensitive tests against parallel-execution flakiness - #214

Merged
ra1028 merged 1 commit into
mainfrom
fix-timing-test-flakiness
Jun 18, 2026
Merged

ra1028 merged 1 commit into
mainfrom
fix-timing-test-flakiness

Conversation

@ra1028

@ra1028 ra1028 commented Jun 18, 2026

Copy link
Copy Markdown
Owner

Summary

Three tests flaked intermittently on CI after the migration to Swift Testing, which runs tests in parallel by default. On slow, contended simulators (especially watchOS) the parallel execution starved timing-sensitive async work, surfacing latent fragility that was masked by XCTest's serial execution. The failing tests don't depend on the change being a behavior regression — they fail purely due to scheduling delays under load.

This PR hardens the three tests. No production code changes.

Changes

  • testWaitFor (AtomTestContextTests): the updates were scheduled relative to the test's start, so if the wait(for:) subscription was delayed past all of them, the predicate was already satisfied and the wait returned false. Now a single update is scheduled right before the wait and delayed so it lands after the wait starts observing — its timing is independent of the test's start.
  • testWaitForUpdateWithinTimeout (AtomTestContextTests): the success case used a tight within: 1 timeout that a contended host could exceed. It now uses a generous timeout, so it only fails if the update never arrives. The timeout-throwing case keeps its small timeout (it deterministically times out because no update happens).
  • testComplexDependencies (StoreContextTests): the test and the atom used a single stream to hand off control in both directions. Under load the atom could consume its own signal from the shared buffer and proceed before the test updated a dependency (causing e.g. after == 0 instead of 1). It now uses two one-way channels (reachedSuspension, resume) so neither side consumes the other's signal.

Verification

Reproduced the original flakiness by running the suite under CPU contention: the old tests failed intermittently (e.g. testComplexDependencies), while the hardened tests passed across many runs, including under heavy load (2× cores of CPU stress). make format / make lint clean.

🤖 Generated with Claude Code

Swift Testing runs tests in parallel by default, which exposed latent timing
fragility in three tests on slow, contended CI simulators (these tests passed
reliably under XCTest's serial execution):

- `testWaitFor`: the updates were scheduled relative to the test's start, so a
  delayed `wait(for:)` subscription could miss them. Schedule a single update
  right before the wait instead, delayed so it lands after the wait starts
  observing.
- `testWaitForUpdateWithinTimeout`: the success case used a tight 1s timeout
  that a contended host could exceed. Use a generous timeout so it only fails
  if the update never arrives.
- `testComplexDependencies`: the test and the atom shared a single stream to
  hand off control in both directions, so the atom could consume its own
  signal from the shared buffer and proceed before the test updated a
  dependency. Use two one-way channels so neither side consumes the other's
  signal.

Verified by reproducing the original flakiness under CPU contention (old tests
failed, hardened tests passed across many runs).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 18, 2026 02:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens three timing-sensitive tests that became flaky after migrating to Swift Testing (parallel execution by default). The changes are confined to test code and aim to remove scheduling/race assumptions that break under CI contention (notably on slower simulators like watchOS).

Changes:

  • Updated AtomTestContextTests.testWaitFor to schedule a single delayed state update immediately before the wait, reducing sensitivity to startup scheduling delays.
  • Increased the success-path timeout in AtomTestContextTests.testWaitForUpdateWithinTimeout to avoid spurious failures on contended hosts.
  • Refactored StoreContextTests.testComplexDependencies to use two one-way async channels to prevent test/atom signal cross-consumption under load.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
Tests/AtomsTests/Core/StoreContextTests.swift Replaces a shared stream pipe with two one-way pipes to remove a race in the test’s coordination protocol.
Tests/AtomsTests/Context/AtomTestContextTests.swift Adjusts timing/timeout behavior in async wait tests to reduce parallel-execution flakiness.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ra1028
ra1028 merged commit 913f4bd into main Jun 18, 2026
5 checks passed
@ra1028
ra1028 deleted the fix-timing-test-flakiness branch June 18, 2026 02:57
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