Skip to content

Commit ecf1dd8

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 86e0572 commit ecf1dd8

2 files changed

Lines changed: 181 additions & 0 deletions

File tree

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,79 @@ extension RunnerTests {
248248
XCTAssertFalse(hasAbandonedMainThreadWork())
249249
}
250250

251+
/// A coordinate tap resolves its system-modal routing on the command queue while main may still be
252+
/// clearing or rebinding the cached target. Target identity belongs to main, so an abandoned
253+
/// routing probe must arm its penalty with the identity main settled on — never with the identity
254+
/// the command queue read while that write was still pending (#2781).
255+
func testCoordinateTapRoutingPenalizesTheIdentityMainSettledOnWhileTheWriteWasPending() throws {
256+
app.launch()
257+
XCTAssertTrue(app.wait(for: .runningForeground, timeout: 10))
258+
let pendingBundleId = "com.example.routing-pending-stale"
259+
let settledBundleId = "com.example.routing-pending-settled"
260+
currentApp = app
261+
currentBundleId = pendingBundleId
262+
snapshotXCTestPenaltyWarmupExemption.isPending = false
263+
clearSnapshotXCTestChannelPenalty(reason: "test-setup")
264+
defer {
265+
systemModalProbeOverrideForTesting = nil
266+
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
267+
invalidateCachedTarget(reason: "unit_test_cleanup")
268+
app.terminate()
269+
}
270+
271+
// Occupy main and rebind the target inside that block: every identity read that arrives while it
272+
// is queued sees a target that main is on its way to replacing.
273+
let mainRelease = DispatchSemaphore(value: 0)
274+
DispatchQueue.main.async {
275+
_ = mainRelease.wait(timeout: .now() + 0.5)
276+
self.currentBundleId = settledBundleId
277+
}
278+
279+
let probeStarted = expectation(description: "system-modal routing probe started")
280+
let probeReleaseGate = DispatchSemaphore(value: 0)
281+
systemModalProbeOverrideForTesting = { _ in
282+
probeStarted.fulfill()
283+
// Outlives the probe's own slice, so the abandonment hook is what arms the penalty.
284+
_ = probeReleaseGate.wait(timeout: .now() + 15)
285+
return nil
286+
}
287+
let tap = try runnerCommandFixture(
288+
#"{"command":"tap","commandId":"tap-pending-target-write","x":10,"y":20}"#
289+
)
290+
291+
final class ResultBox {
292+
var response: Response?
293+
var penalizedPendingIdentity = false
294+
var penalizedSettledIdentity = false
295+
}
296+
let box = ResultBox()
297+
let tapFinished = expectation(description: "off-main tap returned")
298+
DispatchQueue(label: "agent-device.runner.tests.tap-pending-target-write").async {
299+
box.response = try? self.executeDispatched(command: tap)
300+
box.penalizedPendingIdentity = self.isSnapshotXCTestChannelPenalized(bundleId: pendingBundleId)
301+
box.penalizedSettledIdentity = self.isSnapshotXCTestChannelPenalized(bundleId: settledBundleId)
302+
probeReleaseGate.signal()
303+
tapFinished.fulfill()
304+
}
305+
306+
wait(for: [probeStarted, tapFinished], timeout: 40)
307+
mainRelease.signal()
308+
let drainDeadline = Date().addingTimeInterval(5)
309+
while hasAbandonedMainThreadWork(), Date() < drainDeadline {
310+
sleepFor(0.002)
311+
}
312+
313+
XCTAssertTrue(
314+
box.penalizedSettledIdentity,
315+
"the abandoned routing probe must penalize the target main settled on"
316+
)
317+
XCTAssertFalse(
318+
box.penalizedPendingIdentity,
319+
"the command queue may not key a penalty with an identity whose write was still pending on main"
320+
)
321+
XCTAssertFalse(hasAbandonedMainThreadWork())
322+
}
323+
251324
func testSkipAppActivationPreflightRejectsSelectorAndMixedSequenceGestures() throws {
252325
app.launch()
253326
currentApp = app

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

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

0 commit comments

Comments
 (0)