Skip to content

Deflake queue-deallocation tests by awaiting the enqueued task#78

Open
dfed wants to merge 1 commit into
mainfrom
deflake-fifoqueue-deallocation-tests
Open

Deflake queue-deallocation tests by awaiting the enqueued task#78
dfed wants to merge 1 commit into
mainfrom
deflake-fifoqueue-deallocation-tests

Conversation

@dfed

@dfed dfed commented Jun 9, 2026

Copy link
Copy Markdown
Owner

What

Rewrite the four *_executesAfterQueueIsDeallocated() tests in FIFOQueueTests to await the final enqueued task's value instead of waiting on Expectation.fulfillment(withinSeconds: 30).

Why

These tests were the source of the intermittent visionOS_2 CI failures (Expectation not fulfilled within 30 seconds, e.g. iteration 8/100). The expectation is a wall-clock race: it pits a Task.sleep(30s) against fulfill(), and fulfill() itself hops through an extra detached Task. Under the -test-iterations 100 -run-tests-until-failure stress harness on a slow, contended simulator, the cooperative pool can be starved long enough that the sleep wins before the queued work runs — a false failure. The identical code passes on macOS/iOS/Linux; it's purely runner-speed sensitivity.

How it stays correct

Task(on:) captures only its Delivery/Semaphore, never the FIFOQueue, so holding the returned handle doesn't retain the queue — the weak queue == nil assertion still holds. Awaiting the last enqueued task is a strictly stronger check than the old expectation: it waits for the work to actually complete rather than for a side-channel flag, and the Counter assertions still verify execution order. Tests now complete in ~1ms regardless of runner speed.

The other fulfillment(withinSeconds:) call sites (e.g. ExpectationTests, which deliberately tests timeout behavior) are left unchanged.

🤖 Generated with Claude Code

The four `*_executesAfterQueueIsDeallocated` tests confirmed the final
enqueued task ran by waiting on `Expectation.fulfillment(withinSeconds: 30)`.
That is a wall-clock race: the expectation pits a `Task.sleep(30s)` against
`fulfill()`, and `fulfill()` itself hops through an extra detached task. Under
the `-test-iterations 100 -run-tests-until-failure` stress harness on a slow,
contended simulator (visionOS), the cooperative pool can be starved long
enough that the sleep wins before the queued work is scheduled, producing a
false failure.

`Task(on:)` captures only its `Delivery`/`Semaphore` — never the `FIFOQueue` —
so holding the returned handle does not retain the queue, and the
`weak queue == nil` assertion still holds. Capture the last enqueued task and
`await` its value instead. This removes the wall-clock race entirely and is a
strictly stronger check: it waits for the work to actually complete rather
than for a side-channel flag, while the `Counter` assertions still verify
ordering. The tests now finish in ~1ms regardless of runner speed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Jun 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3aaec5b) to head (8d8b7e5).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main       #78   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           13        13           
  Lines         1301      1297    -4     
=========================================
- Hits          1301      1297    -4     
Files with missing lines Coverage Δ
Tests/AsyncQueueTests/FIFOQueueTests.swift 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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