diff --git a/ablySpec/RealtimeClientConnection.swift b/ablySpec/RealtimeClientConnection.swift index ae4be0b02..7f778e22d 100644 --- a/ablySpec/RealtimeClientConnection.swift +++ b/ablySpec/RealtimeClientConnection.swift @@ -1197,9 +1197,6 @@ class RealtimeClientConnection: QuickSpec { // RTN14b context("connection request fails") { - - // NOTE: the connection doesn't retry to request a new token if any failure occurs - it("should not emit error with a renewable token") { let options = AblyTests.commonAppSetup() options.autoConnect = false @@ -1257,6 +1254,66 @@ class RealtimeClientConnection: QuickSpec { expect(failures[0].error!.code).to(equal(40142)) //Token expired } + it("should transition to Failed when the token renewal fails") { + let options = AblyTests.commonAppSetup() + options.autoConnect = false + let tokenTtl = 1.0 + let tokenDetails = getTestTokenDetails(key: options.key, capability: nil, ttl: tokenTtl)! + options.token = tokenDetails.token + options.authCallback = { tokenParams, callback in + callback(tokenDetails, nil) // Return the same expired token again. + } + + // Let the token expire + waitUntil(timeout: testTimeout) { done in + delay(tokenTtl) { + done() + } + } + + let client = ARTRealtime(options: options) + client.setTransportClass(TestProxyTransport.self) + defer { + client.dispose() + client.close() + } + + var transport: TestProxyTransport! + + waitUntil(timeout: testTimeout) { done in + client.connection.on { stateChange in + let stateChange = stateChange! + let state = stateChange.current + let errorInfo = stateChange.reason + switch state { + case .Connected: + fail("Should not be connected") + done() + case .Failed, .Disconnected, .Suspended: + guard let errorInfo = errorInfo else { + fail("ErrorInfo is nil"); done(); return + } + expect(errorInfo.code).to(equal(40142)) //Token expired + done() + default: + break + } + } + client.connect() + transport = client.transport as! TestProxyTransport + } + + let failures = transport.protocolMessagesReceived.filter({ $0.action == .Error }) + + if failures.count != 2 { + fail("Should have two connection request fail") + return + } + + expect(failures[0].error!.code).to(equal(40142)) + expect(failures[1].error!.code).to(equal(40142)) + } + it("should transition to Failed state because the token is invalid and not renewable") { let options = AblyTests.clientOptions() options.autoConnect = false