Skip to content

Commit

Permalink
Updated test for RTN14e adding check for RTN14f and #913
Browse files Browse the repository at this point in the history
  • Loading branch information
maratal committed Nov 11, 2022
1 parent 2f28564 commit 405a9d5
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions Spec/Tests/RealtimeClientConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2263,8 +2263,8 @@ class RealtimeClientConnectionTests: XCTestCase {
}
}

// RTN14e
func test__060__Connection__connection_request_fails__connection_state_has_been_in_the_DISCONNECTED_state_for_more_than_the_default_connectionStateTtl_should_change_the_state_to_SUSPENDED() {
// RTN14e, RTN14f
func test__060__Connection__connection_request_fails__if_connection_state_is_DISCONNECTED_for_more_than_connectionStateTtl_it_will_change_to_SUSPENDED_with_periodic_connection_attemts_within_suspendedRetryTimeout() {
let options = AblyTests.commonAppSetup()
options.disconnectedRetryTimeout = 0.1
options.suspendedRetryTimeout = 0.5
Expand All @@ -2285,15 +2285,39 @@ class RealtimeClientConnectionTests: XCTestCase {
let ttlHookToken = client.overrideConnectionStateTTL(0.3)
defer { ttlHookToken.remove() }

let retryCount = 8 // reasonable amount for RTN14f within testTimeout
waitUntil(timeout: testTimeout) { done in
client.connection.on(.suspended) { _ in
expect(client.connection.errorReason!.message).to(contain("timed out"))
let partialDone = AblyTests.splitDone(retryCount, done: done)

var firstDisconnectedAt: Date!
var suspendedAt: Date!
var connectingAt: Date!

let start = NSDate()
client.connection.once(.connecting) { _ in
let end = NSDate()
expect(end.timeIntervalSince(start as Date)).to(beCloseTo(options.suspendedRetryTimeout, within: 0.5))
done()
client.connection.on(.disconnected) { stateChange in
expect(suspendedAt).to(beNil()) // https://github.com/ably/ably-cocoa/issues/913
if firstDisconnectedAt == nil {
firstDisconnectedAt = Date()
}
expect(stateChange.reason?.message).to(contain("timed out"))
}
client.connection.on(.suspended) { stateChange in
suspendedAt = Date()
expect(client.connection.errorReason?.message).to(contain("timed out"))
expect(suspendedAt.timeIntervalSince(firstDisconnectedAt)) > client.internal.connectionStateTtl
}
client.connection.on(.connecting) { stateChange in
if stateChange.previous == .initialized {
expect(firstDisconnectedAt).to(beNil())
expect(suspendedAt).to(beNil())
expect(connectingAt).to(beNil())
}
else if stateChange.previous == .suspended {
connectingAt = Date()
expect(connectingAt.timeIntervalSince(suspendedAt)).to(beCloseTo(options.suspendedRetryTimeout, within: 0.1))
partialDone()
}
else {
expect(stateChange.previous).to(equal(ARTRealtimeConnectionState.disconnected))
}
}
client.connect()
Expand Down

0 comments on commit 405a9d5

Please sign in to comment.