|
| 1 | +package com.rabbitmq.client.test.functional; |
| 2 | + |
| 3 | +import com.rabbitmq.client.GetResponse; |
| 4 | +import com.rabbitmq.client.QueueingConsumer; |
| 5 | +import com.rabbitmq.client.QueueingConsumer.Delivery; |
| 6 | +import com.rabbitmq.client.test.BrokerTestCase; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import static com.rabbitmq.client.test.functional.QosTests.drain; |
| 13 | + |
| 14 | +public class PerConsumerPrefetch extends BrokerTestCase { |
| 15 | + private String q; |
| 16 | + |
| 17 | + @Override |
| 18 | + protected void createResources() throws IOException { |
| 19 | + q = channel.queueDeclare().getQueue(); |
| 20 | + } |
| 21 | + |
| 22 | + private interface Closure { |
| 23 | + public void makeMore(List<Delivery> deliveries) throws IOException; |
| 24 | + } |
| 25 | + |
| 26 | + public void testSingleAck() throws IOException { |
| 27 | + testPrefetch(new Closure() { |
| 28 | + public void makeMore(List<Delivery> deliveries) throws IOException { |
| 29 | + for (Delivery del : deliveries) { |
| 30 | + ack(del, false); |
| 31 | + } |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + public void testMultiAck() throws IOException { |
| 37 | + testPrefetch(new Closure() { |
| 38 | + public void makeMore(List<Delivery> deliveries) throws IOException { |
| 39 | + ack(deliveries.get(deliveries.size() - 1), true); |
| 40 | + } |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + public void testSingleNack() throws IOException { |
| 45 | + for (final boolean requeue: Arrays.asList(false, true)) { |
| 46 | + testPrefetch(new Closure() { |
| 47 | + public void makeMore(List<Delivery> deliveries) throws IOException { |
| 48 | + for (Delivery del : deliveries) { |
| 49 | + nack(del, false, requeue); |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void testMultiNack() throws IOException { |
| 57 | + for (final boolean requeue: Arrays.asList(false, true)) { |
| 58 | + testPrefetch(new Closure() { |
| 59 | + public void makeMore(List<Delivery> deliveries) throws IOException { |
| 60 | + nack(deliveries.get(deliveries.size() - 1), true, requeue); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void testRecover() throws IOException { |
| 67 | + testPrefetch(new Closure() { |
| 68 | + public void makeMore(List<Delivery> deliveries) throws IOException { |
| 69 | + channel.basicRecover(); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + private void testPrefetch(Closure closure) throws IOException { |
| 75 | + QueueingConsumer c = new QueueingConsumer(channel); |
| 76 | + publish(q, 15); |
| 77 | + consume(c, 5, false); |
| 78 | + List<Delivery> deliveries = drain(c, 5); |
| 79 | + |
| 80 | + ack(channel.basicGet(q, false), false); |
| 81 | + drain(c, 0); |
| 82 | + |
| 83 | + closure.makeMore(deliveries); |
| 84 | + drain(c, 5); |
| 85 | + } |
| 86 | + |
| 87 | + public void testPrefetchOnEmpty() throws IOException { |
| 88 | + QueueingConsumer c = new QueueingConsumer(channel); |
| 89 | + publish(q, 5); |
| 90 | + consume(c, 10, false); |
| 91 | + drain(c, 5); |
| 92 | + publish(q, 10); |
| 93 | + drain(c, 5); |
| 94 | + } |
| 95 | + |
| 96 | + public void testAutoAckIgnoresPrefetch() throws IOException { |
| 97 | + QueueingConsumer c = new QueueingConsumer(channel); |
| 98 | + publish(q, 10); |
| 99 | + consume(c, 1, true); |
| 100 | + drain(c, 10); |
| 101 | + } |
| 102 | + |
| 103 | + public void testPrefetchZeroMeansInfinity() throws IOException { |
| 104 | + QueueingConsumer c = new QueueingConsumer(channel); |
| 105 | + publish(q, 10); |
| 106 | + consume(c, 0, false); |
| 107 | + drain(c, 10); |
| 108 | + } |
| 109 | + |
| 110 | + private void publish(String q, int n) throws IOException { |
| 111 | + for (int i = 0; i < n; i++) { |
| 112 | + channel.basicPublish("", q, null, "".getBytes()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private void consume(QueueingConsumer c, int prefetch, boolean autoAck) throws IOException { |
| 117 | + channel.basicQos(prefetch); |
| 118 | + channel.basicConsume(q, autoAck, c); |
| 119 | + } |
| 120 | + |
| 121 | + private void ack(Delivery del, boolean multi) throws IOException { |
| 122 | + channel.basicAck(del.getEnvelope().getDeliveryTag(), multi); |
| 123 | + } |
| 124 | + |
| 125 | + private void ack(GetResponse get, boolean multi) throws IOException { |
| 126 | + channel.basicAck(get.getEnvelope().getDeliveryTag(), multi); |
| 127 | + } |
| 128 | + |
| 129 | + private void nack(Delivery del, boolean multi, boolean requeue) throws IOException { |
| 130 | + channel.basicNack(del.getEnvelope().getDeliveryTag(), multi, requeue); |
| 131 | + } |
| 132 | +} |
0 commit comments