Skip to content

Commit

Permalink
[ECO-5117][RTL13a] Fixed channel ATTACHING event detach err
Browse files Browse the repository at this point in the history
1. Added impl. to store server sent detach error to avoid modifying
`attachWithTimeout` method signature
2. Updated test assertions for spec RTL13a accordingly
  • Loading branch information
sacOO7 committed Jan 10, 2025
1 parent a6bbcb2 commit 1360bc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 14 additions & 2 deletions lib/src/main/java/io/ably/lib/realtime/ChannelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ public abstract class ChannelBase extends EventEmitter<ChannelEvent, ChannelStat
*/
private boolean released = false;

private ErrorInfo msgError;

private ErrorInfo getMsgError() {
ErrorInfo error = this.msgError;
if (error != null) {
this.msgError = null;
}
return error;
}

/***
* internal
*
Expand Down Expand Up @@ -218,6 +228,7 @@ synchronized void transferQueuedPresenceMessages(List<QueuedMessage> messagesToT
private boolean attachResume;

private void attachImpl(final boolean forceReattach, final CompletionListener listener) throws AblyException {
ErrorInfo lastProtocolMsgError = this.getMsgError();
Log.v(TAG, "attach(); channel = " + name);
if(!forceReattach) {
/* check preconditions */
Expand Down Expand Up @@ -249,7 +260,7 @@ private void attachImpl(final boolean forceReattach, final CompletionListener li
if (listener != null) {
on(new ChannelStateCompletionListener(listener, ChannelState.attached, ChannelState.failed));
}
setState(ChannelState.attaching, null);
setState(ChannelState.attaching, lastProtocolMsgError);
return;
}

Expand Down Expand Up @@ -277,7 +288,7 @@ private void attachImpl(final boolean forceReattach, final CompletionListener li
attachMessage.setFlag(Flag.attach_resume);
}

setState(ChannelState.attaching, null);
setState(ChannelState.attaching, lastProtocolMsgError);
connectionManager.send(attachMessage, true, null);
} catch(AblyException e) {
throw e;
Expand Down Expand Up @@ -1302,6 +1313,7 @@ void onChannelMessage(ProtocolMessage msg) {
case attached:
case suspended:
/* Unexpected detach, reattach immediately as per RTL13a */
this.msgError = msg.error; // Propagate msg error to ATTACHING stateChange event
Log.v(TAG, String.format(Locale.ROOT, "Server initiated detach for channel %s; attempting reattach", name));
attachWithTimeout(true, null);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1813,11 +1813,16 @@ public void server_initiated_detach_for_attached_channel() throws AblyException
ProtocolMessage detachedMessage = new ProtocolMessage() {{
action = Action.detached;
channel = channelName;
error = new ErrorInfo("Simulated detach", 40000);
}};
ably.connection.connectionManager.onMessage(null, detachedMessage);

/* Channel should transition to attaching, then to attached */
channelWaiter.waitFor(ChannelState.attaching);
ErrorInfo detachErr = channelWaiter.waitFor(ChannelState.attaching);
Assert.assertNotNull(detachErr);
Assert.assertEquals(40000, detachErr.code);
Assert.assertEquals("Simulated detach", detachErr.message);

channelWaiter.waitFor(ChannelState.attached);

List<ChannelState> channelStates = channelWaiter.getRecordedStates();
Expand Down Expand Up @@ -1869,11 +1874,16 @@ public void server_initiated_detach_for_suspended_channel() throws AblyException
ProtocolMessage detachedMessage = new ProtocolMessage() {{
action = Action.detached;
channel = channelName;
error = new ErrorInfo("Simulated detach", 40000);
}};
ably.connection.connectionManager.onMessage(null, detachedMessage);

/* Channel should transition to attaching, then to attached */
channelWaiter.waitFor(ChannelState.attaching);
ErrorInfo detachError = channelWaiter.waitFor(ChannelState.attaching);
Assert.assertNotNull(detachError);
Assert.assertEquals(40000, detachError.code);
Assert.assertEquals("Simulated detach", detachError.message);

channelWaiter.waitFor(ChannelState.attached);

List<ChannelState> channelStates = channelWaiter.getRecordedStates();
Expand Down

0 comments on commit 1360bc6

Please sign in to comment.