-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix implicit attach on subscribe #1028
Conversation
Warning Rate limit exceeded@sacOO7 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 0 seconds before requesting another review. How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. WalkthroughThe changes introduce a new configuration option, Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
871197b
to
0436cd9
Compare
0436cd9
to
d8181e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Caution
Inline review comments failed to post
Actionable comments posted: 8
Outside diff range and nitpick comments (3)
lib/src/main/java/io/ably/lib/realtime/ChannelBase.java (1)
693-701
: Ensure consistent terminology in commentsThe JavaDoc comment refers to
channelOptions
, but the code usesoptions
. For clarity and consistency, consider updating the comment to useoptions
to match the code.Apply this diff to update the comment:
/** - * Checks for null channelOptions and checks if options.attachOnSubscribe is true + * Checks for null options and checks if options.attachOnSubscribe is true - * Defaults to @true@ when channelOptions is null. + * Defaults to @true@ when options is null. * Spec: TB4, RTL7g, RTL7gh, RTP6d, RTP6e */lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java (2)
417-417
: Specify generic type forList
to enhance type safetyThe
receivedMsg
list is currently declared with a raw typeList<Object>
. Since you're addingBoolean
values, specifying the generic type asBoolean
improves type safety and readability.Apply this diff:
-List<Object> receivedMsg = new ArrayList<>(); +List<Boolean> receivedMsg = new ArrayList<>();
449-451
: Update error message in catch block for clarityThe error message in the
catch
block refers to "init0," which may not accurately reflect the context of this test method. Consider updating the message to provide a clearer description of the exception.Apply this diff:
-fail("init0: Unexpected exception instantiating library"); +fail("subscribe_without_implicit_attach: Unexpected exception");
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (5)
- lib/src/main/java/io/ably/lib/realtime/ChannelBase.java (4 hunks)
- lib/src/main/java/io/ably/lib/realtime/Presence.java (1 hunks)
- lib/src/main/java/io/ably/lib/types/ChannelOptions.java (1 hunks)
- lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java (1 hunks)
- lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java (1 hunks)
Additional comments not posted (2)
lib/src/main/java/io/ably/lib/realtime/ChannelBase.java (2)
753-755
: Duplication of conditionalattach()
logicThis code block duplicates the
attach()
logic found in othersubscribe
methods. Refer to the earlier refactoring suggestion to extract this logic into a common method and improve code maintainability.
789-791
: Duplication of conditionalattach()
logicThis code block duplicates the
attach()
logic found in othersubscribe
methods. Refer to the earlier refactoring suggestion to extract this logic into a common method and improve code maintainability.
Comments failed to post (8)
lib/src/main/java/io/ably/lib/types/ChannelOptions.java (2)
48-48: Encapsulate 'attachOnSubscribe' field with getter and setter methods
Consider making the
attachOnSubscribe
field private and providing public getter and setter methods. This follows best practices for encapsulation and allows for additional control or validation when accessing or modifying the field.Suggested changes:
- public boolean attachOnSubscribe = true; + private boolean attachOnSubscribe = true; + + public boolean isAttachOnSubscribe() { + return attachOnSubscribe; + } + + public void setAttachOnSubscribe(boolean attachOnSubscribe) { + this.attachOnSubscribe = attachOnSubscribe; + }Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.private boolean attachOnSubscribe = true; public boolean isAttachOnSubscribe() { return attachOnSubscribe; } public void setAttachOnSubscribe(boolean attachOnSubscribe) { this.attachOnSubscribe = attachOnSubscribe; }
44-46: Use proper JavaDoc formatting for code references
In the JavaDoc comments, use
{@code ...}
for code snippets or values, and{@link ...}
for method references. Replace@subscribe@
and@true@
with the appropriate JavaDoc syntax.Suggested changes:
- * Determines whether calling @subscribe@ on a channel or presence object should trigger an implicit attach. - * Defaults to @true@. + * Determines whether calling {@link io.ably.lib.realtime.Channel#subscribe} on a channel or presence object should trigger an implicit attach. + * Defaults to {@code true}.Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.* Determines whether calling {@link io.ably.lib.realtime.Channel#subscribe} on a channel or presence object should trigger an implicit attach. * Defaults to {@code true}. * Spec: TB4, RTL7g, RTL7gh, RTP6d, RTP6e
lib/src/main/java/io/ably/lib/realtime/Presence.java (1)
311-316: Ensure channel state is checked even when
attachOnSubscribe
is disabledIn the modified code, when
channel.attachOnSubscribeEnabled()
returns false, the method exits early without checking if the channel state isfailed
. This omission could allow operations to proceed on a failed channel, leading to unexpected behavior or unhandled exceptions. To maintain consistent error handling, the check forchannel.state == ChannelState.failed
should occur regardless of theattachOnSubscribe
setting.Apply this diff to include the channel state check before returning:
if (!channel.attachOnSubscribeEnabled()) { + if (channel.state == ChannelState.failed) { + String errorString = String.format(Locale.ROOT, "Channel %s: subscribe in FAILED channel state", channel.name); + Log.v(TAG, errorString); + ErrorInfo errorInfo = new ErrorInfo(errorString, 90001); + throw AblyException.fromErrorInfo(errorInfo); + } if (completionListener != null) { completionListener.onSuccess(); } return; }Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.if (!channel.attachOnSubscribeEnabled()) { if (channel.state == ChannelState.failed) { String errorString = String.format(Locale.ROOT, "Channel %s: subscribe in FAILED channel state", channel.name); Log.v(TAG, errorString); ErrorInfo errorInfo = new ErrorInfo(errorString, 90001); throw AblyException.fromErrorInfo(errorInfo); } if (completionListener != null) { completionListener.onSuccess(); } return; }
lib/src/main/java/io/ably/lib/realtime/ChannelBase.java (1)
716-718: Consider refactoring to eliminate code duplication
The conditional
attach()
call based onattachOnSubscribeEnabled()
is repeated in multiplesubscribe
methods. To improve maintainability and reduce duplication, consider extracting this logic into a private helper method.Apply this diff to refactor the repeated code:
+private void attachIfNeeded() throws AblyException { + if (attachOnSubscribeEnabled()) { + attach(); + } +} public synchronized void subscribe(MessageListener listener) throws AblyException { Log.v(TAG, "subscribe(); channel = " + this.name); listeners.add(listener); - if (attachOnSubscribeEnabled()) { - attach(); - } + attachIfNeeded(); }And similarly update the other
subscribe
methods to useattachIfNeeded()
:public synchronized void subscribe(String name, MessageListener listener) throws AblyException { Log.v(TAG, "subscribe(); channel = " + this.name + "; event = " + name); subscribeImpl(name, listener); - if (attachOnSubscribeEnabled()) { - attach(); - } + attachIfNeeded(); } public synchronized void subscribe(String[] names, MessageListener listener) throws AblyException { Log.v(TAG, "subscribe(); channel = " + this.name + "; (multiple events)"); for(String name : names) subscribeImpl(name, listener); - if (attachOnSubscribeEnabled()) { - attach(); - } + attachIfNeeded(); }Committable suggestion was skipped due to low confidence.
lib/src/test/java/io/ably/lib/test/realtime/RealtimeChannelTest.java (2)
420-420: Swap parameters in
assertEquals
methodThe
assertEquals
method expects the expected value as the first parameter and the actual value as the second. Currently, the parameters are reversed, which can lead to confusing error messages if the test fails.Apply the following diff to correct the parameter order:
-assertEquals(channel.state, ChannelState.initialized); +assertEquals(ChannelState.initialized, channel.state);Repeat this change for all instances where
assertEquals
is used with reversed parameters.Also applies to: 423-423, 426-426
419-419: Clarify the reasoning behind expecting two received messages
The test expects
receivedMsg.size() == 2
after publishing a message, which might be unclear to someone reading the code. Adding a comment to explain why two messages are expected can improve the test's readability and maintainability.For example, you could add a comment:
// Expecting two callbacks: one from the wildcard subscription and one from the specific event subscription
Also applies to: 422-422, 425-425
lib/src/test/java/io/ably/lib/test/realtime/RealtimePresenceTest.java (2)
1653-1654: Fix the order of arguments in
assertEquals
method callsIn JUnit's
assertEquals(expected, actual)
method, the first argument is the expected value, and the second is the actual value. Currently, the arguments are reversed in lines 1653, 1654, 1657, and 1658, which can lead to misleading assertion messages if a test fails.Apply this diff to correct the
assertEquals
calls:- assertEquals(completionWaiter.successCount, 1); + assertEquals(1, completionWaiter.successCount); - assertEquals(channel.state, ChannelState.initialized); + assertEquals(ChannelState.initialized, channel.state); - assertEquals(completionWaiter.successCount, 2); + assertEquals(2, completionWaiter.successCount); - assertEquals(channel.state, ChannelState.initialized); + assertEquals(ChannelState.initialized, channel.state);Also applies to: 1657-1658
1649-1652: Ensure thread-safe access to
receivedPresenceMsg
The
receivedPresenceMsg
list is accessed from multiple threads since thePresenceListener
callbacks may be invoked asynchronously. AsArrayList
is not thread-safe, this can lead to concurrency issues. Consider using a thread-safe collection likeCollections.synchronizedList(new ArrayList<>())
or aCopyOnWriteArrayList
.Apply this diff to make
receivedPresenceMsg
thread-safe:import java.util.ArrayList; +import java.util.Collections; ... - List<Object> receivedPresenceMsg = new ArrayList<>(); + List<Object> receivedPresenceMsg = Collections.synchronizedList(new ArrayList<>());Also applies to: 1666-1675
9728f6b
to
de01e91
Compare
…itcodeai comments
de01e91
to
3ecab48
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for the review |
Fixed #1027
Summary by CodeRabbit
New Features
attachOnSubscribe
inChannelOptions
to manage implicit attachment during subscriptions.Bug Fixes
Tests