Skip to content

Commit

Permalink
Ensure graceful handling of DETACH and DISCONNECT.
Browse files Browse the repository at this point in the history
Fixes #454.
  • Loading branch information
tcard committed Jul 13, 2016
1 parent bdaa3d0 commit 4f6c1f8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions Source/ARTRealtime.m
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ - (void)realtimeTransport:(id)transport didReceiveMessage:(ARTProtocolMessage *)
// Event
[self onConnected:message];
break;
case ARTProtocolMessageDisconnect:
case ARTProtocolMessageDisconnected:
[self onDisconnected:message];
break;
Expand Down
9 changes: 8 additions & 1 deletion Source/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ - (void)onChannelMessage:(ARTProtocolMessage *)message {
case ARTProtocolMessageAttached:
[self setAttached:message];
break;
case ARTProtocolMessageDetach:
case ARTProtocolMessageDetached:
[self setDetached:message];
break;
Expand Down Expand Up @@ -413,7 +414,13 @@ - (void)setDetached:(ARTProtocolMessage *)message {
}
self.attachSerial = nil;

ARTStatus *reason = [ARTStatus state:ARTStateNotAttached info:[ARTErrorInfo createWithCode:0 message:@"channel has detached"]];
ARTErrorInfo *errorInfo;
if (message.error) {
errorInfo = message.error;
} else {
errorInfo = [ARTErrorInfo createWithCode:0 message:@"channel has detached"];
}
ARTStatus *reason = [ARTStatus state:ARTStateNotAttached info:errorInfo];
[self detachChannel:reason];
[_detachedEventEmitter emit:[NSNull null] with:nil];
}
Expand Down
26 changes: 26 additions & 0 deletions Spec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2407,6 +2407,32 @@ class RealtimeClientChannel: QuickSpec {
}
}
}

// https://github.com/ably/ably-ios/issues/454
it("should not move to FAILED if received DETACH with an error") {
let options = AblyTests.commonAppSetup()
let client = ARTRealtime(options: options)
defer {
client.dispose()
client.close()
}
let channel = client.channels.get("test")
channel.attach()

expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)

let protoMsg = ARTProtocolMessage()
protoMsg.action = .Detach
protoMsg.error = ARTErrorInfo.createWithCode(123, message: "test error")
protoMsg.channel = "test"

client.realtimeTransport(client.transport, didReceiveMessage: protoMsg)

expect(channel.state).to(equal(ARTRealtimeChannelState.Detached))
expect(channel.errorReason).to(equal(protoMsg.error))
expect(client.connection.state).to(equal(ARTRealtimeConnectionState.Connected))
expect(client.connection.errorReason).to(beNil())
}
}
}
}
20 changes: 20 additions & 0 deletions Spec/RealtimeClientConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,26 @@ class RealtimeClientConnection: QuickSpec {

}

// https://github.com/ably/ably-ios/issues/454
it("should not move to FAILED if received DISCONNECT with an error") {
let options = AblyTests.commonAppSetup()
let client = ARTRealtime(options: options)
defer {
client.dispose()
client.close()
}

expect(client.connection.state).toEventually(equal(ARTRealtimeConnectionState.Connected), timeout: testTimeout)

let protoMsg = ARTProtocolMessage()
protoMsg.action = .Disconnect
protoMsg.error = ARTErrorInfo.createWithCode(123, message: "test error")

client.realtimeTransport(client.transport, didReceiveMessage: protoMsg)

expect(client.connection.state).to(equal(ARTRealtimeConnectionState.Disconnected))
expect(client.connection.errorReason).to(equal(protoMsg.error))
}
}
}
}

0 comments on commit 4f6c1f8

Please sign in to comment.