2025-12-12 16:52:02 +00:00
|
|
|
import Foundation
|
2026-03-02 04:15:43 +00:00
|
|
|
import OpenClawKit
|
2025-12-12 16:52:02 +00:00
|
|
|
import Testing
|
2026-01-30 03:15:10 +01:00
|
|
|
@testable import OpenClaw
|
2025-12-12 16:52:02 +00:00
|
|
|
|
|
|
|
|
@Suite struct GatewayChannelRequestTests {
|
2026-03-02 09:39:30 +00:00
|
|
|
private func makeSession(requestSendDelayMs: Int) -> GatewayTestWebSocketSession {
|
|
|
|
|
GatewayTestWebSocketSession(
|
|
|
|
|
taskFactory: {
|
|
|
|
|
GatewayTestWebSocketTask(
|
|
|
|
|
sendHook: { _, _, sendIndex in
|
|
|
|
|
guard sendIndex == 1 else { return }
|
|
|
|
|
try await Task.sleep(nanoseconds: UInt64(requestSendDelayMs) * 1_000_000)
|
|
|
|
|
throw URLError(.cannotConnectToHost)
|
|
|
|
|
})
|
|
|
|
|
})
|
2025-12-12 16:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-02 04:15:43 +00:00
|
|
|
@Test func requestTimeoutThenSendFailureDoesNotDoubleResume() async throws {
|
2026-03-02 09:39:30 +00:00
|
|
|
let session = self.makeSession(requestSendDelayMs: 100)
|
2026-03-02 04:15:43 +00:00
|
|
|
let channel = try GatewayChannelActor(
|
|
|
|
|
url: #require(URL(string: "ws://example.invalid")),
|
2025-12-12 16:52:02 +00:00
|
|
|
token: nil,
|
2025-12-12 21:34:33 +00:00
|
|
|
session: WebSocketSessionBox(session: session))
|
2025-12-12 16:52:02 +00:00
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
_ = try await channel.request(method: "test", params: nil, timeoutMs: 10)
|
|
|
|
|
Issue.record("Expected request to time out")
|
|
|
|
|
} catch {
|
|
|
|
|
let ns = error as NSError
|
|
|
|
|
#expect(ns.domain == "Gateway")
|
|
|
|
|
#expect(ns.code == 5)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Give the delayed send failure task time to run; this used to crash due to a double-resume.
|
|
|
|
|
try? await Task.sleep(nanoseconds: 250 * 1_000_000)
|
|
|
|
|
}
|
|
|
|
|
}
|