Skip to content

Commit 7dcb317

Browse files
committed
test(ios): pin the query-sweep tier timeout and the tap routing identity to main
Two regressions #2781 still leaves open, both red on this head: A query sweep that stops at its own slice deadline is a tier timeout, but the plan classified its payload by node count, so a partial sweep that beat the sparse threshold armed no XCTest-channel penalty and no later capture of the same screen was deferred. The coordinate tap's system-modal routing probe armed its penalty with currentBundleId read on the command queue, while main owned that state and could still be clearing or rebinding it.
1 parent 6d58218 commit 7dcb317

2 files changed

Lines changed: 181 additions & 0 deletions

File tree

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/RunnerTests+CommandExecution.swift‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,79 @@ extension RunnerTests {
641641
XCTAssertFalse(hasAbandonedMainThreadWork())
642642
}
643643

644+
/// A coordinate tap resolves its system-modal routing on the command queue while main may still be
645+
/// clearing or rebinding the cached target. Target identity belongs to main, so an abandoned
646+
/// routing probe must arm its penalty with the identity main settled on — never with the identity
647+
/// the command queue read while that write was still pending (#2781).
648+
func testCoordinateTapRoutingPenalizesTheIdentityMainSettledOnWhileTheWriteWasPending() throws {
649+
app.launch()
650+
XCTAssertTrue(app.wait(for: .runningForeground, timeout: 10))
651+
let pendingBundleId = "com.example.routing-pending-stale"
652+
let settledBundleId = "com.example.routing-pending-settled"
653+
currentApp = app
654+
currentBundleId = pendingBundleId
655+
snapshotXCTestPenaltyWarmupExemption.isPending = false
656+
clearSnapshotXCTestChannelPenalty(reason: "test-setup")
657+
defer {
658+
systemModalProbeOverrideForTesting = nil
659+
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
660+
invalidateCachedTarget(reason: "unit_test_cleanup")
661+
app.terminate()
662+
}
663+
664+
// Occupy main and rebind the target inside that block: every identity read that arrives while it
665+
// is queued sees a target that main is on its way to replacing.
666+
let mainRelease = DispatchSemaphore(value: 0)
667+
DispatchQueue.main.async {
668+
_ = mainRelease.wait(timeout: .now() + 0.5)
669+
self.currentBundleId = settledBundleId
670+
}
671+
672+
let probeStarted = expectation(description: "system-modal routing probe started")
673+
let probeReleaseGate = DispatchSemaphore(value: 0)
674+
systemModalProbeOverrideForTesting = { _ in
675+
probeStarted.fulfill()
676+
// Outlives the probe's own slice, so the abandonment hook is what arms the penalty.
677+
_ = probeReleaseGate.wait(timeout: .now() + 15)
678+
return nil
679+
}
680+
let tap = try runnerCommandFixture(
681+
#"{"command":"tap","commandId":"tap-pending-target-write","x":10,"y":20}"#
682+
)
683+
684+
final class ResultBox {
685+
var response: Response?
686+
var penalizedPendingIdentity = false
687+
var penalizedSettledIdentity = false
688+
}
689+
let box = ResultBox()
690+
let tapFinished = expectation(description: "off-main tap returned")
691+
DispatchQueue(label: "agent-device.runner.tests.tap-pending-target-write").async {
692+
box.response = try? self.executeDispatched(command: tap)
693+
box.penalizedPendingIdentity = self.isSnapshotXCTestChannelPenalized(bundleId: pendingBundleId)
694+
box.penalizedSettledIdentity = self.isSnapshotXCTestChannelPenalized(bundleId: settledBundleId)
695+
probeReleaseGate.signal()
696+
tapFinished.fulfill()
697+
}
698+
699+
wait(for: [probeStarted, tapFinished], timeout: 40)
700+
mainRelease.signal()
701+
let drainDeadline = Date().addingTimeInterval(5)
702+
while hasAbandonedMainThreadWork(), Date() < drainDeadline {
703+
sleepFor(0.002)
704+
}
705+
706+
XCTAssertTrue(
707+
box.penalizedSettledIdentity,
708+
"the abandoned routing probe must penalize the target main settled on"
709+
)
710+
XCTAssertFalse(
711+
box.penalizedPendingIdentity,
712+
"the command queue may not key a penalty with an identity whose write was still pending on main"
713+
)
714+
XCTAssertFalse(hasAbandonedMainThreadWork())
715+
}
716+
644717
func testSkipAppActivationPreflightRejectsSelectorAndMixedSequenceGestures() throws {
645718
app.launch()
646719
currentApp = app

‎apple/runner/AgentDeviceRunner/AgentDeviceRunnerUITests/UnitTests/RunnerTests+SnapshotCapturePlanOccupancyTests.swift‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,5 +250,113 @@ extension RunnerTests {
250250
XCTAssertLessThan(starts.count, sweepQueryCount, "the slice must cut the sweep short")
251251
XCTAssertFalse(hasAbandonedMainThreadWork())
252252
}
253+
254+
/// A query sweep that stops at its own slice deadline collected a partial tree, not an answer, so
255+
/// it is a tier timeout: the plan must arm the XCTest-channel penalty and let private AX answer,
256+
/// keeping the partial sweep only as the fallback. Accepting it because it carries more nodes than
257+
/// the sparse threshold ships a hierarchy-free capture and leaves every later capture of the same
258+
/// screen to pay for the full sweep again (#2781).
259+
func testQuerySweepThatEndsOnItsSliceDeadlinePenalizesChannelAndReachesPrivateAX() throws {
260+
guard
261+
let queryMethod = class_getInstanceMethod(
262+
XCUIElementQuery.self,
263+
#selector(getter: XCUIElementQuery.allElementsBoundByIndex)
264+
),
265+
let stubMethod = class_getInstanceMethod(
266+
RunnerSlowSweepQueryStub.self,
267+
#selector(getter: RunnerSlowSweepQueryStub.allElementsBoundByIndex)
268+
)
269+
else {
270+
XCTFail("unable to install the slow sweep query stub")
271+
return
272+
}
273+
app.launchArguments = ["--agent-device-selector-read-regression"]
274+
app.launch()
275+
XCTAssertTrue(app.wait(for: .runningForeground, timeout: 10))
276+
currentApp = app
277+
currentBundleId = "com.callstack.agentdevice.runner.query-sweep-timeout-test"
278+
snapshotXCTestPenaltyWarmupExemption.isPending = false
279+
clearSnapshotXCTestChannelPenalty(reason: "test-setup")
280+
RunnerSlowSweepQueryGate.reset()
281+
let captureTarget = takeSnapshotCaptureTarget(app: app)
282+
let originalImplementation = method_getImplementation(queryMethod)
283+
method_setImplementation(queryMethod, method_getImplementation(stubMethod))
284+
defer {
285+
method_setImplementation(queryMethod, originalImplementation)
286+
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
287+
clearPrivateAXAcceptedDepth(reason: "test-cleanup")
288+
invalidateCachedTarget(reason: "unit_test_cleanup")
289+
app.terminate()
290+
}
291+
292+
final class ResultBox {
293+
var payload: DataPayload?
294+
var error: Error?
295+
var abandonedAtReturn = false
296+
var penalizedAtReturn = false
297+
var sweepStartsAfterFirstPlan = 0
298+
var secondPayload: DataPayload?
299+
var secondError: Error?
300+
}
301+
let box = ResultBox()
302+
let planned = expectation(description: "slow-sweep plan answered")
303+
DispatchQueue(label: "agent-device.runner.tests.query-sweep-timeout").async {
304+
do {
305+
box.payload = try self.runSnapshotCapturePlan(
306+
[.querySweep, .privateAX],
307+
target: captureTarget,
308+
options: PresentationOptions(interactiveOnly: false, depth: nil, scope: nil, raw: false),
309+
terminal: .sparseWithFatalOnAXFailure,
310+
deadline: Date().addingTimeInterval(20)
311+
)
312+
} catch {
313+
box.error = error
314+
}
315+
box.abandonedAtReturn = self.hasAbandonedMainThreadWork()
316+
box.penalizedAtReturn = self.isSnapshotXCTestChannelPenalized(bundleId: captureTarget.bundleId)
317+
box.sweepStartsAfterFirstPlan = RunnerSlowSweepQueryGate.recordedStarts().count
318+
do {
319+
box.secondPayload = try self.runSnapshotCapturePlan(
320+
Self.regularVisiblePlan,
321+
target: self.takeSnapshotCaptureTarget(app: self.app),
322+
options: PresentationOptions(interactiveOnly: false, depth: nil, scope: nil, raw: false),
323+
terminal: .sparseWithFatalOnAXFailure,
324+
deadline: Date().addingTimeInterval(20)
325+
)
326+
} catch {
327+
box.secondError = error
328+
}
329+
planned.fulfill()
330+
}
331+
332+
wait(for: [planned], timeout: 60)
333+
XCTAssertNil(box.error)
334+
let quality = box.payload?.snapshotQuality
335+
XCTAssertEqual(
336+
quality?.backend,
337+
SnapshotBackendKind.privateAX.rawValue,
338+
"a sweep that ended on its slice deadline is a tier timeout, not an accepted capture"
339+
)
340+
XCTAssertEqual(quality?.state, "recovered")
341+
XCTAssertGreaterThan(box.payload?.nodes?.count ?? 0, 1, "private AX answers with a real tree")
342+
XCTAssertFalse(box.abandonedAtReturn, "the sweep must answer inside its own main-thread hop")
343+
XCTAssertTrue(
344+
box.penalizedAtReturn,
345+
"the tier timeout must arm the XCTest-channel penalty for the captured target"
346+
)
347+
348+
XCTAssertNil(box.secondError)
349+
XCTAssertEqual(
350+
box.secondPayload?.snapshotQuality?.backend,
351+
SnapshotBackendKind.privateAX.rawValue,
352+
"the armed penalty must defer the next plan's XCTest tiers, not re-run the sweep"
353+
)
354+
XCTAssertEqual(
355+
RunnerSlowSweepQueryGate.recordedStarts().count,
356+
box.sweepStartsAfterFirstPlan,
357+
"the deferred plan may start no sweep query at all"
358+
)
359+
XCTAssertFalse(hasAbandonedMainThreadWork())
360+
}
253361
}
254362
#endif

0 commit comments

Comments
 (0)