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. Updated `attachWithTimeout` and `attachImpl` method to accept msgError param
2. Updated test assertions for spec RTL13a accordingly
  • Loading branch information
sacOO7 committed Jan 13, 2025
1 parent a6bbcb2 commit 0dfc6c3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
16 changes: 8 additions & 8 deletions lib/src/main/java/io/ably/lib/realtime/ChannelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void attach(CompletionListener listener) throws AblyException {

void attach(boolean forceReattach, CompletionListener listener) {
clearAttachTimers();
attachWithTimeout(forceReattach, listener);
attachWithTimeout(forceReattach, listener, null);
}

/**
Expand All @@ -217,7 +217,7 @@ synchronized void transferQueuedPresenceMessages(List<QueuedMessage> messagesToT

private boolean attachResume;

private void attachImpl(final boolean forceReattach, final CompletionListener listener) throws AblyException {
private void attachImpl(final boolean forceReattach, final CompletionListener listener, ErrorInfo msgErr) throws AblyException {
Log.v(TAG, "attach(); channel = " + name);
if(!forceReattach) {
/* check preconditions */
Expand Down Expand Up @@ -249,7 +249,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, msgErr);
return;
}

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

setState(ChannelState.attaching, null);
setState(ChannelState.attaching, msgErr);
connectionManager.send(attachMessage, true, null);
} catch(AblyException e) {
throw e;
Expand Down Expand Up @@ -470,14 +470,14 @@ synchronized private void clearAttachTimers() {
}

private void attachWithTimeout(final CompletionListener listener) throws AblyException {
this.attachWithTimeout(false, listener);
this.attachWithTimeout(false, listener, null);
}

/**
* Attach channel, if not attached within timeout set state to suspended and
* set up timer to reattach it later
*/
synchronized private void attachWithTimeout(final boolean forceReattach, final CompletionListener listener) {
synchronized private void attachWithTimeout(final boolean forceReattach, final CompletionListener listener, ErrorInfo msgErr) {
checkChannelIsNotReleased();
Timer currentAttachTimer;
try {
Expand All @@ -502,7 +502,7 @@ public void onError(ErrorInfo reason) {
clearAttachTimers();
callCompletionListenerError(listener, reason);
}
});
}, msgErr);
} catch(AblyException e) {
attachTimer = null;
callCompletionListenerError(listener, e.errorInfo);
Expand Down Expand Up @@ -1303,7 +1303,7 @@ void onChannelMessage(ProtocolMessage msg) {
case suspended:
/* Unexpected detach, reattach immediately as per RTL13a */
Log.v(TAG, String.format(Locale.ROOT, "Server initiated detach for channel %s; attempting reattach", name));
attachWithTimeout(true, null);
attachWithTimeout(true, null, msg.error);
break;
case attaching:
/* RTL13b says we need to be suspended, but continue to retry */
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 0dfc6c3

Please sign in to comment.