Skip to content

Commit

Permalink
Test RTN14b error path.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard committed Mar 2, 2016
1 parent 9c83202 commit e20a699
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions ablySpec/RealtimeClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e20a699

Please sign in to comment.