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

GH-2478 Handle conversion exception in AsyncRabbitTemplate #2932

Merged
merged 3 commits into from
Dec 19, 2024
Merged
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 @@ -49,6 +49,7 @@
import org.springframework.amqp.rabbit.listener.DirectReplyToMessageListenerContainer.ChannelHolder;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SmartMessageConverter;
import org.springframework.amqp.utils.JavaUtils;
Expand Down Expand Up @@ -89,6 +90,7 @@
* @author Artem Bilan
* @author FengYang Su
* @author Ngoc Nhan
* @author Ben Efrati
*
* @since 1.6
*/
Expand Down Expand Up @@ -604,12 +606,17 @@ public void onMessage(Message message, Channel channel) {
if (future instanceof RabbitConverterFuture) {
MessageConverter messageConverter = this.template.getMessageConverter();
RabbitConverterFuture<Object> rabbitFuture = (RabbitConverterFuture<Object>) future;
Object converted = rabbitFuture.getReturnType() != null
try {
Object converted = rabbitFuture.getReturnType() != null
&& messageConverter instanceof SmartMessageConverter smart
? smart.fromMessage(message,
rabbitFuture.getReturnType())
: messageConverter.fromMessage(message);
rabbitFuture.complete(converted);
rabbitFuture.complete(converted);
}
catch (MessageConversionException e) {
rabbitFuture.completeExceptionally(e);
}
}
else {
((RabbitMessageFuture) future).complete(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.listener.adapter.ReplyingMessageListener;
import org.springframework.amqp.support.converter.MessageConversionException;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.amqp.support.postprocessor.GUnzipPostProcessor;
import org.springframework.amqp.support.postprocessor.GZipPostProcessor;
Expand All @@ -72,6 +73,7 @@
/**
* @author Gary Russell
* @author Artem Bilan
* @author Ben Efrati
*
* @since 1.6
*/
Expand Down Expand Up @@ -394,6 +396,29 @@ public void testStopCancelled() throws Exception {
assertThat(callback.result).isNull();
}

@Test
@DirtiesContext
artembilan marked this conversation as resolved.
Show resolved Hide resolved
public void testConversionException() throws InterruptedException {
this.asyncTemplate.getRabbitTemplate().setMessageConverter(new SimpleMessageConverter() {
@Override
public Object fromMessage(Message message) throws MessageConversionException {
throw new MessageConversionException("Failed to convert message");
}
});

RabbitConverterFuture<String> replyFuture = this.asyncTemplate.convertSendAndReceive("conversionException");

final CountDownLatch cdl = new CountDownLatch(1);
final AtomicReference<Object> resultRef = new AtomicReference<>();
replyFuture.whenComplete((result, ex) -> {
resultRef.set(result);
cdl.countDown();
});
assertThat(cdl.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(replyFuture).isCompletedExceptionally();
Copy link
Member

Choose a reason for hiding this comment

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

May we also verify what exactly exception we got here?

Copy link
Contributor Author

@BenEfrati BenEfrati Dec 19, 2024

Choose a reason for hiding this comment

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

Suggested change
final CountDownLatch cdl = new CountDownLatch(1);
final AtomicReference<Object> resultRef = new AtomicReference<>();
replyFuture.whenComplete((result, ex) -> {
resultRef.set(result);
cdl.countDown();
});
assertThat(cdl.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(replyFuture).isCompletedExceptionally();
assertThat(replyFuture).failsWithin(Duration.ofSeconds(10))
.withThrowableThat()
.withCauseInstanceOf(MessageConversionException.class);

Something like this?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good!
But does not look like it would be a good candidate to commit such a suggestion: too many broken indents.

Copy link
Member

Choose a reason for hiding this comment

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

Or never mind. I like it.
You have changed the logic, but at the same it is covered with that failsWithin() 😄
Committing and merging.
Thank you!

assertThat(resultRef.get()).isNull();
Copy link
Member

Choose a reason for hiding this comment

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

But this line has to be removed then 😄

Copy link
Contributor Author

@BenEfrati BenEfrati Dec 19, 2024

Choose a reason for hiding this comment

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

sure :)

Do you want me to align other tests to use failsWithin

void testReturn() {
...
		assertThat(future)
				.as("Expected exception")
				.failsWithin(Duration.ofSeconds(10))
				.withThrowableThat()
				.withCauseInstanceOf(AmqpMessageReturnedException.class)
				.extracting("routingKey")
				.isEqualTo(this.requests.getName() + "x");

instead

try {
			future.get(10, TimeUnit.SECONDS);
			fail("Expected exception");
		}
		catch (ExecutionException e) {
			assertThat(e.getCause()).isInstanceOf(AmqpMessageReturnedException.class);
			assertThat(((AmqpMessageReturnedException) e.getCause()).getRoutingKey()).isEqualTo(this.requests.getName() + "x");
		}

Copy link
Member

Choose a reason for hiding this comment

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

I would appreciate.
Thanks.
Those try..catch in tests is on my TODO list in favor of assertThat() when I touch these classes.
Although still would be with some code.
But now I have learned about failsWithin() which looks much cleaner. 😄

}

@Test
void ctorCoverage() {
AsyncRabbitTemplate template = new AsyncRabbitTemplate(mock(ConnectionFactory.class), "ex", "rk");
Expand Down
Loading