Source:
public void testShutdownWhileWaitingForReply() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); given(mockConnection.isOpen()).willReturn(true); given(mockConnection.createChannel()).willReturn(mockChannel); given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference<ShutdownListener> listener = new AtomicReference<>(); final CountDownLatch shutdownLatch = new CountDownLatch(1); willAnswer(invocation -> { listener.set(invocation.getArgument(0)); shutdownLatch.countDown(); return null; }).given(mockChannel).addShutdownListener(any()); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setReplyTimeout(60_000); Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); Executors.newSingleThreadExecutor().execute(() -> { try { shutdownLatch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null)); }); try { template.doSendAndReceiveWithTemporary("foo", "bar", input, null); fail("Expected exception"); } catch (AmqpException e) { assertThat(e.getCause(), instanceOf(ShutdownSignalException.class)); } }

Target:
public void testShutdownWhileWaitingForReply() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); given(mockConnection.isOpen()).willReturn(true); given(mockConnection.createChannel()).willReturn(mockChannel); given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference<ShutdownListener> listener = new AtomicReference<>(); final CountDownLatch shutdownLatch = new CountDownLatch(1); willAnswer(invocation -> { listener.set(invocation.getArgument(0)); shutdownLatch.countDown(); return null; }).given(mockChannel).addShutdownListener(any()); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setReplyTimeout(60_000); Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); ExecutorService exec = Executors.newSingleThreadExecutor(); exec.execute(() -> { try { shutdownLatch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null)); }); try { template.doSendAndReceiveWithTemporary("foo", "bar", input, null); fail("Expected exception"); } catch (AmqpException e) { assertThat(e.getCause(), instanceOf(ShutdownSignalException.class)); } exec.shutdownNow(); }