提交 a106e06a 编写于 作者: R Rossen Stoyanchev

Expose failing test in GenericMessagingTemplateTests

Assertions made in callbacks invoked on separate thread were not
reaching the test framework. This fix ensures failures are detected
and cause tests to fail.
上级 3ffc3d44
...@@ -18,6 +18,7 @@ package org.springframework.messaging.core; ...@@ -18,6 +18,7 @@ package org.springframework.messaging.core;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -56,6 +57,7 @@ public class GenericMessagingTemplateTests { ...@@ -56,6 +57,7 @@ public class GenericMessagingTemplateTests {
@Test @Test
public void sendAndReceive() { public void sendAndReceive() {
SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor); SubscribableChannel channel = new ExecutorSubscribableChannel(this.executor);
channel.subscribe(new MessageHandler() { channel.subscribe(new MessageHandler() {
@Override @Override
...@@ -66,12 +68,13 @@ public class GenericMessagingTemplateTests { ...@@ -66,12 +68,13 @@ public class GenericMessagingTemplateTests {
}); });
String actual = this.template.convertSendAndReceive(channel, "request", String.class); String actual = this.template.convertSendAndReceive(channel, "request", String.class);
assertEquals("response", actual); assertEquals("response", actual);
} }
@Test @Test
public void sendAndReceiveTimeout() throws InterruptedException { public void sendAndReceiveTimeout() throws InterruptedException {
final AtomicReference<Throwable> failure = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
this.template.setReceiveTimeout(1); this.template.setReceiveTimeout(1);
...@@ -85,14 +88,17 @@ public class GenericMessagingTemplateTests { ...@@ -85,14 +88,17 @@ public class GenericMessagingTemplateTests {
Thread.sleep(500); Thread.sleep(500);
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel(); MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
replyChannel.send(new GenericMessage<String>("response")); replyChannel.send(new GenericMessage<String>("response"));
fail("Expected exception"); failure.set(new IllegalStateException("Expected exception"));
} }
catch (InterruptedException e) { catch (InterruptedException e) {
fail("Unexpected exception " + e.getMessage()); failure.set(e);
} }
catch (MessageDeliveryException ex) { catch (MessageDeliveryException ex) {
assertEquals("Reply message received but the receiving thread has already received a reply", String expected = "Reply message received but the receiving thread has exited due to a timeout";
ex.getMessage()); String actual = ex.getMessage();
if (!expected.equals(actual)) {
failure.set(new IllegalStateException("Unexpected error: '" + actual + "'"));
}
} }
finally { finally {
latch.countDown(); latch.countDown();
...@@ -101,8 +107,11 @@ public class GenericMessagingTemplateTests { ...@@ -101,8 +107,11 @@ public class GenericMessagingTemplateTests {
}); });
assertNull(this.template.convertSendAndReceive(channel, "request", String.class)); assertNull(this.template.convertSendAndReceive(channel, "request", String.class));
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
if (failure.get() != null) {
throw new AssertionError(failure.get());
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册