Merge pull request #1375 from emersonsoftware/fix_reconnect_issue
Handling reconnect scenarios properly when socket is hung
This commit is contained in:
		
						commit
						71a627c099
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@ -50,3 +50,5 @@ Socket.IO-Test-Server/node_modules/*
 | 
			
		||||
.idea/
 | 
			
		||||
docs/docsets/
 | 
			
		||||
docs/undocumented.json
 | 
			
		||||
 | 
			
		||||
.swiftpm
 | 
			
		||||
 | 
			
		||||
@ -150,7 +150,8 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
 | 
			
		||||
 | 
			
		||||
        manager.handleQueue.asyncAfter(deadline: DispatchTime.now() + timeoutAfter) {[weak self] in
 | 
			
		||||
            guard let this = self, this.status == .connecting || this.status == .notConnected else { return }
 | 
			
		||||
 | 
			
		||||
            DefaultSocketLogger.Logger.log("Timeout: Socket not connected, so setting to disconnected", type: this.logType)
 | 
			
		||||
            
 | 
			
		||||
            this.status = .disconnected
 | 
			
		||||
            this.leaveNamespace()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -132,7 +132,7 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
 | 
			
		||||
    private(set) var reconnectAttempts = -1
 | 
			
		||||
 | 
			
		||||
    private var _config: SocketIOClientConfiguration
 | 
			
		||||
    private var currentReconnectAttempt = 0
 | 
			
		||||
    internal var currentReconnectAttempt = 0
 | 
			
		||||
    private var reconnecting = false
 | 
			
		||||
 | 
			
		||||
    // MARK: Initializers
 | 
			
		||||
@ -186,9 +186,8 @@ open class SocketManager: NSObject, SocketManagerSpec, SocketParsable, SocketDat
 | 
			
		||||
    ///
 | 
			
		||||
    /// Override if you wish to attach a custom `SocketEngineSpec`.
 | 
			
		||||
    open func connect() {
 | 
			
		||||
        guard !status.active else {
 | 
			
		||||
        if status == .connected || (status == .connecting && currentReconnectAttempt == 0) {
 | 
			
		||||
            DefaultSocketLogger.Logger.log("Tried connecting an already active socket", type: SocketManager.logType)
 | 
			
		||||
 | 
			
		||||
            return
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -60,6 +60,44 @@ class SocketMangerTest : XCTestCase {
 | 
			
		||||
        waitForExpectations(timeout: 0.3)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func testManagerDoesNotCallConnectWhenConnectingWithLessThanOneReconnect() {
 | 
			
		||||
        setUpSockets()
 | 
			
		||||
        
 | 
			
		||||
        let expect = expectation(description: "The manager should not call connect on the engine")
 | 
			
		||||
        expect.isInverted = true
 | 
			
		||||
        
 | 
			
		||||
        let engine = TestEngine(client: manager, url: manager.socketURL, options: nil)
 | 
			
		||||
        
 | 
			
		||||
        engine.onConnect = {
 | 
			
		||||
            expect.fulfill()
 | 
			
		||||
        }
 | 
			
		||||
        manager.setTestStatus(.connecting)
 | 
			
		||||
        manager.setCurrentReconnect(currentReconnect: 0)
 | 
			
		||||
        manager.engine = engine
 | 
			
		||||
        
 | 
			
		||||
        manager.connect()
 | 
			
		||||
 | 
			
		||||
        waitForExpectations(timeout: 0.3)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func testManagerCallConnectWhenConnectingAndMoreThanOneReconnect() {
 | 
			
		||||
        setUpSockets()
 | 
			
		||||
        
 | 
			
		||||
        let expect = expectation(description: "The manager should call connect on the engine")
 | 
			
		||||
        let engine = TestEngine(client: manager, url: manager.socketURL, options: nil)
 | 
			
		||||
        
 | 
			
		||||
        engine.onConnect = {
 | 
			
		||||
            expect.fulfill()
 | 
			
		||||
        }
 | 
			
		||||
        manager.setTestStatus(.connecting)
 | 
			
		||||
        manager.setCurrentReconnect(currentReconnect: 1)
 | 
			
		||||
        manager.engine = engine
 | 
			
		||||
        
 | 
			
		||||
        manager.connect()
 | 
			
		||||
 | 
			
		||||
        waitForExpectations(timeout: 0.8)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    func testManagerCallsDisconnect() {
 | 
			
		||||
        setUpSockets()
 | 
			
		||||
 | 
			
		||||
@ -154,6 +192,10 @@ public enum ManagerExpectation: String {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public class TestManager: SocketManager {
 | 
			
		||||
    public func setCurrentReconnect(currentReconnect: Int) {
 | 
			
		||||
        self.currentReconnectAttempt = currentReconnect
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public override func disconnect() {
 | 
			
		||||
        setTestStatus(.disconnected)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -487,7 +487,7 @@ class TestEngine: SocketEngineSpec {
 | 
			
		||||
    private(set) var ws: WebSocket? = nil
 | 
			
		||||
    private(set) var version = SocketIOVersion.three
 | 
			
		||||
 | 
			
		||||
    fileprivate var onConnect: (() -> ())?
 | 
			
		||||
    internal var onConnect: (() -> ())?
 | 
			
		||||
 | 
			
		||||
    required init(client: SocketEngineClient, url: URL, options: [String: Any]?) {
 | 
			
		||||
        self.client = client
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user