|
18 | 18 |
|
19 | 19 | import static org.hamcrest.Matchers.containsString;
|
20 | 20 | import static org.hamcrest.Matchers.equalTo;
|
| 21 | +import static org.hamcrest.Matchers.instanceOf; |
21 | 22 | import static org.junit.Assert.assertEquals;
|
22 | 23 | import static org.junit.Assert.assertSame;
|
23 | 24 | import static org.junit.Assert.assertThat;
|
24 | 25 | import static org.junit.Assert.assertTrue;
|
| 26 | +import static org.junit.Assert.fail; |
25 | 27 | import static org.mockito.ArgumentMatchers.any;
|
26 | 28 | import static org.mockito.ArgumentMatchers.anyBoolean;
|
27 | 29 | import static org.mockito.ArgumentMatchers.anyString;
|
|
36 | 38 | import java.util.Collections;
|
37 | 39 | import java.util.HashMap;
|
38 | 40 | import java.util.Map;
|
| 41 | +import java.util.concurrent.CountDownLatch; |
39 | 42 | import java.util.concurrent.ExecutorService;
|
| 43 | +import java.util.concurrent.Executors; |
| 44 | +import java.util.concurrent.TimeUnit; |
40 | 45 | import java.util.concurrent.atomic.AtomicBoolean;
|
41 | 46 | import java.util.concurrent.atomic.AtomicInteger;
|
42 | 47 | import java.util.concurrent.atomic.AtomicReference;
|
|
47 | 52 | import org.mockito.Mockito;
|
48 | 53 |
|
49 | 54 | import org.springframework.amqp.AmqpAuthenticationException;
|
| 55 | +import org.springframework.amqp.AmqpException; |
50 | 56 | import org.springframework.amqp.core.Address;
|
51 | 57 | import org.springframework.amqp.core.Message;
|
52 | 58 | import org.springframework.amqp.core.MessageProperties;
|
|
77 | 83 | import com.rabbitmq.client.ConnectionFactory;
|
78 | 84 | import com.rabbitmq.client.Consumer;
|
79 | 85 | import com.rabbitmq.client.Envelope;
|
| 86 | +import com.rabbitmq.client.ShutdownListener; |
| 87 | +import com.rabbitmq.client.ShutdownSignalException; |
80 | 88 | import com.rabbitmq.client.impl.AMQImpl;
|
81 | 89 | import com.rabbitmq.client.impl.AMQImpl.Queue.DeclareOk;
|
82 | 90 |
|
@@ -365,6 +373,48 @@ public void testNestedTxBinding() throws Exception {
|
365 | 373 | verify(channel1).txCommit();
|
366 | 374 | }
|
367 | 375 |
|
| 376 | + @Test |
| 377 | + public void testShutdownWhileWaitingForReply() throws Exception { |
| 378 | + ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); |
| 379 | + Connection mockConnection = mock(Connection.class); |
| 380 | + Channel mockChannel = mock(Channel.class); |
| 381 | + |
| 382 | + given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); |
| 383 | + given(mockConnection.isOpen()).willReturn(true); |
| 384 | + given(mockConnection.createChannel()).willReturn(mockChannel); |
| 385 | + given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); |
| 386 | + final AtomicReference<ShutdownListener> listener = new AtomicReference<>(); |
| 387 | + final CountDownLatch shutdownLatch = new CountDownLatch(1); |
| 388 | + willAnswer(invocation -> { |
| 389 | + listener.set(invocation.getArgument(0)); |
| 390 | + shutdownLatch.countDown(); |
| 391 | + return null; |
| 392 | + }).given(mockChannel).addShutdownListener(any()); |
| 393 | + SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); |
| 394 | + connectionFactory.setExecutor(mock(ExecutorService.class)); |
| 395 | + RabbitTemplate template = new RabbitTemplate(connectionFactory); |
| 396 | + template.setReplyTimeout(60_000); |
| 397 | + Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); |
| 398 | + ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 399 | + exec.execute(() -> { |
| 400 | + try { |
| 401 | + shutdownLatch.await(10, TimeUnit.SECONDS); |
| 402 | + } |
| 403 | + catch (InterruptedException e) { |
| 404 | + Thread.currentThread().interrupt(); |
| 405 | + } |
| 406 | + listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null)); |
| 407 | + }); |
| 408 | + try { |
| 409 | + template.doSendAndReceiveWithTemporary("foo", "bar", input, null); |
| 410 | + fail("Expected exception"); |
| 411 | + } |
| 412 | + catch (AmqpException e) { |
| 413 | + assertThat(e.getCause(), instanceOf(ShutdownSignalException.class)); |
| 414 | + } |
| 415 | + exec.shutdownNow(); |
| 416 | + } |
| 417 | + |
368 | 418 | @SuppressWarnings("serial")
|
369 | 419 | private class TestTransactionManager extends AbstractPlatformTransactionManager {
|
370 | 420 |
|
|
0 commit comments