Skip to content

Commit d7c868a

Browse files
authored
Fix test_webSocket hang (#4973)
1 parent ff64e86 commit d7c868a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Sources/FoundationNetworking/URLSession/URLSessionTask.swift

Lines changed: 6 additions & 0 deletions
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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,8 +2064,15 @@ class TestURLSession: LoopbackServerTest {
20642064
XCTFail("Unexpected Data Message")
20652065
}
20662066

2067-
try await task.sendPing()
2068-
2067+
do {
2068+
try await task.sendPing()
2069+
// Server hasn't closed the connection yet
2070+
} catch {
2071+
// Server closed the connection before we could process the pong
2072+
let urlError = try XCTUnwrap(error as? URLError)
2073+
XCTAssertEqual(urlError._nsError.code, NSURLErrorNetworkConnectionLost)
2074+
}
2075+
20692076
wait(for: [delegate.expectation], timeout: 50)
20702077

20712078
do {

0 commit comments

Comments
 (0)