From d8181e874b980a20be2210019c557c92105938eb Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 17 Sep 2024 15:03:37 +0530 Subject: [PATCH] Added tests for channel/presence subscribe without implicit attach --- .../java/io/ably/lib/realtime/Presence.java | 4 +- .../test/realtime/RealtimeChannelTest.java | 62 +++++++++++++++++++ .../test/realtime/RealtimePresenceTest.java | 60 ++++++++++++++++++ 3 files changed, 125 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/io/ably/lib/realtime/Presence.java b/lib/src/main/java/io/ably/lib/realtime/Presence.java index cae2ec845..504985e98 100644 --- a/lib/src/main/java/io/ably/lib/realtime/Presence.java +++ b/lib/src/main/java/io/ably/lib/realtime/Presence.java @@ -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) { diff --git a/lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java b/lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java index f58fe832f..82763a62e 100644 --- a/lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java +++ b/lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java @@ -393,6 +393,68 @@ public void onMessage(Message message) { } } + /** + *

+ * Validates a client can subscribe to messages without implicit channel attach + * Refer Spec TB4, RTL7g, RTL7gh + *

+ * @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 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(); + } + } + /** *

* Verifies that unsubscribe call with no argument removes all listeners, diff --git a/lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java b/lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java index 9fe903675..c5699be08 100644 --- a/lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java +++ b/lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java @@ -1624,6 +1624,66 @@ public void onPresenceMessage(PresenceMessage message) { } } + /** + *

+ * Validates a client can subscribe to presence without implicit channel attach + * Refer Spec TB4, RTP6d, RTP6e + *

+ * @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 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(); + } + } + /** *

* Validates a client sending multiple presence updates when the channel is in the attaching