Skip to content

Commit

Permalink
Merge pull request #1740 from STomperi/fixDataRace
Browse files Browse the repository at this point in the history
Fixed a data race that occasionally causes a crash
  • Loading branch information
designatednerd authored Apr 23, 2021
2 parents 456774e + 69466ad commit 802327a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Sources/ApolloWebSocket/WebSocketTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,13 @@ public class WebSocketTransport {
}

public func initServer() {
self.acked = false
processingQueue.async {
self.acked = false

if let str = OperationMessage(payload: self.connectingPayload, type: .connectionInit).rawMessage {
write(str, force:true)
if let str = OperationMessage(payload: self.connectingPayload, type: .connectionInit).rawMessage {
self.write(str, force:true)
}
}

}

public func closeConnection() {
Expand Down
17 changes: 17 additions & 0 deletions Tests/ApolloTests/WebSocket/WebSocketTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ class WebSocketTransportTests: XCTestCase {

waitForExpectations(timeout: 3, handler: nil)
}

func testCloseConnectionAndInit() {
WebSocketTransport.provider = MockWebSocket.self

self.webSocketTransport = WebSocketTransport(request: URLRequest(url: TestURL.mockServer.url),
connectingPayload: ["Authorization": "OldToken"])
self.webSocketTransport.closeConnection()
self.webSocketTransport.updateConnectingPayload(["Authorization": "UpdatedToken"])
self.webSocketTransport.initServer()

let exp = expectation(description: "Wait")
let result = XCTWaiter.wait(for: [exp], timeout: 1.0)
if result == XCTWaiter.Result.timedOut {
} else {
XCTFail("Delay interrupted")
}
}
}

private final class MockWebSocketDelegate: WebSocketDelegate {
Expand Down

0 comments on commit 802327a

Please sign in to comment.