@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"); }
((Consumer) i.getArgument(6)).handleConsumeOk("consumerTag"); return "consumerTag"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
return "consumerTag"; }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); assertTrue(latch3.await(10, TimeUnit.SECONDS)); verify(channel, times(1)).basicConsume(eq("test1"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); verify(channel, times(2)).basicConsume(eq("test2"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
given(channel.queueDeclarePassive(Mockito.anyString())) .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); given(channel.basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class))).willReturn("consumerTag");
return "consumerTag"; }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));