Skip to content

Commit 9719773

Browse files
committed
test(ios): pin the non-interactive query sweep to its tier slice through the plan
Swizzles XCUIElementQuery.allElementsBoundByIndex with a slow query and runs a non-interactive [.querySweep] plan with a 20 s plan deadline. The sweep must not be abandoned, and no query may start after the plan answers, so passing the plan deadline to the sweep in either loop turns it red.
1 parent d054acd commit 9719773

2 files changed

Lines changed: 127 additions & 0 deletions

File tree

‎.github/workflows/ios.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ jobs:
255255
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testActionNamesAreCappedPerElementAndReported \
256256
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testHungCustomActionReadIsContainedAndRecovers \
257257
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testAbandonedTreeCaptureSkipsQuerySweepAndHonorsWarmupExemption \
258+
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testNonInteractiveQuerySweepStopsAtTheSliceItsCallerWaitsFor \
258259
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testBlockingModalSnapshotLeavesWarmupExemptionForTheFirstCapturePlan \
259260
-only-testing:AgentDeviceRunnerUITests/RunnerTests/testPrivateAXPinnedRegularDepthReachesAcquisitionAndPresentation 2>&1 | tee /tmp/agent-device-runner-regressions.log
260261
node --input-type=module -e '

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

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,44 @@ private final class RunnerBlockingSnapshotStub: NSObject {
2626
}
2727
}
2828

29+
/// Records when each `RunnerSlowSweepQueryStub` query started. The swizzled IMP cannot capture
30+
/// test-local state, so each run resets it.
31+
private enum RunnerSlowSweepQueryGate {
32+
/// Shorter than `flatInteractiveQueryBudget`, so a sweep that stops at its slice deadline never
33+
/// outlives the slice; a full sweep of these outlasts the slice, so one that ignores it does.
34+
static let queryDuration: TimeInterval = 0.06
35+
private static let lock = NSLock()
36+
private static var starts: [Date] = []
37+
38+
static func reset() {
39+
lock.lock()
40+
starts = []
41+
lock.unlock()
42+
}
43+
44+
static func recordStart() {
45+
lock.lock()
46+
starts.append(Date())
47+
lock.unlock()
48+
}
49+
50+
static func recordedStarts() -> [Date] {
51+
lock.lock()
52+
defer { lock.unlock() }
53+
return starts
54+
}
55+
}
56+
57+
/// Stands in for `-[XCUIElementQuery allElementsBoundByIndex]` so every query the sweep runs holds
58+
/// the main thread for a fixed time and finds nothing.
59+
private final class RunnerSlowSweepQueryStub: NSObject {
60+
@objc var allElementsBoundByIndex: [XCUIElement] {
61+
RunnerSlowSweepQueryGate.recordStart()
62+
Thread.sleep(forTimeInterval: RunnerSlowSweepQueryGate.queryDuration)
63+
return []
64+
}
65+
}
66+
2967
extension RunnerTests {
3068
/// The Bluesky feed shape: the tree XPC grinds past its slice. The plan must recover through
3169
/// private AX without queueing the query sweep behind the abandoned XPC, and a fresh process's
@@ -124,5 +162,93 @@ extension RunnerTests {
124162
return XCTFail("expected the runner idle once the tree XPC drained")
125163
}
126164
}
165+
166+
/// A non-interactive query sweep runs its queries on the main thread one at a time. It must stop
167+
/// starting them at the slice deadline its caller waits for, not at the plan deadline, or it holds
168+
/// the main thread after the plan has answered (#2783).
169+
func testNonInteractiveQuerySweepStopsAtTheSliceItsCallerWaitsFor() throws {
170+
let sweepQueryCount = 19
171+
XCTAssertLessThan(RunnerSlowSweepQueryGate.queryDuration, Self.flatInteractiveQueryBudget)
172+
XCTAssertGreaterThan(
173+
RunnerSlowSweepQueryGate.queryDuration * Double(sweepQueryCount),
174+
Self.flatInteractiveFallbackBudget
175+
)
176+
guard
177+
let queryMethod = class_getInstanceMethod(
178+
XCUIElementQuery.self,
179+
#selector(getter: XCUIElementQuery.allElementsBoundByIndex)
180+
),
181+
let stubMethod = class_getInstanceMethod(
182+
RunnerSlowSweepQueryStub.self,
183+
#selector(getter: RunnerSlowSweepQueryStub.allElementsBoundByIndex)
184+
)
185+
else {
186+
XCTFail("unable to install the slow sweep query stub")
187+
return
188+
}
189+
app.launchArguments = ["--agent-device-selector-read-regression"]
190+
app.launch()
191+
XCTAssertTrue(app.wait(for: .runningForeground, timeout: 10))
192+
XCTAssertFalse(app.frame.isEmpty)
193+
currentApp = app
194+
currentBundleId = "com.callstack.agentdevice.runner.query-sweep-slice-test"
195+
let captureTarget = takeSnapshotCaptureTarget(app: app)
196+
RunnerSlowSweepQueryGate.reset()
197+
let originalImplementation = method_getImplementation(queryMethod)
198+
method_setImplementation(queryMethod, method_getImplementation(stubMethod))
199+
defer {
200+
method_setImplementation(queryMethod, originalImplementation)
201+
clearSnapshotXCTestChannelPenalty(reason: "test-cleanup")
202+
invalidateCachedTarget(reason: "unit_test_cleanup")
203+
app.terminate()
204+
}
205+
206+
final class ResultBox {
207+
var payload: DataPayload?
208+
var error: Error?
209+
var returnedAt: Date?
210+
var abandonedAtReturn: Bool?
211+
}
212+
let box = ResultBox()
213+
let planned = expectation(description: "query-sweep plan answered")
214+
DispatchQueue(label: "agent-device.runner.tests.query-sweep-slice").async {
215+
do {
216+
box.payload = try self.runSnapshotCapturePlan(
217+
[.querySweep],
218+
target: captureTarget,
219+
options: PresentationOptions(interactiveOnly: false, depth: nil, scope: nil, raw: false),
220+
terminal: .sparseWithFatalOnAXFailure,
221+
deadline: Date().addingTimeInterval(20)
222+
)
223+
} catch {
224+
box.error = error
225+
}
226+
box.returnedAt = Date()
227+
box.abandonedAtReturn = self.hasAbandonedMainThreadWork()
228+
planned.fulfill()
229+
}
230+
231+
wait(for: [planned], timeout: 30)
232+
let drainDeadline = Date().addingTimeInterval(3)
233+
while hasAbandonedMainThreadWork(), Date() < drainDeadline {
234+
sleepFor(0.005)
235+
}
236+
let starts = RunnerSlowSweepQueryGate.recordedStarts()
237+
238+
XCTAssertNil(box.error)
239+
XCTAssertEqual(box.payload?.snapshotQuality?.backend, SnapshotBackendKind.querySweep.rawValue)
240+
XCTAssertEqual(
241+
box.abandonedAtReturn,
242+
false,
243+
"the sweep must finish inside the slice its caller waits for"
244+
)
245+
let returnedAt = try XCTUnwrap(box.returnedAt)
246+
XCTAssertTrue(
247+
starts.allSatisfy { $0 <= returnedAt },
248+
"no sweep query may start after the plan answered"
249+
)
250+
XCTAssertLessThan(starts.count, sweepQueryCount, "the slice must cut the sweep short")
251+
XCTAssertFalse(hasAbandonedMainThreadWork())
252+
}
127253
}
128254
#endif

0 commit comments

Comments
 (0)