@@ -235,29 +235,65 @@ class ReconnectableConnectionTests: XCTestCase {
235235
236236 waitForExpectations ( timeout: 2 /*seconds*/)
237237 }
238-
239-
238+
239+ public func testReconnectableConnectionIgnoreStopRequestWhenDisconnected( ) {
240+
241+ var isConnectionOpen = false
242+
243+ let testConnection = TestConnection ( )
244+ let delegate = TestConnectionDelegate ( )
245+ let reconnectableConnection = ReconnectableConnection ( connectionFactory: { return testConnection} , reconnectPolicy: NoReconnectPolicy ( ) , callbackQueue: callbackQueue, logger: PrintLogger ( ) )
246+ reconnectableConnection. delegate = delegate
247+
248+ delegate. connectionDidOpenHandler = { connection in
249+ isConnectionOpen = true
250+ }
251+
252+ delegate. connectionDidCloseHandler = { connection in
253+ isConnectionOpen = false
254+ }
255+
256+ reconnectableConnection. start ( )
257+ XCTAssertTrue ( isConnectionOpen)
258+
259+ reconnectableConnection. stop ( stopError: nil )
260+ XCTAssertTrue ( !isConnectionOpen)
261+
262+ // stop the connection while it is disconnected
263+ reconnectableConnection. stop ( stopError: nil )
264+ XCTAssertTrue ( !isConnectionOpen)
265+
266+ reconnectableConnection. start ( )
267+ XCTAssertTrue ( isConnectionOpen)
268+ }
269+
240270 class TestConnection : Connection {
241271 var delegate : ConnectionDelegate ?
242272 var openError : Error ?
243-
273+
244274 var connectionId : String ?
245275 var inherentKeepAlive = false
246-
276+ var isClosed : Bool = false
277+
247278 func start( ) {
248279 if let e = openError {
249280 delegate? . connectionDidFailToOpen ( error: e)
250281 } else {
251282 delegate? . connectionDidOpen ( connection: self )
283+ self . isClosed = false
252284 }
253285 }
254-
286+
255287 func send( data: Data , sendDidComplete: ( Error ? ) -> Void ) {
256288 sendDidComplete ( nil )
257289 }
258-
290+
259291 func stop( stopError: Error ? ) {
292+ // Recreating the logic as in HTTPConnection. (the delegate method would only be called when the connection state changes to stopped. Therefore the state transition of ReconnectableConnection to disconnected will not be executed
293+ guard !self . isClosed else { return }
294+ self . isClosed = true
260295 delegate? . connectionDidClose ( error: stopError)
261296 }
297+
262298 }
263299}
0 commit comments