Skip to content

Commit 8e25c6c

Browse files
committed
Fixes #676
1 parent 8d16a83 commit 8e25c6c

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

SocketIO-MacTests/SocketSideEffectTest.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,39 @@ class SocketSideEffectTest: XCTestCase {
221221
waitForExpectations(timeout: 0.2)
222222
}
223223

224+
func testConnectTimesOutIfNotConnected() {
225+
let expect = expectation(description: "The client should call the timeout function")
226+
227+
socket.setTestStatus(.notConnected)
228+
229+
socket.connect(timeoutAfter: 1, withHandler: {
230+
expect.fulfill()
231+
})
232+
233+
waitForExpectations(timeout: 2)
234+
}
235+
236+
func testConnectDoesNotTimeOutIfConnected() {
237+
let expect = expectation(description: "The client should not call the timeout function")
238+
239+
socket.setTestStatus(.notConnected)
240+
241+
socket.connect(timeoutAfter: 1, withHandler: {
242+
XCTFail("Should not call timeout handler if status is connected")
243+
})
244+
245+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
246+
// Fake connecting
247+
self.socket.setTestStatus(.connected)
248+
}
249+
250+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.1) {
251+
expect.fulfill()
252+
}
253+
254+
waitForExpectations(timeout: 2)
255+
}
256+
224257
let data = "test".data(using: String.Encoding.utf8)!
225258
let data2 = "test2".data(using: String.Encoding.utf8)!
226259
private var socket: SocketIOClient!

Source/SocketIOClient.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
184184

185185
guard timeoutAfter != 0 else { return }
186186

187-
let time = DispatchTime.now() + Double(UInt64(timeoutAfter) * NSEC_PER_SEC) / Double(NSEC_PER_SEC)
188-
189-
handleQueue.asyncAfter(deadline: time) {[weak self] in
190-
guard let this = self, this.status != .connected && this.status != .disconnected else { return }
187+
handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(timeoutAfter)) {[weak self] in
188+
guard let this = self, this.status == .connecting || this.status == .notConnected else { return }
191189

192190
this.status = .disconnected
193191
this.engine?.disconnect(reason: "Connect timeout")
@@ -555,9 +553,9 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
555553
}
556554

557555
private func _tryReconnect() {
558-
guard reconnecting else { return }
556+
guard reconnects && reconnecting && status != .disconnected else { return }
559557

560-
if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts || !reconnects {
558+
if reconnectAttempts != -1 && currentReconnectAttempt + 1 > reconnectAttempts {
561559
return didDisconnect(reason: "Reconnect Failed")
562560
}
563561

0 commit comments

Comments
 (0)