From d59236e66fc769af32e778bdf355a15d7dc302ec Mon Sep 17 00:00:00 2001 From: Vincent Taverna Date: Sat, 31 Oct 2026 15:19:02 -0400 Subject: [PATCH] Drain native benchmark observations through main dispatch --- .../HotPathBenchmarkTests.swift | 9 +++++++++ .../HotPathNativeFixtures.swift | 11 ++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Tests/ParcelPerformanceTests/HotPathBenchmarkTests.swift b/Tests/ParcelPerformanceTests/HotPathBenchmarkTests.swift index e1b54cd8..c9d0fbaa 100644 --- a/Tests/ParcelPerformanceTests/HotPathBenchmarkTests.swift +++ b/Tests/ParcelPerformanceTests/HotPathBenchmarkTests.swift @@ -103,6 +103,15 @@ extension AppKitTestSuite { #expect(chips.panels.allSatisfy { !$0.isVisible }) } + @Test func nativeObservationGateServicesQueuedMainDispatchWork() async throws { + let fixture = HotPathNativeFixture(count: 10) + defer { fixture.stop() } + let target = WindowID(rawValue: 1) + DispatchQueue.main.async { fixture.state.focusedWindow = target } + try await fixture.drain { fixture.state.focusedWindow == target } + #expect(fixture.state.focusedWindow == target) + } + @Test func smokeSoakUsesABoundedTickCountAndReportsActualWork() throws { #expect(HotPathBenchmarks.soakTicks(full: true) == 10000) #expect(HotPathBenchmarks.soakTicks(full: false) == 30) diff --git a/Tests/ParcelPerformanceTests/HotPathNativeFixtures.swift b/Tests/ParcelPerformanceTests/HotPathNativeFixtures.swift index 73723e91..ed717c62 100644 --- a/Tests/ParcelPerformanceTests/HotPathNativeFixtures.swift +++ b/Tests/ParcelPerformanceTests/HotPathNativeFixtures.swift @@ -197,15 +197,20 @@ final class HotPathNativeFixture { backend.resetCounts() failure = nil } - func drain(until predicate: @MainActor () -> Bool) async throws { + func drain(label: String = #function, until predicate: @MainActor () -> Bool) async throws { let deadline = ContinuousClock.now.advanced(by: .seconds(3)) repeat { scheduler.runReady() - await Task.yield() + // A yield alone can resume before the dispatch callbacks that publish observations. + await withCheckedContinuation { (continuation: CheckedContinuation) in + DispatchQueue.main.async { continuation.resume() } + } if let failure { throw failure } if predicate() { return } } while ContinuousClock.now < deadline - throw BenchmarkFailure.measurement("native workload did not reach expected observation") + throw BenchmarkFailure.measurement( + "native \(label) did not reach expected observation; windows=\(state.windows.count), events=\(events.count), pending=\(scheduler.pendingCount)" + ) } func focus() async throws -> BenchmarkObservation { -- knotmirror 0.0.1