Skip to content

Commit

Permalink
Move to DISCONNECTED on any kind of network error.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcard committed Aug 30, 2017
1 parent 2a9f75d commit 03c412b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Source/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ - (void)realtimeTransportFailed:(id<ARTRealtimeTransport>)transport withError:(A
}
}

if (error.type == ARTRealtimeTransportErrorTypeNoInternet) {
if (error.type != ARTRealtimeTransportErrorTypeOther) {
[self transition:ARTRealtimeDisconnected];
} else {
[self transition:ARTRealtimeFailed withErrorInfo:[ARTErrorInfo createFromNSError:error.error]];
Expand Down
2 changes: 1 addition & 1 deletion Source/ARTWebSocketTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ - (ARTRealtimeTransportError *)classifyError:(NSError *)error {
type = ARTRealtimeTransportErrorTypeTimeout;
} else if ([error.domain isEqualToString:(NSString *)kCFErrorDomainCFNetwork]) {
type = ARTRealtimeTransportErrorTypeHostUnreachable;
} else if ([error.domain isEqualToString:@"NSPOSIXErrorDomain"] && error.code == 57) {
} else if ([error.domain isEqualToString:@"NSPOSIXErrorDomain"] && (error.code == 57 || error.code == 50)) {
type = ARTRealtimeTransportErrorTypeNoInternet;
} else if ([error.domain isEqualToString:SRWebSocketErrorDomain] && error.code == 2132) {
id status = error.userInfo[SRHTTPResponseErrorKey];
Expand Down
7 changes: 1 addition & 6 deletions Spec/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,7 @@ class RealtimeClient: QuickSpec {
expect(webSocketTransport.websocketURL?.host).to(equal("fake.ably.io"))
partialDone()
}
client.connection.once(.failed) { stateChange in
guard let reason = stateChange?.reason else {
fail("Reason is nil"); done(); return
}
expect(reason.code) == Int(CFNetworkErrors.cfHostErrorUnknown.rawValue)
expect(reason.message).to(contain("kCFErrorDomainCFNetwork"))
client.connection.once(.disconnected) { stateChange in
partialDone()
}
client.connect()
Expand Down
39 changes: 24 additions & 15 deletions Spec/RealtimeClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3073,25 +3073,34 @@ class RealtimeClientConnection: QuickSpec {
}
}

it("should move to disconnected when there's no internet") {
let options = AblyTests.commonAppSetup()
let client = AblyTests.newRealtime(options)
defer {
client.dispose()
client.close()
context("should move to disconnected when there's no internet") {
var errors: [(String, NSError)] = []
for code in [57, 50] {
errors.append(("with NSPOSIXErrorDomain with code \(code)", NSError(domain: "NSPOSIXErrorDomain", code: code, userInfo: [NSLocalizedDescriptionKey: "shouldn't matter"])))
}
client.setTransport(TestProxyTransport.self)

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.connected), timeout: testTimeout)
errors.append(("with any kCFErrorDomainCFNetwork", NSError(domain: "kCFErrorDomainCFNetwork", code: 1337, userInfo: [NSLocalizedDescriptionKey: "shouldn't matter"])))

for (name, error) in errors {
it(name) {
let options = AblyTests.commonAppSetup()
let client = AblyTests.newRealtime(options)
defer {
client.dispose()
client.close()
}
client.setTransport(TestProxyTransport.self)

guard let wsTransport = client.transport as? ARTWebSocketTransport else {
fail("expected WS transport")
return
}
expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.connected), timeout: testTimeout)

wsTransport.webSocket(wsTransport.websocket, didFailWithError:NSError(domain: "NSPOSIXErrorDomain", code: 57, userInfo: [NSLocalizedDescriptionKey: "Socket is not connected"]))
guard let wsTransport = client.transport as? ARTWebSocketTransport else {
fail("expected WS transport")
return
}

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.disconnected), timeout: testTimeout)
wsTransport.webSocket(wsTransport.websocket, didFailWithError:error)
expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.disconnected), timeout: testTimeout)
}
}
}

it("should not use an alternative host when the client receives a bad request") {
Expand Down

0 comments on commit 03c412b

Please sign in to comment.