Skip to content

Commit 6cc43c9

Browse files
matt2eclaude
andcommitted
test(mobile): report host hop latency when a push snapshot callback times out
Follow-up to 885804282 from its review. That commit added a class-level `BuzzTestHostSettleBarrier` that measured a user-initiated-queue-then-main hop in `setUp` and let the class start once three consecutive hops were fast. Re-reading both 2026-11-01 CI logs showed the barrier would have opened immediately in both failing runs: in job 124905175903 the one-hop test `testAllowedNotificationRestoreNeedsNoAppGroupStore` passed in 0.070 s at the start of the class, and in job 124910378184 the gate would have opened in the 1-2 s lull after its 1.287 s hop. Both stalls began mid-class and recurred mid-test, which a one-instant gate cannot cover, and a sustained-hop version would be a CI-only wait. The barrier, its doc comment with the measurement figures, and its "settled/gave up" print are removed. The per-element QoS assertion from 885804282 and everything from f0bf2c0c8 stay. In its place, a `waitForCallback` helper in `BuzzPushSnapshotEnrichmentTests` wraps the three callback waits in the two affected tests. It waits with `XCTWaiter` for the unchanged 10 s `callbackTimeout` and returns at once on success, costing nothing on a passing run. Only when the wait does not complete does it time one fresh user-initiated-queue-then-main-queue hop, the path every bridge reply follows, bounded at 2 s, and fail with the expectation's description plus the measured hop ("a fresh queue->main hop took 705.3 ms", "took 0.4 ms", or "did not finish within 2 s"). That lets a starved host be told apart from a callback that never fired the next time this appears in CI. The original assertions still run after the helper, so the nil-versus-reason evidence is unchanged. If this recurs, the remaining levers are the test host (RunnerTests boot the full Flutter app as TEST_HOST, which is what saturated the 3-vCPU runner) or the 10 s `callbackTimeout`. Verification (Xcode 27.2, iOS 26.2 simulator runtime, 16-core host): - Forced timeout, with temporary variants not committed: making the bridge's retry deletion never call back produced "'retry failure stays allowed' not fulfilled within 10 s; a fresh queue->main hop took 0.4 ms" followed by the existing nil-versus-reason assertion failure; adding a 0.7 s sleep inside the hop produced "took 705.3 ms". - Class in isolation with the final code: 10/10 green, 0.1-0.5 s each. - Full RunnerTests bundle with CI's `-parallel-testing-enabled NO`: 2/2 green (101 tests, 24-29 s). - The commit touches only this test file; the pinned Dart formatter was first on PATH so the pre-commit hook reformatted nothing else. No swiftformat or swiftlint configuration in the repo; `git diff --check` is clean. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent ff19edf commit 6cc43c9

1 file changed

Lines changed: 27 additions & 52 deletions

File tree

‎mobile/ios/RunnerTests/BuzzCommunicationNotificationTests.swift‎

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -242,61 +242,36 @@ final class BuzzCommunicationNotificationTests: XCTestCase {
242242
}
243243
}
244244

245-
/// RunnerTests load the full Flutter app as their host. On CI's 3-vCPU runners
246-
/// its launch saturated the machine for about 40 s (both 2026-11-01 failures):
247-
/// a trivial queue-to-main hop took 1.3 s, then over 10 s, while a synchronous
248-
/// main-thread test slowed from 0.013 s to 2.65 s, and the bridge's replies
249-
/// missed their 10 s waits. This barrier measures that same hop, at the QoS
250-
/// the bridge uses, and lets the class begin only once three consecutive hops
251-
/// are fast again. On an idle host it costs a few milliseconds. The cap keeps a
252-
/// host that never settles from hanging the run; its tests then fail with the
253-
/// real symptom, and the printed line says the barrier gave up.
254-
enum BuzzTestHostSettleBarrier {
255-
/// A hop this slow leaves too little margin under the class's 10 s waits.
256-
static let slowHop: TimeInterval = 0.5
257-
static let consecutiveFastHops = 3
258-
static let cap: TimeInterval = 60
259-
260-
/// Waits once per process; later calls return immediately.
261-
static func waitUntilSettled() {
262-
_ = settled
263-
}
264-
265-
private static let settled: Void = {
266-
let queue = DispatchQueue(label: "xyz.block.buzz.test-host-settle", qos: .userInitiated)
267-
let started = Date()
268-
let deadline = started.addingTimeInterval(cap)
269-
var fastHops = 0
270-
var slowHops = 0
271-
while fastHops < consecutiveFastHops, Date() < deadline {
272-
let hopStarted = Date()
273-
let hop = XCTestExpectation(description: "queue to main hop")
274-
queue.async { DispatchQueue.main.async { hop.fulfill() } }
275-
_ = XCTWaiter().wait(for: [hop], timeout: max(deadline.timeIntervalSinceNow, 0))
276-
if Date().timeIntervalSince(hopStarted) < slowHop {
277-
fastHops += 1
278-
} else {
279-
fastHops = 0
280-
slowHops += 1
281-
}
282-
}
283-
let outcome = fastHops < consecutiveFastHops ? "gave up" : "settled"
284-
print(
285-
"BuzzTestHostSettleBarrier \(outcome) after \(Date().timeIntervalSince(started)) s"
286-
+ " (\(slowHops) slow hops)"
287-
)
288-
}()
289-
}
290-
291245
/// Callbacks only record what they receive and assertions run after the wait,
292246
/// so a callback that lands after its test returned cannot fail a later test.
293247
/// The timeout is a cap; a passing test returns as soon as its callback runs.
294248
final class BuzzPushSnapshotEnrichmentTests: XCTestCase {
295249
private let callbackTimeout: TimeInterval = 10
296250

297-
override class func setUp() {
298-
super.setUp()
299-
BuzzTestHostSettleBarrier.waitUntilSettled()
251+
/// Waits for a bridge callback. On timeout the failure also reports how long
252+
/// one fresh user-initiated-queue-then-main-queue hop takes right then (the
253+
/// path every bridge reply follows), so a starved test host ("hop took 7.2 s")
254+
/// reads differently from a callback that never fired ("hop took 12 ms").
255+
/// Both 2026-11-01 CI failures were the former: the RunnerTests host is the
256+
/// full Flutter app, and its launch saturated the 3-vCPU runner mid-class.
257+
/// A passing wait returns as soon as the callback runs and measures nothing.
258+
private func waitForCallback(
259+
_ expectation: XCTestExpectation, file: StaticString = #filePath, line: UInt = #line
260+
) {
261+
guard XCTWaiter().wait(for: [expectation], timeout: callbackTimeout) != .completed else { return }
262+
let hop = XCTestExpectation(description: "queue to main hop")
263+
let started = Date()
264+
DispatchQueue(label: "xyz.block.buzz.test-hop", qos: .userInitiated).async {
265+
DispatchQueue.main.async { hop.fulfill() }
266+
}
267+
let hopResult = XCTWaiter().wait(for: [hop], timeout: 2)
268+
let elapsed = String(format: "%.1f ms", Date().timeIntervalSince(started) * 1000)
269+
let hopReport = hopResult == .completed ? "took \(elapsed)" : "did not finish within 2 s"
270+
XCTFail(
271+
"'\(expectation.expectationDescription)' not fulfilled within \(Int(callbackTimeout)) s;"
272+
+ " a fresh queue->main hop \(hopReport)",
273+
file: file, line: line
274+
)
300275
}
301276

302277
func testAgeSignalRequestDoesNotDependOnNotificationStorage() {
@@ -406,7 +381,7 @@ final class BuzzPushSnapshotEnrichmentTests: XCTestCase {
406381
purgeResult = value
407382
restricted.fulfill()
408383
})
409-
wait(for: [restricted], timeout: callbackTimeout)
384+
waitForCallback(restricted)
410385
XCTAssertEqual((purgeResult as? FlutterError)?.code, "age_restriction_purge_failed")
411386
XCTAssertTrue(BuzzAgeRestrictionSession.isRestricted(containerURL: directory))
412387
XCTAssertEqual(try Data(contentsOf: snapshotURL), original)
@@ -419,7 +394,7 @@ final class BuzzPushSnapshotEnrichmentTests: XCTestCase {
419394
restoreResult = value
420395
restored.fulfill()
421396
})
422-
wait(for: [restored], timeout: callbackTimeout)
397+
waitForCallback(restored)
423398
XCTAssertNil(restoreResult)
424399
XCTAssertFalse(BuzzAgeRestrictionSession.isRestricted(containerURL: directory))
425400
XCTAssertEqual(deletionCalls, 2)
@@ -459,7 +434,7 @@ final class BuzzPushSnapshotEnrichmentTests: XCTestCase {
459434
received = value
460435
completed.fulfill()
461436
})
462-
wait(for: [completed], timeout: callbackTimeout)
437+
waitForCallback(completed)
463438
XCTAssertEqual((received as? FlutterError)?.code, "interaction_cleanup_retry_failed")
464439
XCTAssertFalse(BuzzAgeRestrictionSession.isRestricted(containerURL: directory))
465440
}

0 commit comments

Comments
 (0)