|
31 | 31 | import java.util.List;
|
32 | 32 | import java.util.Random;
|
33 | 33 | import java.util.concurrent.*;
|
| 34 | +import java.util.concurrent.atomic.AtomicInteger; |
34 | 35 |
|
35 | 36 | import static org.awaitility.Awaitility.*;
|
36 | 37 | import static org.hamcrest.Matchers.*;
|
@@ -426,6 +427,57 @@ private void errorInChannel(ConnectionFactory connectionFactory) throws IOExcept
|
426 | 427 |
|
427 | 428 | }
|
428 | 429 |
|
| 430 | + @Test public void checkAcksWithAutomaticRecovery() throws Exception { |
| 431 | + ConnectionFactory connectionFactory = createConnectionFactory(); |
| 432 | + connectionFactory.setNetworkRecoveryInterval(2000); |
| 433 | + connectionFactory.setAutomaticRecoveryEnabled(true); |
| 434 | + StandardMetricsCollector metrics = new StandardMetricsCollector(); |
| 435 | + connectionFactory.setMetricsCollector(metrics); |
| 436 | + |
| 437 | + Connection connection = null; |
| 438 | + try { |
| 439 | + connection = connectionFactory.newConnection(); |
| 440 | + |
| 441 | + final Channel channel1 = connection.createChannel(); |
| 442 | + final AtomicInteger ackedMessages = new AtomicInteger(0); |
| 443 | + |
| 444 | + channel1.basicConsume(QUEUE, false, new DefaultConsumer(channel1) { |
| 445 | + @Override |
| 446 | + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { |
| 447 | + channel1.basicAck(envelope.getDeliveryTag(), false); |
| 448 | + ackedMessages.incrementAndGet(); |
| 449 | + } |
| 450 | + }); |
| 451 | + |
| 452 | + Channel channel2 = connection.createChannel(); |
| 453 | + channel2.confirmSelect(); |
| 454 | + int nbMessages = 10; |
| 455 | + for (int i = 0; i < nbMessages; i++) { |
| 456 | + sendMessage(channel2); |
| 457 | + } |
| 458 | + channel2.waitForConfirms(1000); |
| 459 | + |
| 460 | + closeAndWaitForRecovery((AutorecoveringConnection) connection); |
| 461 | + |
| 462 | + for (int i = 0; i < nbMessages; i++) { |
| 463 | + sendMessage(channel2); |
| 464 | + } |
| 465 | + |
| 466 | + waitAtMost(timeout()).until(new Callable<Integer>() { |
| 467 | + @Override |
| 468 | + public Integer call() { |
| 469 | + return ackedMessages.get(); |
| 470 | + } |
| 471 | + }, equalTo(nbMessages * 2)); |
| 472 | + |
| 473 | + assertThat(metrics.getConsumedMessages().getCount(), is((long) (nbMessages * 2))); |
| 474 | + assertThat(metrics.getAcknowledgedMessages().getCount(), is((long) (nbMessages * 2))); |
| 475 | + |
| 476 | + } finally { |
| 477 | + safeClose(connection); |
| 478 | + } |
| 479 | + } |
| 480 | + |
429 | 481 | private ConnectionFactory createConnectionFactory() {
|
430 | 482 | ConnectionFactory connectionFactory = TestUtils.connectionFactory();
|
431 | 483 | return connectionFactory;
|
|
0 commit comments