Skip to content

Commit 3497826

Browse files
committed
test(ios-runner): pin the optional main-thread gate across a real timeout and drain
1 parent 6dbe4ce commit 3497826

1 file changed

Lines changed: 47 additions & 18 deletions

File tree

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

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,44 +181,73 @@ extension RunnerTests {
181181
XCTAssertFalse(hasAbandonedMainThreadWork())
182182
}
183183

184-
func testRunMainThreadWorkIfIdleDeclinesOnlyForInFlightWork() {
185-
// Production marks work abandoned only while its block is still running on main, so the mark
186-
// never stands alone. The gate therefore reads in-flight dispatches and nothing else: a mark
187-
// with an idle main thread names no work to wait for and must not starve the recorder.
184+
func testRunMainThreadWorkIfIdleDeclinesAbandonedWorkUntilItDrains() {
188185
final class Outcome {
189-
var value: Bool?
190-
var offered = false
186+
var abandonedCount = 0
187+
var inFlightCount = 0
188+
var offeredWhileAbandoned = false
189+
var whileAbandoned: Bool?
190+
var ranWhileAbandoned = false
191+
var afterDrain: Bool?
191192
var error: Error?
192193
}
193194
let outcome = Outcome()
194-
let finished = expectation(description: "optional work was offered with main free")
195-
abandonedMainThreadWorkCount = 1
196-
defer { abandonedMainThreadWorkCount = 0 }
195+
let releaseWork = DispatchSemaphore(value: 0)
196+
let finished = expectation(description: "optional work was offered while abandoned and after drain")
197197

198-
DispatchQueue(label: "agent-device.runner.tests.stale-abandoned-mark").async {
198+
DispatchQueue(label: "agent-device.runner.tests.abandoned-then-drained").async {
199199
defer { finished.fulfill() }
200+
_ = try? self.runMainThreadWork(
201+
"command_execution",
202+
timeout: 0,
203+
timeoutError: self.mainThreadExecutionTimeoutError
204+
) {
205+
_ = releaseWork.wait(timeout: .now() + 3)
206+
}
207+
self.mainThreadWorkLock.lock()
208+
outcome.abandonedCount = self.abandonedMainThreadWorkCount
209+
outcome.inFlightCount = self.mainThreadWorkInFlightCount
210+
self.mainThreadWorkLock.unlock()
200211
do {
201-
outcome.value = try self.runMainThreadWorkIfIdle(
212+
outcome.whileAbandoned = try self.runMainThreadWorkIfIdle(
213+
"recording_frame",
214+
timeout: 5,
215+
timeoutError: self.mainThreadExecutionTimeoutError
216+
) { () -> Bool in
217+
outcome.ranWhileAbandoned = true
218+
return true
219+
}
220+
outcome.offeredWhileAbandoned = true
221+
} catch {
222+
outcome.error = error
223+
}
224+
releaseWork.signal()
225+
let drainDeadline = Date().addingTimeInterval(3)
226+
while self.hasAbandonedMainThreadWork(), Date() < drainDeadline {
227+
usleep(2_000)
228+
}
229+
do {
230+
outcome.afterDrain = try self.runMainThreadWorkIfIdle(
202231
"recording_frame",
203232
timeout: 5,
204233
timeoutError: self.mainThreadExecutionTimeoutError
205234
) {
206235
Thread.isMainThread
207236
}
208-
outcome.offered = true
209237
} catch {
210238
outcome.error = error
211239
}
212240
}
213241

214242
wait(for: [finished], timeout: 10)
215243
XCTAssertNil(outcome.error)
216-
XCTAssertTrue(outcome.offered)
217-
XCTAssertEqual(
218-
outcome.value,
219-
true,
220-
"an abandoned mark over an idle main thread is no reason to decline a frame"
221-
)
244+
XCTAssertEqual(outcome.abandonedCount, 1)
245+
XCTAssertEqual(outcome.inFlightCount, 1, "abandoned work stays counted in flight until it returns")
246+
XCTAssertTrue(outcome.offeredWhileAbandoned)
247+
XCTAssertNil(outcome.whileAbandoned, "optional work declines while abandoned work holds main")
248+
XCTAssertFalse(outcome.ranWhileAbandoned, "declined work is never dispatched")
249+
XCTAssertEqual(outcome.afterDrain, true, "optional work runs on main once abandoned work drained")
250+
XCTAssertFalse(hasAbandonedMainThreadWork())
222251
}
223252

224253
private final class BoundaryOutcome {

0 commit comments

Comments
 (0)