Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

test(spaces-macos): make the hung window listing test deterministic (#5037)

Francesco Bonaccib72e91c42074
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/StreamRowsModel.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/StreamRowsModel.swift
index ef3b5da51b..bc9c1e8a34 100644
--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/StreamRowsModel.swift
+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/StreamRowsModel.swift
@@ -33,14 +33,18 @@ final class StreamRowsModel: ObservableObject {
private let listWindows: Windows
private let primaryDisplay: Display
private let appIcons: Icons
+ /// When a window listing times out (tests fire it by hand).
+ private let deadline: DeadlineScheduler
init(os: AppSpaceOs, osName: String?, windows: @escaping Windows,
- display: @escaping Display, icons: @escaping Icons) {
+ display: @escaping Display, icons: @escaping Icons,
+ deadline: @escaping DeadlineScheduler = dispatchDeadline) {
self.os = os
self.osName = osName
self.listWindows = windows
self.primaryDisplay = display
self.appIcons = icons
+ self.deadline = deadline
}
/// From the app's backend.
@@ -75,12 +79,12 @@ final class StreamRowsModel: ObservableObject {
/// Reads the windows and the display once.
/// How long a window listing may take: a hung one would keep "Looking
/// for this Space's windows…" up forever; past this it reads as failed.
- static var windowsTimeout: Double = 15
+ static let windowsTimeout: Double = 15
func refresh() async {
async let d = primaryDisplay()
do {
- windows = try await withTimeout(seconds: Self.windowsTimeout) { [listWindows] in
+ windows = try await withTimeout(seconds: Self.windowsTimeout, deadline: deadline) { [listWindows] in
try await listWindows()
}.get()
failed = false
diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StreamRowsTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StreamRowsTests.swift
index 6ba7b308f7..cf6c2625e4 100644
--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StreamRowsTests.swift
+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/StreamRowsTests.swift
@@ -7,6 +7,7 @@ import CuaSpacesFFI
@testable import CuaSpacesMacKit
import CuaSpacesStreaming
import Foundation
+import os
import Testing
/// Stream rows fixtures: an Ubuntu Space with a screen target, two Firefox
@@ -72,19 +73,22 @@ struct StreamRowsTests {
/// A listing that never answers does not keep "Looking for this
/// Space's windows…" up: past the bound it reads as failed, with the
/// Desktop row still there.
+ /// The deadline fires at once here, so the test does not depend on the
+ /// runner's speed.
@Test func aHungWindowListingFailsInsteadOfLoadingForever() async {
- let saved = StreamRowsModel.windowsTimeout
- StreamRowsModel.windowsTimeout = 0.2
- defer { StreamRowsModel.windowsTimeout = saved }
+ let scheduled = OSAllocatedUnfairLock<[Double]>(initialState: [])
let m = StreamRowsModel(os: .linux, osName: "Ubuntu 24.04",
windows: {
try await Task.sleep(for: .seconds(3600))
return []
},
- display: { nil }, icons: { _ in [] })
- let started = ContinuousClock.now
+ display: { nil }, icons: { _ in [] },
+ deadline: { seconds, fire in
+ scheduled.withLock { $0.append(seconds) }
+ fire()
+ })
await m.refresh()
- #expect(ContinuousClock.now - started < .seconds(5))
+ #expect(scheduled.withLock { $0 } == [StreamRowsModel.windowsTimeout])
#expect(m.failed)
#expect(m.windows == [])
#expect(m.section().rows.map(\.label) == ["Desktop"])