Skip to content

Commit

Permalink
test: fix RealtimeChannelTest channel_resume_lost_continuity
Browse files Browse the repository at this point in the history
This test was failing because of the order in which listeners were registered. The channel waiters
were created (and thus registered as listeners) before the listeners that add state changes to the
arrays ready for assertions. EventEmitter runs listeners that don't have a filter in order of registration.

This meant that sometimes, the test would run, the waiter would be informed of a channel state change, thus allowing
it to proceed into the assertions phase on the main thread, and only then would the subjects of those assertions be updated.

So you can end up in a situation where the main thread would start asserting on things before all the listeners had run.

This change fixes the issue by ensuring the waiters are registered after the main listeners, so that the assertions will
always run once all the states that are going to be received, are received.

You can test this behaviour by adding a sleep just before the line where attachingStateReached[0] is set to true. If you remove
the change in this commit, the test will fail.

Fixes #945
  • Loading branch information
AndyTWF committed May 24, 2023
1 parent 47e7cde commit 55f6c0d
Showing 1 changed file with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1560,13 +1560,12 @@ public void channel_resume_lost_continuity() throws AblyException {

/* prepare channels */
Channel attachedChannel = ably.channels.get(attachedChannelName);
ChannelWaiter attachedChannelWaiter = new ChannelWaiter(attachedChannel);
ChannelWaiter preAttachChannelWaiter = new ChannelWaiter(attachedChannel);
attachedChannel.attach();
attachedChannelWaiter.waitFor(ChannelState.attached);
preAttachChannelWaiter.waitFor(ChannelState.attached);

Channel suspendedChannel = ably.channels.get(suspendedChannelName);
suspendedChannel.state = ChannelState.suspended;
ChannelWaiter suspendedChannelWaiter = new ChannelWaiter(suspendedChannel);

final boolean[] suspendedStateReached = new boolean[2];
final boolean[] attachingStateReached = new boolean[2];
Expand Down Expand Up @@ -1608,6 +1607,17 @@ public void onChannelStateChanged(ChannelStateChange stateChange) {
}
});

/*
We need to set up the waiters after the above state listeners, as listeners
without filters are run in the order registered.
This ensures that the various state changes have been recorded by the above
listeners, before the waiters are notified of a state change, thus allowing
the main test to continue.
*/
ChannelWaiter attachedChannelWaiter = new ChannelWaiter(attachedChannel);
ChannelWaiter suspendedChannelWaiter = new ChannelWaiter(suspendedChannel);

/* disconnect, and sabotage the resume */
String originalConnectionId = ably.connection.id;
ably.connection.key = "_____!ably___test_fake-key____";
Expand Down

0 comments on commit 55f6c0d

Please sign in to comment.