Skip to content

Commit fbe96a8

Browse files
committed
Polishing contribution
Closes gh-35224
1 parent b89dcb1 commit fbe96a8

File tree

3 files changed

+14
-35
lines changed

3 files changed

+14
-35
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ private Receiptable unsubscribe(String id, @Nullable StompHeaders headers) {
351351
if (headers == null){
352352
headers = new StompHeaders();
353353
}
354-
355354
String receiptId = checkOrAddReceipt(headers);
356355
Receiptable receiptable = new ReceiptHandler(receiptId);
357356

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompSession.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,15 @@ interface Subscription extends Receiptable {
182182

183183
/**
184184
* Remove the subscription by sending an UNSUBSCRIBE frame.
185+
* <p>As of 7.0, this method returns {@link Receiptable}.
185186
*/
186187
Receiptable unsubscribe();
187188

188189
/**
189190
* Alternative to {@link #unsubscribe()} with additional custom headers
190-
* to send to the server.
191-
* <p><strong>Note:</strong> There is no need to set the subscription id.
191+
* to send to the server. Note, however, there is no need to set the
192+
* subscription id.
193+
* <p>As of 7.0, this method returns {@link Receiptable}.
192194
* @param headers the custom headers, if any
193195
* @since 5.0
194196
*/

spring-messaging/src/test/java/org/springframework/messaging/simp/stomp/DefaultStompSessionTests.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -664,70 +664,48 @@ public void receiptNotReceived() {
664664
}
665665

666666
@Test
667-
void unsubscribeWithReceipt() {
667+
void unsubscribeWithoutReceipt() {
668668
this.session.afterConnected(this.connection);
669669
assertThat(this.session.isConnected()).isTrue();
670670
Subscription subscription = this.session.subscribe("/topic/foo", mock());
671671

672672
Receiptable receipt = subscription.unsubscribe();
673673
assertThat(receipt).isNotNull();
674674
assertThat(receipt.getReceiptId()).isNull();
675-
676-
Message<byte[]> message = this.messageCaptor.getValue();
677-
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
678-
assertThat(accessor.getCommand()).isEqualTo(StompCommand.UNSUBSCRIBE);
679-
680-
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
681-
assertThat(stompHeaders).hasSize(1);
682-
assertThat(stompHeaders.getId()).isEqualTo(subscription.getSubscriptionId());
683675
}
684676

685677
@Test
686-
void unsubscribeWithCustomHeaderAndReceipt() {
678+
void unsubscribeWithReceipt() {
687679
this.session.afterConnected(this.connection);
688680
this.session.setTaskScheduler(mock());
689681
this.session.setAutoReceipt(true);
682+
Subscription subscription = this.session.subscribe("/topic/foo", mock());
690683

691-
StompHeaders subHeaders = new StompHeaders();
692-
subHeaders.setDestination("/topic/foo");
693-
Subscription subscription = this.session.subscribe(subHeaders, mock());
694-
695-
StompHeaders custom = new StompHeaders();
696-
custom.set("x-cust", "value");
697-
698-
Receiptable receipt = subscription.unsubscribe(custom);
684+
Receiptable receipt = subscription.unsubscribe();
699685
assertThat(receipt).isNotNull();
700686
assertThat(receipt.getReceiptId()).isNotNull();
701687

702688
Message<byte[]> message = this.messageCaptor.getValue();
703689
StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
704-
assertThat(accessor.getCommand()).isEqualTo(StompCommand.UNSUBSCRIBE);
705-
706-
StompHeaders stompHeaders = StompHeaders.readOnlyStompHeaders(accessor.getNativeHeaders());
707-
assertThat(stompHeaders.getId()).isEqualTo(subscription.getSubscriptionId());
708-
assertThat(stompHeaders.get("x-cust")).containsExactly("value");
709-
assertThat(stompHeaders.getReceipt()).isEqualTo(receipt.getReceiptId());
690+
assertThat(accessor.getReceipt()).isEqualTo(receipt.getReceiptId());
710691
}
711692

712693
@Test
713694
void receiptReceivedOnUnsubscribe() {
714695
this.session.afterConnected(this.connection);
715-
TaskScheduler scheduler = mock();
716-
this.session.setTaskScheduler(scheduler);
696+
this.session.setTaskScheduler(mock());
717697
this.session.setAutoReceipt(true);
718698

719699
Subscription subscription = this.session.subscribe("/topic/foo", mock());
720700
Receiptable receipt = subscription.unsubscribe();
721701

722-
StompHeaderAccessor ack = StompHeaderAccessor.create(StompCommand.RECEIPT);
723-
ack.setReceiptId(receipt.getReceiptId());
724-
ack.setLeaveMutable(true);
725-
Message<byte[]> receiptMessage = MessageBuilder.createMessage(new byte[0], ack.getMessageHeaders());
726-
727702
AtomicBoolean called = new AtomicBoolean(false);
728703
receipt.addReceiptTask(() -> called.set(true));
729704

730-
this.session.handleMessage(receiptMessage);
705+
StompHeaderAccessor ack = StompHeaderAccessor.create(StompCommand.RECEIPT);
706+
ack.setReceiptId(receipt.getReceiptId());
707+
ack.setLeaveMutable(true);
708+
this.session.handleMessage(MessageBuilder.createMessage(new byte[0], ack.getMessageHeaders()));
731709

732710
assertThat(called.get()).isTrue();
733711
}

0 commit comments

Comments
 (0)