Skip to content

Commit f2ee9aa

Browse files
committed
Fix test_webSocket hang (swiftlang#4973)
1 parent 0a0a4df commit f2ee9aa

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Sources/FoundationNetworking/URLSession/URLSessionTask.swift

+6
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,12 @@ open class URLSessionWebSocketTask : URLSessionTask {
865865
}
866866
}
867867
self.receiveCompletionHandlers.removeAll()
868+
for handler in self.pongCompletionHandlers {
869+
session.delegateQueue.addOperation {
870+
handler(taskError)
871+
}
872+
}
873+
self.pongCompletionHandlers.removeAll()
868874
self._getProtocol { urlProtocol in
869875
self.workQueue.async {
870876
if self.handshakeCompleted && self.state != .completed {

Tests/Foundation/Tests/TestURLSession.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -1981,8 +1981,15 @@ class TestURLSession: LoopbackServerTest {
19811981
XCTFail("Unexpected Data Message")
19821982
}
19831983

1984-
try await task.sendPing()
1985-
1984+
do {
1985+
try await task.sendPing()
1986+
// Server hasn't closed the connection yet
1987+
} catch {
1988+
// Server closed the connection before we could process the pong
1989+
let urlError = try XCTUnwrap(error as? URLError)
1990+
XCTAssertEqual(urlError._nsError.code, NSURLErrorNetworkConnectionLost)
1991+
}
1992+
19861993
wait(for: [delegate.expectation], timeout: 50)
19871994

19881995
do {

0 commit comments

Comments
 (0)