|
| 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.ReturnListener; |
| 36 | +import com.rabbitmq.client.GetResponse; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.HashMap; |
| 42 | + |
| 43 | +public class UnroutableMessageExchange extends BrokerTestCase |
| 44 | +{ |
| 45 | + |
| 46 | + static private String[] resources = new String[]{"x","u","v"}; |
| 47 | + static private String[] keys = new String[]{"x","u","v","z"}; |
| 48 | + |
| 49 | + private AtomicBoolean gotReturn = new AtomicBoolean(); |
| 50 | + |
| 51 | + private static boolean[] expected(String key) { |
| 52 | + boolean[] expected = new boolean[resources.length]; |
| 53 | + for (int i = 0; i < resources.length; i++) { |
| 54 | + expected[i] = resources[i].equals(key); |
| 55 | + } |
| 56 | + return expected; |
| 57 | + } |
| 58 | + |
| 59 | + protected void setUp() throws IOException { |
| 60 | + super.setUp(); |
| 61 | + channel.setReturnListener(new ReturnListener() { |
| 62 | + public void handleBasicReturn(int replyCode, |
| 63 | + String replyText, |
| 64 | + String exchange, |
| 65 | + String routingKey, |
| 66 | + AMQP.BasicProperties properties, |
| 67 | + byte[] body) |
| 68 | + throws IOException { |
| 69 | + gotReturn.set(true); |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + protected void setupRouting(String x, String ume) throws IOException { |
| 75 | + Map<String, Object> args = new HashMap<String, Object>(); |
| 76 | + if (ume != null) args.put("ume", ume); |
| 77 | + channel.exchangeDeclare(x, "direct", false, false, false, args); |
| 78 | + channel.queueBind(x, x, x); |
| 79 | + } |
| 80 | + |
| 81 | + protected void publish(String key, boolean mandatory, boolean immediate) |
| 82 | + throws IOException { |
| 83 | + channel.basicPublish("x", key, mandatory, immediate, null, |
| 84 | + "ume-test".getBytes()); |
| 85 | + } |
| 86 | + |
| 87 | + protected void publish(String key) throws IOException { |
| 88 | + publish(key, false, false); |
| 89 | + } |
| 90 | + |
| 91 | + protected void checkGet(boolean[] expected) throws IOException { |
| 92 | + for (int i = 0; i < resources.length; i++) { |
| 93 | + String q = resources[i]; |
| 94 | + GetResponse r = channel.basicGet(q, true); |
| 95 | + assertEquals("check " + q , expected[i], r != null); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + protected void check(String key, boolean mandatory, boolean immediate, |
| 100 | + boolean[] expected, boolean ret) |
| 101 | + throws IOException { |
| 102 | + |
| 103 | + gotReturn.set(false); |
| 104 | + publish(key, mandatory, immediate); |
| 105 | + checkGet(expected); |
| 106 | + assertEquals(ret, gotReturn.get()); |
| 107 | + } |
| 108 | + |
| 109 | + protected void check(String key, boolean[] expected, boolean ret) |
| 110 | + throws IOException { |
| 111 | + check(key, false, false, expected, ret); |
| 112 | + } |
| 113 | + |
| 114 | + protected void check(String key, boolean mandatory, boolean immediate, |
| 115 | + boolean ret) throws IOException { |
| 116 | + check(key, mandatory, immediate, expected(key), ret); |
| 117 | + } |
| 118 | + |
| 119 | + protected void check(String key, boolean ret) throws IOException { |
| 120 | + check(key, false, false, ret); |
| 121 | + } |
| 122 | + |
| 123 | + public void testUme() throws IOException { |
| 124 | + |
| 125 | + for (String q : resources) { |
| 126 | + channel.queueDeclare(q, false, false, true, false, null); |
| 127 | + } |
| 128 | + |
| 129 | + //check various cases of missing UMEs - we expect to see some |
| 130 | + //warnings in the server logs |
| 131 | + |
| 132 | + boolean unrouted[] = new boolean[] {false, false, false}; |
| 133 | + |
| 134 | + setupRouting("x", "u"); |
| 135 | + check("x", false); //no warning |
| 136 | + check("u", unrouted, false); //warning |
| 137 | + |
| 138 | + setupRouting("u", "v"); |
| 139 | + check("u", false); //no warning |
| 140 | + check("v", unrouted, false); //warning |
| 141 | + |
| 142 | + setupRouting("v", null); |
| 143 | + check("v", false); //no warning |
| 144 | + check("z", unrouted, false); //no warning |
| 145 | + |
| 146 | + //routing with UMEs in place |
| 147 | + for (String k : keys) { |
| 148 | + //ordinary |
| 149 | + check(k, false); |
| 150 | + //mandatory |
| 151 | + check(k, true, false, !k.equals("x")); |
| 152 | + //immediate |
| 153 | + check(k, false, true, k.equals("x") ? unrouted : expected(k), true); |
| 154 | + /* |
| 155 | + if (k.equals("x")) |
| 156 | + check(k, false, true, unrouted, true); |
| 157 | + else |
| 158 | + check(k, false, true, true); |
| 159 | + */ |
| 160 | + } |
| 161 | + |
| 162 | + //tx |
| 163 | + channel.txSelect(); |
| 164 | + for (String k : keys) { |
| 165 | + publish(k); |
| 166 | + checkGet(unrouted); |
| 167 | + channel.txRollback(); |
| 168 | + checkGet(unrouted); |
| 169 | + publish(k); |
| 170 | + checkGet(unrouted); |
| 171 | + channel.txCommit(); |
| 172 | + checkGet(expected(k)); |
| 173 | + } |
| 174 | + |
| 175 | + //cleanup |
| 176 | + for (String r : resources) { |
| 177 | + channel.exchangeDelete(r); |
| 178 | + channel.queueDelete(r); |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments