Skip to content
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

Check hasNext when looking for ordered UserDestinationResult sessionIds #34333

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void send(UserDestinationResult destinationResult, Message<?> message) th
Iterator<String> itr = (sessionIds != null ? sessionIds.iterator() : null);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, sessionIds coming from destinationResult.getSessionIds() is documented to be nullable, but actually the sessionIds are final and instantiated to always be non-null. That method could be annotated @nonnull and the logic here would be simpler. sessionIds doesn't have to be checked for null in this line, and therefore itr is never null and doesn't need to be check for null in line 283.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I'll make that change just so it's up for viewing and we can get some feedback from @rstoyanchev.


for (String target : destinationResult.getTargetDestinations()) {
String sessionId = (itr != null ? itr.next() : null);
String sessionId = (itr != null && itr.hasNext() ? itr.next() : null);
getTemplateToUse(sessionId).send(target, message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.messaging.simp.user;

import java.nio.charset.StandardCharsets;
import java.util.Set;

import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -98,6 +99,26 @@ void handleMessage() {
assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
}

@Test
@SuppressWarnings("rawtypes")
void handleMessageWithoutSessionIds() {
UserDestinationResolver resolver = mock();
Message message = createWith(SimpMessageType.MESSAGE, "joe", null, "/user/joe/queue/foo");
UserDestinationResult result = new UserDestinationResult("/queue/foo-user123", Set.of("/queue/foo-user123"), "/user/queue/foo", "joe");
given(resolver.resolveDestination(message)).willReturn(result);

given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
UserDestinationMessageHandler handler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, resolver);
handler.handleMessage(message);

ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
Mockito.verify(this.brokerChannel).send(captor.capture());

SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
assertThat(accessor.getDestination()).isEqualTo("/queue/foo-user123");
assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
}

@Test
@SuppressWarnings("rawtypes")
void handleMessageWithoutActiveSession() {
Expand Down