Skip to content

Commit 3ecab48

Browse files
committed
Refactored attachOnSubscribe code, spec and related tests as per rabbitcodeai comments
1 parent d8181e8 commit 3ecab48

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,11 @@ public synchronized void unsubscribe() {
691691
}
692692

693693
/**
694-
* Checks for null channelOptions and checks if options.attachOnSubscribe is true
695-
* Defaults to @true@ when channelOptions is null.
696-
* Spec: TB4, RTL7g, RTL7gh, RTP6d, RTP6e
694+
* <p>
695+
* Checks if {@link io.ably.lib.types.ChannelOptions#attachOnSubscribe} is true.
696+
* </p>
697+
* Defaults to {@code true} when {@link io.ably.lib.realtime.ChannelBase#options} is null.
698+
* <p>Spec: TB4, RTL7g, RTL7gh, RTP6d, RTP6e</p>
697699
*/
698700
protected boolean attachOnSubscribeEnabled() {
699701
return options == null || options.attachOnSubscribe;

lib/src/main/java/io/ably/lib/types/ChannelOptions.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ public class ChannelOptions {
4141
public boolean encrypted;
4242

4343
/**
44-
* Determines whether calling @subscribe@ on a channel or presence object should trigger an implicit attach.
45-
* Defaults to @true@.
46-
* Spec: TB4, RTL7g, RTL7gh, RTP6d, RTP6e
44+
* <p>
45+
* Determines whether calling {@link io.ably.lib.realtime.Channel#subscribe Channel.subscribe} or
46+
* {@link io.ably.lib.realtime.Presence#subscribe Presence.subscribe} method
47+
* should trigger an implicit attach.
48+
* </p>
49+
* <p>Defaults to {@code true}.</p>
50+
* <p>Spec: TB4, RTL7g, RTL7gh, RTP6d, RTP6e</p>
4751
*/
4852
public boolean attachOnSubscribe = true;
4953

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,41 +414,44 @@ public void subscribe_without_implicit_attach() {
414414
chOpts.attachOnSubscribe = false;
415415
channel.setOptions(chOpts);
416416

417-
List<Object> receivedMsg = new ArrayList<>();
417+
List<Boolean> receivedMsg = Collections.synchronizedList(new ArrayList<>());
418418

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

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

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

429429
channel.attach();
430430
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
431431

432432
channel.publish("test_event", "hi there");
433+
// Expecting two msg: one from the wildcard subscription and one from test_event subscription
433434
Exception conditionError = new Helpers.ConditionalWaiter().
434435
wait(() -> receivedMsg.size() == 2, 5000);
435436
assertNull(conditionError);
436437

437438
receivedMsg.clear();
438439
channel.publish("test_event1", "hi there");
440+
// Expecting two msg: one from the wildcard subscription and one from test_event1 subscription
439441
conditionError = new Helpers.ConditionalWaiter().
440442
wait(() -> receivedMsg.size() == 2, 5000);
441443
assertNull(conditionError);
442444

443445
receivedMsg.clear();
444446
channel.publish("test_event2", "hi there");
447+
// Expecting two msg: one from the wildcard subscription and one from test_event2 subscription
445448
conditionError = new Helpers.ConditionalWaiter().
446449
wait(() -> receivedMsg.size() == 2, 5000);
447450
assertNull(conditionError);
448451

449452
} catch (AblyException e) {
450453
e.printStackTrace();
451-
fail("init0: Unexpected exception instantiating library");
454+
fail("subscribe_without_implicit_attach: Unexpected exception");
452455
} finally {
453456
if(ably != null)
454457
ably.close();

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,38 +1646,40 @@ public void presence_subscribe_without_implicit_attach() {
16461646
chOpts.attachOnSubscribe = false;
16471647
channel.setOptions(chOpts);
16481648

1649-
List<Object> receivedPresenceMsg = new ArrayList<>();
1649+
List<Boolean> receivedPresenceMsg = Collections.synchronizedList(new ArrayList<>());
16501650
CompletionWaiter completionWaiter = new CompletionWaiter();
16511651

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

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

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

16641664
channel.attach();
16651665
(new ChannelWaiter(channel)).waitFor(ChannelState.attached);
16661666

16671667
channel.presence.enter("enter client1", null);
1668+
// Expecting 3 msg: one from the wildcard subscription and two from specific event subscription
16681669
Exception conditionError = new Helpers.ConditionalWaiter().
16691670
wait(() -> receivedPresenceMsg.size() == 3, 5000);
16701671
assertNull(conditionError);
16711672

16721673
receivedPresenceMsg.clear();
16731674
channel.presence.leave(null);
1675+
// Expecting 2 msg: one from the wildcard subscription and one from specific event subscription
16741676
conditionError = new Helpers.ConditionalWaiter().
16751677
wait(() -> receivedPresenceMsg.size() == 2, 5000);
16761678
assertNull(conditionError);
16771679

16781680
} catch (AblyException e) {
16791681
e.printStackTrace();
1680-
fail("init0: Unexpected exception instantiating library");
1682+
fail("presence_subscribe_without_implicit_attach: Unexpected exception");
16811683
} finally {
16821684
if(ably != null)
16831685
ably.close();

0 commit comments

Comments
 (0)