|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License at |
| 4 | +// http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 8 | +// License for the specific language governing rights and limitations |
| 9 | +// under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developers of the Original Code are LShift Ltd, |
| 14 | +// Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd. |
| 15 | +// |
| 16 | +// Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd, |
| 17 | +// Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd |
| 18 | +// are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial |
| 19 | +// Technologies LLC, and Rabbit Technologies Ltd. |
| 20 | +// |
| 21 | +// Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift |
| 22 | +// Ltd. Portions created by Cohesive Financial Technologies LLC are |
| 23 | +// Copyright (C) 2007-2009 Cohesive Financial Technologies |
| 24 | +// LLC. Portions created by Rabbit Technologies Ltd are Copyright |
| 25 | +// (C) 2007-2009 Rabbit Technologies Ltd. |
| 26 | +// |
| 27 | +// All Rights Reserved. |
| 28 | +// |
| 29 | +// Contributor(s): ______________________________________. |
| 30 | +// |
| 31 | + |
| 32 | +package com.rabbitmq.client.test.functional; |
| 33 | + |
| 34 | +import com.rabbitmq.client.AMQP; |
| 35 | +import com.rabbitmq.client.Channel; |
| 36 | +import com.rabbitmq.client.Connection; |
| 37 | +import com.rabbitmq.client.GetResponse; |
| 38 | +import com.rabbitmq.client.QueueingConsumer; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | + |
| 42 | +/** |
| 43 | + * This tests whether bindings are created and nuked properly. |
| 44 | + * |
| 45 | + * The tests attempt to declare durable queues on a secondary node, if |
| 46 | + * present, and that node is restarted as part of the tests while the |
| 47 | + * primary node is still running. That way we exercise any node-down |
| 48 | + * handler code in the server. |
| 49 | + * |
| 50 | + */ |
| 51 | +public class BindingLifecycle extends BindingLifecycleBase { |
| 52 | + |
| 53 | + /** |
| 54 | + * This tests that when you purge a queue, all of its messages go. |
| 55 | + */ |
| 56 | + public void testQueuePurge() throws IOException { |
| 57 | + |
| 58 | + Binding binding = setupExchangeBindings(false); |
| 59 | + channel.basicPublish(binding.x, binding.k, null, payload); |
| 60 | + |
| 61 | + // Purge the queue, and test that we don't recieve a message |
| 62 | + channel.queuePurge(binding.q); |
| 63 | + |
| 64 | + GetResponse response = channel.basicGet(binding.q, true); |
| 65 | + assertNull("The response SHOULD BE null", response); |
| 66 | + |
| 67 | + deleteExchangeAndQueue(binding); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * This tests whether when you delete an exchange, that any |
| 72 | + * bindings attached to it are deleted as well. |
| 73 | + */ |
| 74 | + public void testExchangeDelete() throws IOException { |
| 75 | + |
| 76 | + boolean durable = true; |
| 77 | + Binding binding = setupExchangeAndRouteMessage(durable); |
| 78 | + |
| 79 | + // Nuke the exchange and repeat this test, this time you |
| 80 | + // expect nothing to get routed |
| 81 | + |
| 82 | + channel.exchangeDelete(binding.x); |
| 83 | + channel.exchangeDeclare(binding.x, "direct"); |
| 84 | + |
| 85 | + sendUnroutable(binding); |
| 86 | + |
| 87 | + channel.queueDelete(binding.q); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * This tests whether the server checks that an exchange is |
| 92 | + * actually being used when you try to delete it with the ifunused |
| 93 | + * flag. |
| 94 | + * |
| 95 | + * To test this, you try to delete an exchange with a queue still |
| 96 | + * bound to it and expect the delete operation to fail. |
| 97 | + */ |
| 98 | + public void testExchangeIfUnused() throws IOException { |
| 99 | + |
| 100 | + boolean durable = true; |
| 101 | + Binding binding = setupExchangeBindings(durable); |
| 102 | + |
| 103 | + try { |
| 104 | + channel.exchangeDelete(binding.x, true); |
| 105 | + } |
| 106 | + catch (Exception e) { |
| 107 | + // do nothing, this is the correct behaviour |
| 108 | + openChannel(); |
| 109 | + deleteExchangeAndQueue(binding); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + fail("Exchange delete should have failed"); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * This tests whether the server checks that an auto_delete |
| 118 | + * exchange actually deletes the bindings attached to it when it |
| 119 | + * is deleted. |
| 120 | + * |
| 121 | + * To test this, you declare and auto_delete exchange and bind an |
| 122 | + * auto_delete queue to it. |
| 123 | + * |
| 124 | + * Start a consumer on this queue, send a message, let it get |
| 125 | + * consumed and then cancel the consumer |
| 126 | + * |
| 127 | + * The unsubscribe should cause the queue to auto_delete, which in |
| 128 | + * turn should cause the exchange to auto_delete. |
| 129 | + * |
| 130 | + * Then re-declare the queue again and try to rebind it to the same exhange. |
| 131 | + * |
| 132 | + * Because the exchange has been auto-deleted, the bind operation |
| 133 | + * should fail. |
| 134 | + */ |
| 135 | + public void testExchangeAutoDelete() throws IOException { |
| 136 | + doAutoDelete(false, 1); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Runs something similar to testExchangeAutoDelete, but adds |
| 141 | + * different queues with the same binding to the same exchange. |
| 142 | + * |
| 143 | + * The difference should be that the original exchange should not |
| 144 | + * get auto-deleted |
| 145 | + */ |
| 146 | + public void testExchangeAutoDeleteManyBindings() throws IOException { |
| 147 | + doAutoDelete(false, 10); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Test the behaviour of queue.unbind |
| 152 | + */ |
| 153 | + public void testUnbind() throws Exception { |
| 154 | + |
| 155 | + Binding b = new Binding(channel.queueDeclare().getQueue(), |
| 156 | + "amq.direct", |
| 157 | + "quay"); |
| 158 | + |
| 159 | + // failure cases |
| 160 | + |
| 161 | + Binding[] tests = new Binding[] { |
| 162 | + new Binding("unknown_queue", b.x, b.k), |
| 163 | + new Binding(b.q, "unknown_exchange", b.k), |
| 164 | + new Binding("unknown_unknown", "exchange_queue", b.k), |
| 165 | + new Binding(b.q, b.x, "unknown_rk"), |
| 166 | + new Binding("unknown_queue", "unknown_exchange", "unknown_rk") |
| 167 | + }; |
| 168 | + |
| 169 | + for (int i = 0; i < tests.length; i++) { |
| 170 | + |
| 171 | + Binding test = tests[i]; |
| 172 | + try { |
| 173 | + channel.queueUnbind(test.q, test.x, test.k); |
| 174 | + fail("expected not_found in test " + i); |
| 175 | + } catch (IOException ee) { |
| 176 | + checkShutdownSignal(AMQP.NOT_FOUND, ee); |
| 177 | + openChannel(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // success case |
| 182 | + |
| 183 | + channel.queueBind(b.q, b.x, b.k); |
| 184 | + sendRoutable(b); |
| 185 | + channel.queueUnbind(b.q, b.x, b.k); |
| 186 | + sendUnroutable(b); |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments