@Test
public void testAlwaysCancelAutoRecoverConsumer() throws IOException {
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
Connection connection = mock(Connection.class);
ChannelProxy channel = mock(ChannelProxy.class);
Channel rabbitChannel = mock(AutorecoveringChannel.class);
when(channel.getTargetChannel()).thenReturn(rabbitChannel);
when(connectionFactory.createConnection()).thenReturn(connection);
when(connection.createChannel(anyBoolean())).thenReturn(channel);
final AtomicBoolean isOpen = new AtomicBoolean(true);
doReturn(isOpen.get()).when(channel).isOpen();
when(channel.queueDeclarePassive(anyString()))
.then(invocation -> mock(AMQP.Queue.DeclareOk.class));
doAnswer(i -> {
((Consumer) i.getArgument(6)).handleConsumeOk("consumerTag");
return "consumerTag";
}).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(),
anyMap(), any(Consumer.class));
BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory,
new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<>(),
AcknowledgeMode.AUTO, true, 2, "test");
blockingQueueConsumer.setDeclarationRetries(1);
blockingQueueConsumer.setRetryDeclarationInterval(10);
blockingQueueConsumer.setFailedDeclarationRetryInterval(10);
blockingQueueConsumer.start();
verify(channel).basicQos(2);
isOpen.set(false);
blockingQueueConsumer.stop();
verify(channel).basicCancel("consumerTag");
}