Skip to content

Commit d8181e8

Browse files
committed
Added tests for channel/presence subscribe without implicit attach
1 parent d83d3ae commit d8181e8

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

lib/src/main/java/io/ably/lib/realtime/Presence.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ public void unsubscribe() {
309309
*/
310310
private void implicitAttachOnSubscribe(CompletionListener completionListener) throws AblyException {
311311
if (!channel.attachOnSubscribeEnabled()) {
312-
completionListener.onSuccess();
312+
if (completionListener != null) {
313+
completionListener.onSuccess();
314+
}
313315
return;
314316
}
315317
if (channel.state == ChannelState.failed) {

lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,68 @@ public void onMessage(Message message) {
393393
}
394394
}
395395

396+
/**
397+
* <p>
398+
* Validates a client can subscribe to messages without implicit channel attach
399+
* Refer Spec TB4, RTL7g, RTL7gh
400+
* </p>
401+
* @throws AblyException
402+
*/
403+
@Test
404+
public void subscribe_without_implicit_attach() {
405+
String channelName = "subscribe_" + testParams.name;
406+
AblyRealtime ably = null;
407+
try {
408+
ClientOptions opts = createOptions(testVars.keys[0].keyStr);
409+
ably = new AblyRealtime(opts);
410+
411+
/* create a channel and set attachOnSubscribe to false */
412+
final Channel channel = ably.channels.get(channelName);
413+
ChannelOptions chOpts = new ChannelOptions();
414+
chOpts.attachOnSubscribe = false;
415+
channel.setOptions(chOpts);
416+
417+
List<Object> receivedMsg = new ArrayList<>();
418+
419+
/* Check for all subscriptions without ATTACHING state */
420+
channel.subscribe(message -> receivedMsg.add(true));
421+
assertEquals(channel.state, ChannelState.initialized);
422+
423+
channel.subscribe("test_event", message -> receivedMsg.add(true));
424+
assertEquals(channel.state, ChannelState.initialized);
425+
426+
channel.subscribe(new String[]{"test_event1", "test_event2"}, message -> receivedMsg.add(true));
427+
assertEquals(channel.state, ChannelState.initialized);
428+
429+
channel.attach();
430+
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
431+
432+
channel.publish("test_event", "hi there");
433+
Exception conditionError = new Helpers.ConditionalWaiter().
434+
wait(() -> receivedMsg.size() == 2, 5000);
435+
assertNull(conditionError);
436+
437+
receivedMsg.clear();
438+
channel.publish("test_event1", "hi there");
439+
conditionError = new Helpers.ConditionalWaiter().
440+
wait(() -> receivedMsg.size() == 2, 5000);
441+
assertNull(conditionError);
442+
443+
receivedMsg.clear();
444+
channel.publish("test_event2", "hi there");
445+
conditionError = new Helpers.ConditionalWaiter().
446+
wait(() -> receivedMsg.size() == 2, 5000);
447+
assertNull(conditionError);
448+
449+
} catch (AblyException e) {
450+
e.printStackTrace();
451+
fail("init0: Unexpected exception instantiating library");
452+
} finally {
453+
if(ably != null)
454+
ably.close();
455+
}
456+
}
457+
396458
/**
397459
* <p>
398460
* Verifies that unsubscribe call with no argument removes all listeners,

lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,66 @@ public void onPresenceMessage(PresenceMessage message) {
16241624
}
16251625
}
16261626

1627+
/**
1628+
* <p>
1629+
* Validates a client can subscribe to presence without implicit channel attach
1630+
* Refer Spec TB4, RTP6d, RTP6e
1631+
* </p>
1632+
* @throws AblyException
1633+
*/
1634+
@Test
1635+
public void presence_subscribe_without_implicit_attach() {
1636+
String ablyChannel = "subscribe_" + testParams.name;
1637+
AblyRealtime ably = null;
1638+
try {
1639+
ClientOptions option1 = createOptions(testVars.keys[0].keyStr);
1640+
option1.clientId = "client1";
1641+
ably = new AblyRealtime(option1);
1642+
1643+
/* create a channel and set attachOnSubscribe to false */
1644+
final Channel channel = ably.channels.get(ablyChannel);
1645+
ChannelOptions chOpts = new ChannelOptions();
1646+
chOpts.attachOnSubscribe = false;
1647+
channel.setOptions(chOpts);
1648+
1649+
List<Object> receivedPresenceMsg = new ArrayList<>();
1650+
CompletionWaiter completionWaiter = new CompletionWaiter();
1651+
1652+
/* Check for all subscriptions without ATTACHING state */
1653+
channel.presence.subscribe(m -> receivedPresenceMsg.add(true), completionWaiter);
1654+
assertEquals(completionWaiter.successCount, 1);
1655+
assertEquals(channel.state, ChannelState.initialized);
1656+
1657+
channel.presence.subscribe(Action.enter, m -> receivedPresenceMsg.add(true), completionWaiter);
1658+
assertEquals(completionWaiter.successCount, 2);
1659+
assertEquals(channel.state, ChannelState.initialized);
1660+
1661+
channel.presence.subscribe(EnumSet.of(Action.enter, Action.leave),m -> receivedPresenceMsg.add(true));
1662+
assertEquals(channel.state, ChannelState.initialized);
1663+
1664+
channel.attach();
1665+
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
1666+
1667+
channel.presence.enter("enter client1", null);
1668+
Exception conditionError = new Helpers.ConditionalWaiter().
1669+
wait(() -> receivedPresenceMsg.size() == 3, 5000);
1670+
assertNull(conditionError);
1671+
1672+
receivedPresenceMsg.clear();
1673+
channel.presence.leave(null);
1674+
conditionError = new Helpers.ConditionalWaiter().
1675+
wait(() -> receivedPresenceMsg.size() == 2, 5000);
1676+
assertNull(conditionError);
1677+
1678+
} catch (AblyException e) {
1679+
e.printStackTrace();
1680+
fail("init0: Unexpected exception instantiating library");
1681+
} finally {
1682+
if(ably != null)
1683+
ably.close();
1684+
}
1685+
}
1686+
16271687
/**
16281688
* <p>
16291689
* Validates a client sending multiple presence updates when the channel is in the attaching

0 commit comments

Comments
 (0)