This commit is contained in:
Erik 2017-05-07 14:01:31 -04:00
parent 8d16a8312d
commit 8e25c6c417
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
2 changed files with 37 additions and 6 deletions

View File

@ -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!

View File

@ -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")
} }