Fixes #676
This commit is contained in:
parent
8d16a8312d
commit
8e25c6c417
@ -221,6 +221,39 @@ class SocketSideEffectTest: XCTestCase {
|
|||||||
waitForExpectations(timeout: 0.2)
|
waitForExpectations(timeout: 0.2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testConnectTimesOutIfNotConnected() {
|
||||||
|
let expect = expectation(description: "The client should call the timeout function")
|
||||||
|
|
||||||
|
socket.setTestStatus(.notConnected)
|
||||||
|
|
||||||
|
socket.connect(timeoutAfter: 1, withHandler: {
|
||||||
|
expect.fulfill()
|
||||||
|
})
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConnectDoesNotTimeOutIfConnected() {
|
||||||
|
let expect = expectation(description: "The client should not call the timeout function")
|
||||||
|
|
||||||
|
socket.setTestStatus(.notConnected)
|
||||||
|
|
||||||
|
socket.connect(timeoutAfter: 1, withHandler: {
|
||||||
|
XCTFail("Should not call timeout handler if status is connected")
|
||||||
|
})
|
||||||
|
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
|
||||||
|
// Fake connecting
|
||||||
|
self.socket.setTestStatus(.connected)
|
||||||
|
}
|
||||||
|
|
||||||
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.1) {
|
||||||
|
expect.fulfill()
|
||||||
|
}
|
||||||
|
|
||||||
|
waitForExpectations(timeout: 2)
|
||||||
|
}
|
||||||
|
|
||||||
let data = "test".data(using: String.Encoding.utf8)!
|
let data = "test".data(using: String.Encoding.utf8)!
|
||||||
let data2 = "test2".data(using: String.Encoding.utf8)!
|
let data2 = "test2".data(using: String.Encoding.utf8)!
|
||||||
private var socket: SocketIOClient!
|
private var socket: SocketIOClient!
|
||||||
|
|||||||
@ -184,10 +184,8 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
|
|
||||||
guard timeoutAfter != 0 else { return }
|
guard timeoutAfter != 0 else { return }
|
||||||
|
|
||||||
let time = DispatchTime.now() + Double(UInt64(timeoutAfter) * NSEC_PER_SEC) / Double(NSEC_PER_SEC)
|
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(timeoutAfter)) {[weak self] in
|
||||||
|
guard let this = self, this.status == .connecting || this.status == .notConnected else { return }
|
||||||
handleQueue.asyncAfter(deadline: time) {[weak self] in
|
|
||||||
guard let this = self, this.status != .connected && this.status != .disconnected else { return }
|
|
||||||
|
|
||||||
this.status = .disconnected
|
this.status = .disconnected
|
||||||
this.engine?.disconnect(reason: "Connect timeout")
|
this.engine?.disconnect(reason: "Connect timeout")
|
||||||
@ -555,9 +553,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func _tryReconnect() {
|
private func _tryReconnect() {
|
||||||
guard reconnecting else { return }
|
guard reconnects && reconnecting && status != .disconnected else { return }
|
||||||
|
|
||||||
if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects {
|
if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts {
|
||||||
return didDisconnect(reason: "Reconnect Failed")
|
return didDisconnect(reason: "Reconnect Failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user