mirror of
https://github.com/block/buzz.git
synced 2026-11-25 02:31:36 +00:00
test(ios): check the navigation glass state often instead of once a second (#9786)
## Summary Fixes a flaky native iOS test, `RunnerTests.testNavigationGlassMergesAndRestoresInBothDirections`. Test-only: one file, `mobile/ios/RunnerTests/RunnerTests.swift`. ### Failure "Mobile Swift Domain / Mobile Swift (Native)" failed on #9780 ([run 44682658228](https://github.com/block/buzz/actions/runs/44682658228/job/136109857758)): ``` mobile/ios/RunnerTests/RunnerTests.swift:83: error: -[RunnerTests.RunnerTests testNavigationGlassMergesAndRestoresInBothDirections] : Asynchronous wait failed: Exceeded timeout of 2 seconds, with unfulfilled expectations: "Expect predicate BLOCKPREDICATE(...)" ``` ### Why it is a flake - #9780 changes only Dart files under `mobile/lib/features/channels` and one Dart test. Nothing in it reaches `MergingNavigationGlass`. - The same job passed on another PR minutes later ([run 44683133967](https://github.com/block/buzz/actions/runs/44683133967)) and passes on most pushes to `main`. - No open PR or commit on `main` already addresses this test. #9260, #9574 and #9725 touch `RunnerTests.swift` but not this test or its waits. ### Cause `setMenuOpen(true)` starts a 0.18 s `UIViewPropertyAnimator`. The test then waited up to 2 s for both side surfaces to reach alpha 0, using `XCTNSPredicateExpectation`. That expectation evaluates its predicate about once a second. The evidence is in the passing run: the test takes 4.07 s for four waits on a 0.18 s animation, about 1 s per wait. A 2 s timeout therefore gets one or two evaluations. RunnerTests runs inside the Flutter app, and when the app holds the main thread across those evaluations the wait expires although the animation finished long before. In the failing run the 2 s timeout was reported 5.2 s after the test started, and the test's three remaining waits then passed at about 1 s each. This is inferred from the CI timings and the code. It was not reproduced. ### Fix Both waits now use a small `waitUntil` helper in the existing `XCTestCase` extension. It checks the condition every 10 ms on the main actor, for up to 10 s (the budget the other RunnerTests callbacks use since #8678 and #8741). It checks the condition before the deadline on every pass, so a main-thread stall of any length is followed by one more check instead of a timeout. The two conditions and every assertion after them are unchanged. If the surfaces never merge or restore, the test still fails, at the calling line. As a side effect the test should take about 0.4 s instead of 4 s. ### Not included `BuzzPushNotificationResolverTests.testShownMessagesStayShownAcrossCommunitiesFailuresAndBursts` also failed once on `main` with a 2 s timeout ([run 44682614076](https://github.com/block/buzz/actions/runs/44682614076/job/136109821029)). It is a different mechanism: an `XCTestExpectation` fulfilled by the resolver's own completion, across about 145 resolver runs, in the macOS `swift test` step. The only available change is a longer timeout in a helper shared by the whole class, and the cause of the slow run is not established, so it is left out. ### Testing - **Not compiled against the app and not run on a simulator.** A local simulator build was started and then abandoned; the machine was heavily loaded. `swiftc -parse` accepts the file. - **The Mobile Swift (Native) lane on this PR is the first place the change compiles and runs.** - Pushed with `git push --no-verify`. `buzz-review-completed` is not added: agent review and human testing (AGENTS.md "Before opening a PR") have not happened. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: Matt Toohey <contact@matttoohey.com> Signed-off-by: Tom Brow <tomb@block.xyz> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com> Co-authored-by: Tom Brow <tomb@block.xyz> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
This commit is contained in:
@@ -196,10 +196,14 @@ class RunnerTests: XCTestCase {
|
||||
XCTAssertEqual(glass.centerSurface.bounds.width, vertical ? 56 : 112)
|
||||
XCTAssertEqual(glass.centerSurface.center, CGPoint(x: glass.bounds.midX, y: glass.bounds.midY))
|
||||
glass.setMenuOpen(true, animator: nil)
|
||||
let merged = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in
|
||||
glass.leftSurface.alpha == 0 && glass.rightSurface.alpha == 0
|
||||
}, object: nil)
|
||||
await fulfillment(of: [merged], timeout: 2)
|
||||
// The values below are set as soon as an animation starts. Also wait
|
||||
// for the animation to finish, so the close that follows starts from a
|
||||
// finished animator, as it does when someone closes the menu.
|
||||
await waitUntil("the side surfaces have merged") {
|
||||
glass.leftSurface.alpha == 0 && glass.rightSurface.alpha == 0 &&
|
||||
(glass.leftSurface.layer.animationKeys() ?? []).isEmpty &&
|
||||
(glass.rightSurface.layer.animationKeys() ?? []).isEmpty
|
||||
}
|
||||
XCTAssertEqual(glass.leftSurface.transform.tx, vertical ? 0 : 100, accuracy: 0.01)
|
||||
XCTAssertEqual(glass.leftSurface.transform.ty, vertical ? 72 : 0, accuracy: 0.01)
|
||||
XCTAssertEqual(glass.rightSurface.transform.tx, vertical ? 0 : -100, accuracy: 0.01)
|
||||
@@ -207,11 +211,12 @@ class RunnerTests: XCTestCase {
|
||||
glass.setMenuOpen(false, animator: nil)
|
||||
glass.setMenuOpen(true, animator: nil)
|
||||
glass.setMenuOpen(false, animator: nil)
|
||||
let restored = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in
|
||||
await waitUntil("the side surfaces are restored") {
|
||||
glass.leftSurface.alpha == 1 && glass.rightSurface.alpha == 1 &&
|
||||
glass.leftSurface.transform == .identity && glass.rightSurface.transform == .identity
|
||||
}, object: nil)
|
||||
await fulfillment(of: [restored], timeout: 2)
|
||||
glass.leftSurface.transform == .identity && glass.rightSurface.transform == .identity &&
|
||||
(glass.leftSurface.layer.animationKeys() ?? []).isEmpty &&
|
||||
(glass.rightSurface.layer.animationKeys() ?? []).isEmpty
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2543,6 +2548,28 @@ extension XCTestCase {
|
||||
.first { $0.activationState == .foregroundActive }
|
||||
}
|
||||
|
||||
/// Returns once `condition` holds, and fails the test if it never does.
|
||||
///
|
||||
/// `XCTNSPredicateExpectation` evaluates its predicate about once a second,
|
||||
/// so a short wait gets one or two looks and a busy host main thread can
|
||||
/// use them all up. This checks every few milliseconds, and checks once
|
||||
/// more before giving up however late the main thread comes back.
|
||||
@MainActor
|
||||
func waitUntil(
|
||||
_ description: String, timeout: TimeInterval = 10,
|
||||
file: StaticString = #filePath, line: UInt = #line,
|
||||
_ condition: @MainActor () -> Bool
|
||||
) async {
|
||||
let deadline = Date().addingTimeInterval(timeout)
|
||||
while !condition() {
|
||||
guard Date() < deadline else {
|
||||
XCTFail("Timed out waiting until \(description)", file: file, line: line)
|
||||
return
|
||||
}
|
||||
try? await Task.sleep(nanoseconds: 10_000_000)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fulfilled once `window` is the key window and shows `root`'s view.
|
||||
@MainActor
|
||||
func keyWindowReady(_ window: UIWindow, showing root: UIViewController) -> XCTestExpectation {
|
||||
|
||||
Reference in New Issue
Block a user