|
| 1 | +package com.rabbitmq.client.test.functional; |
| 2 | + |
| 3 | +import com.rabbitmq.client.QueueingConsumer; |
| 4 | +import com.rabbitmq.client.QueueingConsumer.Delivery; |
| 5 | +import com.rabbitmq.client.test.BrokerTestCase; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Queue; |
| 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 | + public void testPrefetch() throws IOException { |
| 23 | + QueueingConsumer c = new QueueingConsumer(channel); |
| 24 | + publish(q, 5); |
| 25 | + consume(c, false); |
| 26 | + Queue<Delivery> dels = drain(c, 2); |
| 27 | + for (Delivery del : dels) { |
| 28 | + ack(del, false); |
| 29 | + } |
| 30 | + Delivery last = drain(c, 2).getLast(); |
| 31 | + ack(last, true); |
| 32 | + drain(c, 1); |
| 33 | + publish(q, 5); |
| 34 | + drain(c, 1); |
| 35 | + } |
| 36 | + |
| 37 | + public void testAutoAckIgnoresPrefetch() throws IOException { |
| 38 | + QueueingConsumer c = new QueueingConsumer(channel); |
| 39 | + publish(q, 10); |
| 40 | + consume(c, true); |
| 41 | + drain(c, 10); |
| 42 | + } |
| 43 | + |
| 44 | + private void publish(String q, int n) throws IOException { |
| 45 | + for (int i = 0; i < n; i++) { |
| 46 | + channel.basicPublish("", q, null, "".getBytes()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private void consume(QueueingConsumer c, boolean autoAck) throws IOException { |
| 51 | + channel.basicConsume(q, autoAck, "", false, false, args(2), c); |
| 52 | + } |
| 53 | + |
| 54 | + private void ack(Delivery del, boolean multi) throws IOException { |
| 55 | + channel.basicAck(del.getEnvelope().getDeliveryTag(), multi); |
| 56 | + } |
| 57 | + |
| 58 | + private Map<String, Object> args(int prefetch) { |
| 59 | + Map<String, Object> a = new HashMap<String, Object>(); |
| 60 | + if (prefetch != 0) { |
| 61 | + a.put("x-prefetch", prefetch); |
| 62 | + } |
| 63 | + return a; |
| 64 | + } |
| 65 | +} |
0 commit comments