Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTN17с as a part of 0.9 #504

Merged
merged 5 commits into from
Sep 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ - (void)realtimeTransportFailed:(id<ARTRealtimeTransport>)transport withError:(A
if (!_fallbacks && [error.url.host isEqualToString:[ARTDefault realtimeHost]]) {
[self.rest internetIsUp:^void(BOOL isUp) {
_fallbacks = [[ARTFallback alloc] initWithFallbackHosts:[self getClientOptions].fallbackHosts];
[self reconnectWithFallback];
(_fallbacks != nil) ? [self reconnectWithFallback] : [self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createWithNSError:error.error]];
}];
return;
} else if (_fallbacks && [self reconnectWithFallback]) {
Expand Down
76 changes: 76 additions & 0 deletions Spec/RealtimeClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,79 @@ class RealtimeClientConnection: QuickSpec {
expect(resultFallbackHosts).to(equal(expectedFallbackHosts))
}

it("should retry custom fallback hosts in random order after checkin if an internet connection is available") {
let fbHosts = ["f.ably-realtime.com", "g.ably-realtime.com", "h.ably-realtime.com", "i.ably-realtime.com", "j.ably-realtime.com"]

let options = ARTClientOptions(key: "xxxx:xxxx")
options.autoConnect = false
options.fallbackHosts = fbHosts
let client = ARTRealtime(options: options)
let channel = client.channels.get("test")

let testHttpExecutor = TestProxyHTTPExecutor()
client.rest.httpExecutor = testHttpExecutor

client.setTransportClass(TestProxyTransport.self)
TestProxyTransport.network = .HostUnreachable
defer { TestProxyTransport.network = nil }

var urlConnections = [NSURL]()
TestProxyTransport.networkConnectEvent = { url in
urlConnections.append(url)
}

client.connect()
defer { client.close() }

waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: "message") { error in
done()
}
}

expect(NSRegularExpression.match(testHttpExecutor.requests[0].URL!.absoluteString, pattern: "//internet-up.ably-realtime.com/is-the-internet-up.txt")).to(beTrue())
expect(urlConnections).to(haveCount(6)) // default + 5 provided fallbacks

let extractHostname = { (url: NSURL) in
NSRegularExpression.extract(url.absoluteString, pattern: "[f-j].ably-realtime.com")
}
let resultFallbackHosts = urlConnections.flatMap(extractHostname)
let expectedFallbackHosts = Array(expectedHostOrder.map({ fbHosts[$0] }))

expect(resultFallbackHosts).to(equal(expectedFallbackHosts))
}

it("won't use fallback hosts feature if an empty array is provided") {
let options = ARTClientOptions(key: "xxxx:xxxx")
options.autoConnect = false
options.fallbackHosts = []
let client = ARTRealtime(options: options)
let channel = client.channels.get("test")

let testHttpExecutor = TestProxyHTTPExecutor()
client.rest.httpExecutor = testHttpExecutor

client.setTransportClass(TestProxyTransport.self)
TestProxyTransport.network = .HostUnreachable
defer { TestProxyTransport.network = nil }

var urlConnections = [NSURL]()
TestProxyTransport.networkConnectEvent = { url in
urlConnections.append(url)
}

client.connect()
defer { client.close() }

waitUntil(timeout: testTimeout) { done in
channel.publish(nil, data: "message") { error in
done()
}
}

expect(NSRegularExpression.match(testHttpExecutor.requests[0].URL!.absoluteString, pattern: "//internet-up.ably-realtime.com/is-the-internet-up.txt")).to(beTrue())
expect(urlConnections).to(haveCount(1))
}

// RTN17e
it("client is connected to a fallback host endpoint should do HTTP requests to the same data centre") {
Expand All @@ -2853,6 +2926,9 @@ class RealtimeClientConnection: QuickSpec {
}

client.connect()
// Because we're faking the CONNECTED state, we can't client.close() or it
// will actually try to use the connection believing it's ready and throw an
// exception because it's really not.

expect(urlConnections).toEventually(haveCount(2), timeout: testTimeout)

Expand Down