mirror of
https://github.com/stablyai/orca.git
synced 2026-10-31 17:17:38 +00:00
The macOS helper serves each sidecar connection on a dedicated thread that lives as long as the connection, and the sidecar keeps one connection open while the helper runs. Encoding a screenshot with NSBitmapImageRep leaves autoreleased objects that keep the captured image alive, and that thread never drained its pool, so every screenshot kept a window-sized buffer in WindowServer until the helper restarted. Drain an autorelease pool around each request.
51 lines
1.6 KiB
Swift
51 lines
1.6 KiB
Swift
import Foundation
|
|
@testable import OrcaComputerUseMacOSCore
|
|
import XCTest
|
|
|
|
final class AgentConnectionRequestLoopTests: XCTestCase {
|
|
func testReleasesARequestsAutoreleasedObjectsBeforeTheNextRequest() {
|
|
var lines = ["screenshot", "next"]
|
|
weak var screenshotBuffer: NSObject?
|
|
var releasedBeforeNextRequest: Bool?
|
|
|
|
AgentConnectionRequestLoop.run(readLine: { lines.isEmpty ? nil : lines.removeFirst() }) { line in
|
|
if line == "screenshot" {
|
|
let buffer = NSObject()
|
|
screenshotBuffer = buffer
|
|
// Why: stands in for the objects AppKit PNG encoding leaves in the pool.
|
|
_ = Unmanaged.passRetained(buffer).autorelease()
|
|
} else {
|
|
releasedBeforeNextRequest = screenshotBuffer == nil
|
|
}
|
|
return true
|
|
}
|
|
|
|
XCTAssertEqual(releasedBeforeNextRequest, true)
|
|
}
|
|
|
|
func testServesEveryLineUntilInputEnds() {
|
|
var lines = ["a", "b", "c"]
|
|
var handled: [String] = []
|
|
|
|
AgentConnectionRequestLoop.run(readLine: { lines.isEmpty ? nil : lines.removeFirst() }) { line in
|
|
handled.append(line)
|
|
return true
|
|
}
|
|
|
|
XCTAssertEqual(handled, ["a", "b", "c"])
|
|
}
|
|
|
|
func testStopsReadingWhenTheHandlerClosesTheConnection() {
|
|
var lines = ["a", "b", "c"]
|
|
var handled: [String] = []
|
|
|
|
AgentConnectionRequestLoop.run(readLine: { lines.isEmpty ? nil : lines.removeFirst() }) { line in
|
|
handled.append(line)
|
|
return line != "b"
|
|
}
|
|
|
|
XCTAssertEqual(handled, ["a", "b"])
|
|
XCTAssertEqual(lines, ["c"])
|
|
}
|
|
}
|