|
25 | 25 | import static org.mockito.ArgumentMatchers.anyLong;
|
26 | 26 | import static org.mockito.ArgumentMatchers.anyMap;
|
27 | 27 | import static org.mockito.ArgumentMatchers.anyString;
|
| 28 | +import static org.mockito.ArgumentMatchers.eq; |
28 | 29 | import static org.mockito.BDDMockito.given;
|
29 | 30 | import static org.mockito.BDDMockito.willAnswer;
|
30 | 31 | import static org.mockito.Mockito.mock;
|
| 32 | +import static org.mockito.Mockito.times; |
31 | 33 | import static org.mockito.Mockito.verify;
|
32 | 34 |
|
33 | 35 | import java.util.concurrent.CountDownLatch;
|
@@ -198,6 +200,64 @@ else if (i.getArgument(0).equals(17L)) {
|
198 | 200 | verify(channel).basicAck(20L, true);
|
199 | 201 | }
|
200 | 202 |
|
| 203 | + @Test |
| 204 | + public void testRemoveQueuesWhileNotConnected() throws Exception { |
| 205 | + ConnectionFactory connectionFactory = mock(ConnectionFactory.class); |
| 206 | + Connection connection = mock(Connection.class); |
| 207 | + ChannelProxy channel = mock(ChannelProxy.class); |
| 208 | + Channel rabbitChannel = mock(AutorecoveringChannel.class); |
| 209 | + given(channel.getTargetChannel()).willReturn(rabbitChannel); |
| 210 | + |
| 211 | + given(connectionFactory.createConnection()).willReturn(connection); |
| 212 | + given(connection.createChannel(anyBoolean())).willReturn(channel); |
| 213 | + final AtomicBoolean isOpen = new AtomicBoolean(true); |
| 214 | + willAnswer(i -> isOpen.get()).given(channel).isOpen(); |
| 215 | + given(channel.queueDeclarePassive(Mockito.anyString())) |
| 216 | + .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); |
| 217 | + given(channel.basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), |
| 218 | + anyMap(), any(Consumer.class))).willReturn("consumerTag"); |
| 219 | + |
| 220 | + final CountDownLatch latch1 = new CountDownLatch(2); |
| 221 | + final CountDownLatch latch3 = new CountDownLatch(3); |
| 222 | + final AtomicInteger qos = new AtomicInteger(); |
| 223 | + willAnswer(i -> { |
| 224 | + qos.set(i.getArgument(0)); |
| 225 | + latch1.countDown(); |
| 226 | + latch3.countDown(); |
| 227 | + return null; |
| 228 | + }).given(channel).basicQos(anyInt()); |
| 229 | + final CountDownLatch latch2 = new CountDownLatch(2); |
| 230 | + willAnswer(i -> { |
| 231 | + latch2.countDown(); |
| 232 | + return null; |
| 233 | + }).given(channel).basicCancel("consumerTag"); |
| 234 | + |
| 235 | + DirectMessageListenerContainer container = new DirectMessageListenerContainer(connectionFactory); |
| 236 | + container.setQueueNames("test1", "test2"); |
| 237 | + container.setPrefetchCount(2); |
| 238 | + container.setMonitorInterval(100); |
| 239 | + container.setFailedDeclarationRetryInterval(100); |
| 240 | + container.setRecoveryInterval(100); |
| 241 | + container.setShutdownTimeout(1); |
| 242 | + container.afterPropertiesSet(); |
| 243 | + container.start(); |
| 244 | + |
| 245 | + assertTrue(latch1.await(10, TimeUnit.SECONDS)); |
| 246 | + assertThat(qos.get(), equalTo(2)); |
| 247 | + isOpen.set(false); |
| 248 | + assertTrue(latch2.await(10, TimeUnit.SECONDS)); |
| 249 | + container.removeQueueNames("test1"); |
| 250 | + isOpen.set(true); |
| 251 | + assertTrue(latch3.await(10, TimeUnit.SECONDS)); |
| 252 | + |
| 253 | + verify(channel, times(1)).basicConsume(eq("test1"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), |
| 254 | + anyMap(), any(Consumer.class)); |
| 255 | + verify(channel, times(2)).basicConsume(eq("test2"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), |
| 256 | + anyMap(), any(Consumer.class)); |
| 257 | + |
| 258 | + container.stop(); |
| 259 | + } |
| 260 | + |
201 | 261 | private Envelope envelope(long tag) {
|
202 | 262 | return new Envelope(tag, false, "", "");
|
203 | 263 | }
|
|
0 commit comments