Skip to content

Commit

Permalink
Added tests for channel/presence subscribe without implicit attach
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 17, 2024
1 parent d83d3ae commit d8181e8
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/src/main/java/io/ably/lib/realtime/Presence.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ public void unsubscribe() {
*/
private void implicitAttachOnSubscribe(CompletionListener completionListener) throws AblyException {
if (!channel.attachOnSubscribeEnabled()) {
completionListener.onSuccess();
if (completionListener != null) {
completionListener.onSuccess();
}
return;
}
if (channel.state == ChannelState.failed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,68 @@ public void onMessage(Message message) {
}
}

/**
* <p>
* Validates a client can subscribe to messages without implicit channel attach
* Refer Spec TB4, RTL7g, RTL7gh
* </p>
* @throws AblyException
*/
@Test
public void subscribe_without_implicit_attach() {
String channelName = "subscribe_" + testParams.name;
AblyRealtime ably = null;
try {
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
ably = new AblyRealtime(opts);

/* create a channel and set attachOnSubscribe to false */
final Channel channel = ably.channels.get(channelName);
ChannelOptions chOpts = new ChannelOptions();
chOpts.attachOnSubscribe = false;
channel.setOptions(chOpts);

List<Object> receivedMsg = new ArrayList<>();

/* Check for all subscriptions without ATTACHING state */
channel.subscribe(message -> receivedMsg.add(true));
assertEquals(channel.state, ChannelState.initialized);

channel.subscribe("test_event", message -> receivedMsg.add(true));
assertEquals(channel.state, ChannelState.initialized);

channel.subscribe(new String[]{"test_event1", "test_event2"}, message -> receivedMsg.add(true));
assertEquals(channel.state, ChannelState.initialized);

channel.attach();
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);

channel.publish("test_event", "hi there");
Exception conditionError = new Helpers.ConditionalWaiter().
wait(() -> receivedMsg.size() == 2, 5000);
assertNull(conditionError);

receivedMsg.clear();
channel.publish("test_event1", "hi there");
conditionError = new Helpers.ConditionalWaiter().
wait(() -> receivedMsg.size() == 2, 5000);
assertNull(conditionError);

receivedMsg.clear();
channel.publish("test_event2", "hi there");
conditionError = new Helpers.ConditionalWaiter().
wait(() -> receivedMsg.size() == 2, 5000);
assertNull(conditionError);

} catch (AblyException e) {
e.printStackTrace();
fail("init0: Unexpected exception instantiating library");
} finally {
if(ably != null)
ably.close();
}
}

/**
* <p>
* Verifies that unsubscribe call with no argument removes all listeners,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,66 @@ public void onPresenceMessage(PresenceMessage message) {
}
}

/**
* <p>
* Validates a client can subscribe to presence without implicit channel attach
* Refer Spec TB4, RTP6d, RTP6e
* </p>
* @throws AblyException
*/
@Test
public void presence_subscribe_without_implicit_attach() {
String ablyChannel = "subscribe_" + testParams.name;
AblyRealtime ably = null;
try {
ClientOptions option1 = createOptions(testVars.keys[0].keyStr);
option1.clientId = "client1";
ably = new AblyRealtime(option1);

/* create a channel and set attachOnSubscribe to false */
final Channel channel = ably.channels.get(ablyChannel);
ChannelOptions chOpts = new ChannelOptions();
chOpts.attachOnSubscribe = false;
channel.setOptions(chOpts);

List<Object> receivedPresenceMsg = new ArrayList<>();
CompletionWaiter completionWaiter = new CompletionWaiter();

/* Check for all subscriptions without ATTACHING state */
channel.presence.subscribe(m -> receivedPresenceMsg.add(true), completionWaiter);
assertEquals(completionWaiter.successCount, 1);
assertEquals(channel.state, ChannelState.initialized);

channel.presence.subscribe(Action.enter, m -> receivedPresenceMsg.add(true), completionWaiter);
assertEquals(completionWaiter.successCount, 2);
assertEquals(channel.state, ChannelState.initialized);

channel.presence.subscribe(EnumSet.of(Action.enter, Action.leave),m -> receivedPresenceMsg.add(true));
assertEquals(channel.state, ChannelState.initialized);

channel.attach();
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);

channel.presence.enter("enter client1", null);
Exception conditionError = new Helpers.ConditionalWaiter().
wait(() -> receivedPresenceMsg.size() == 3, 5000);
assertNull(conditionError);

receivedPresenceMsg.clear();
channel.presence.leave(null);
conditionError = new Helpers.ConditionalWaiter().
wait(() -> receivedPresenceMsg.size() == 2, 5000);
assertNull(conditionError);

} catch (AblyException e) {
e.printStackTrace();
fail("init0: Unexpected exception instantiating library");
} finally {
if(ably != null)
ably.close();
}
}

/**
* <p>
* Validates a client sending multiple presence updates when the channel is in the attaching
Expand Down

0 comments on commit d8181e8

Please sign in to comment.