|
36 | 36 |
|
37 | 37 | import com.rabbitmq.client.AMQP; |
38 | 38 | import com.rabbitmq.client.QueueingConsumer; |
| 39 | +import com.rabbitmq.client.Channel; |
39 | 40 |
|
40 | 41 | import com.rabbitmq.client.test.BrokerTestCase; |
41 | 42 |
|
42 | 43 | public class Recover extends BrokerTestCase { |
43 | 44 |
|
44 | 45 | String queue; |
45 | 46 | byte[] body = "message".getBytes(); |
46 | | - |
| 47 | + |
47 | 48 | public void createResources() throws IOException { |
48 | 49 | AMQP.Queue.DeclareOk ok = channel.queueDeclare(); |
49 | 50 | queue = ok.getQueue(); |
50 | 51 | } |
51 | 52 |
|
52 | | - public void testRedeliverOnRecover() throws IOException, InterruptedException { |
| 53 | + static interface RecoverCallback { |
| 54 | + void recover(Channel channel) throws IOException; |
| 55 | + } |
| 56 | + |
| 57 | + // The AMQP specification under-specifies the behaviour when |
| 58 | + // requeue=false. So we can't really test any scenarios for |
| 59 | + // requeue=false. |
| 60 | + |
| 61 | + void verifyRedeliverOnRecover(RecoverCallback call) |
| 62 | + throws IOException, InterruptedException { |
53 | 63 | QueueingConsumer consumer = new QueueingConsumer(channel); |
54 | 64 | channel.basicConsume(queue, false, consumer); // require acks. |
55 | 65 | channel.basicPublish("", queue, new AMQP.BasicProperties(), body); |
56 | 66 | QueueingConsumer.Delivery delivery = consumer.nextDelivery(); |
57 | 67 | assertTrue("consumed message body not as sent", |
58 | 68 | Arrays.equals(body, delivery.getBody())); |
59 | 69 | // Don't ack it, and get it redelivered to the same consumer |
60 | | - channel.basicRecoverAsync(true); |
| 70 | + call.recover(channel); |
61 | 71 | QueueingConsumer.Delivery secondDelivery = consumer.nextDelivery(5000); |
62 | 72 | assertNotNull("timed out waiting for redelivered message", secondDelivery); |
63 | 73 | assertTrue("consumed (redelivered) message body not as sent", |
64 | | - Arrays.equals(body, delivery.getBody())); |
| 74 | + Arrays.equals(body, delivery.getBody())); |
65 | 75 | } |
66 | 76 |
|
67 | | - public void testNoRedeliveryWithAutoAck() throws IOException, InterruptedException { |
| 77 | + void verifyNoRedeliveryWithAutoAck(RecoverCallback call) |
| 78 | + throws IOException, InterruptedException { |
68 | 79 | QueueingConsumer consumer = new QueueingConsumer(channel); |
69 | 80 | channel.basicConsume(queue, true, consumer); // auto ack. |
70 | 81 | channel.basicPublish("", queue, new AMQP.BasicProperties(), body); |
71 | 82 | QueueingConsumer.Delivery delivery = consumer.nextDelivery(); |
72 | 83 | assertTrue("consumed message body not as sent", |
73 | 84 | Arrays.equals(body, delivery.getBody())); |
74 | | - channel.basicRecoverAsync(true); |
| 85 | + call.recover(channel); |
75 | 86 | // there's a race here between our recover finishing and the basic.get; |
76 | 87 | Thread.sleep(500); |
77 | 88 | assertNull("should be no message available", channel.basicGet(queue, true)); |
78 | 89 | } |
79 | 90 |
|
80 | | - // The AMQP specification under-specifies the behaviour when |
81 | | - // requeue=false. So we can't really test any scenarios for |
82 | | - // requeue=false. |
| 91 | + RecoverCallback recoverAsync = new RecoverCallback() { |
| 92 | + public void recover(Channel channel) throws IOException { |
| 93 | + channel.basicRecoverAsync(true); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + RecoverCallback recoverSync = new RecoverCallback() { |
| 98 | + public void recover(Channel channel) throws IOException { |
| 99 | + channel.basicRecover(true); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + public void testRedeliverOnRecoverAsync() throws IOException, InterruptedException { |
| 104 | + verifyRedeliverOnRecover(recoverAsync); |
| 105 | + } |
| 106 | + |
| 107 | + public void testRedeliveryOnRecover() throws IOException, InterruptedException { |
| 108 | + verifyRedeliverOnRecover(recoverSync); |
| 109 | + } |
| 110 | + |
| 111 | + public void testNoRedeliveryWithAutoAckAsync() |
| 112 | + throws IOException, InterruptedException { |
| 113 | + verifyNoRedeliveryWithAutoAck(recoverAsync); |
| 114 | + } |
| 115 | + |
| 116 | + public void testNoRedeliveryWithAutoAck() |
| 117 | + throws IOException, InterruptedException { |
| 118 | + verifyNoRedeliveryWithAutoAck(recoverSync); |
| 119 | + } |
83 | 120 | } |
0 commit comments