Skip to content

Commit 85e7c51

Browse files
author
Simon MacMullen
committed
Add extra tests from the amqp_0_9_1 branch.
1 parent defc417 commit 85e7c51

File tree

5 files changed

+428
-1
lines changed

5 files changed

+428
-1
lines changed

test/src/com/rabbitmq/client/test/functional/FunctionalTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static TestSuite suite() {
5151
suite.addTestSuite(Bug20004Test.class);
5252
suite.addTestSuite(QosTests.class);
5353
suite.addTestSuite(AlternateExchange.class);
54+
suite.addTestSuite(QueueLifecycle.class);
55+
suite.addTestSuite(QueueExclusivity.class);
5456
return suite;
5557
}
5658
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 java.util.Map;
35+
import java.util.HashMap;
36+
import java.io.IOException;
37+
38+
import com.rabbitmq.client.AMQP;
39+
import com.rabbitmq.client.Connection;
40+
import com.rabbitmq.client.Channel;
41+
import com.rabbitmq.client.QueueingConsumer;
42+
43+
import com.rabbitmq.client.test.BrokerTestCase;
44+
45+
// Test queue auto-delete and exclusive semantics.
46+
public class QueueExclusivity extends BrokerTestCase
47+
{
48+
49+
HashMap<String,Object> noArgs = new HashMap();
50+
51+
public Connection altConnection;
52+
public Channel altChannel;
53+
String q = "exclusiveQ";
54+
55+
protected void createResources() throws IOException {
56+
altConnection = connectionFactory.newConnection();
57+
altChannel = altConnection.createChannel();
58+
AMQP.Queue.DeclareOk ok = altChannel.queueDeclare(q,
59+
// not durable, exclusive, not auto-delete
60+
false, true, false, noArgs);
61+
}
62+
63+
protected void releaseResources() throws IOException {
64+
if (altConnection != null && altConnection.isOpen()) {
65+
altConnection.close();
66+
}
67+
}
68+
69+
public void testQueueExclusiveForPassiveDeclare() throws Exception {
70+
try {
71+
AMQP.Queue.DeclareOk ok2 = channel.queueDeclarePassive(q);
72+
}
73+
catch (IOException ioe) {
74+
// TODO test the particular error
75+
return;
76+
}
77+
fail("Passive queue declaration of an exclusive queue from another connection should fail");
78+
}
79+
80+
// This is a different scenario because active declare takes notice of
81+
// the all the arguments
82+
public void testQueueExclusiveForDeclare() throws Exception {
83+
try {
84+
AMQP.Queue.DeclareOk ok2 = channel.queueDeclare(q, false, true, false, noArgs);
85+
}
86+
catch (IOException ioe) {
87+
// TODO test the particular error
88+
return;
89+
}
90+
fail("Active queue declaration of an exclusive queue from another connection should fail");
91+
}
92+
93+
public void testQueueExclusiveForConsume() throws Exception {
94+
QueueingConsumer c = new QueueingConsumer(channel);
95+
try {
96+
channel.basicConsume(q, c);
97+
}
98+
catch (IOException ioe) {
99+
return;
100+
}
101+
fail("Exclusive queue should be locked for basic consume from another connection");
102+
}
103+
104+
public void testQueueExclusiveForPurge() throws Exception {
105+
try {
106+
channel.queuePurge(q);
107+
}
108+
catch (IOException ioe) {
109+
return;
110+
}
111+
fail("Exclusive queue should be locked for queue purge from another connection");
112+
}
113+
114+
public void testQueueExclusiveForDelete() throws Exception {
115+
try {
116+
channel.queueDelete(q);
117+
}
118+
catch (IOException ioe) {
119+
return;
120+
}
121+
fail("Exclusive queue should be locked for queue delete from another connection");
122+
}
123+
124+
public void testQueueExclusiveForBind() throws Exception {
125+
try {
126+
channel.queueBind(q, "", ""); // NB uses default exchange
127+
}
128+
catch (IOException ioe) {
129+
return;
130+
}
131+
fail("Exclusive queue should be locked for queue bind from another connection");
132+
}
133+
134+
// NB The spec XML doesn't mention queue.unbind, basic.cancel, or
135+
// basic.get in the exclusive rule. It seems the most sensible
136+
// interpretation to include queue.unbind and basic.get in the
137+
// prohibition.
138+
// basic.cancel is inherently local to a channel, so it
139+
// *doesn't* make sense to include it.
140+
141+
public void testQueueExclusiveForUnbind() throws Exception {
142+
altChannel.queueBind(q, "", ""); // NB uses default exchange
143+
try {
144+
channel.queueUnbind(q, "", "");
145+
}
146+
catch (IOException ioe) {
147+
return;
148+
}
149+
fail("Exclusive queue should be locked for queue unbind from another connection");
150+
}
151+
152+
public void testQueueExclusiveForGet() throws Exception {
153+
try {
154+
channel.basicGet(q, true);
155+
}
156+
catch (IOException ioe) {
157+
return;
158+
}
159+
fail("Exclusive queue should be locked for basic get from another connection");
160+
}
161+
162+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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 java.util.Map;
35+
import java.util.HashMap;
36+
import java.io.IOException;
37+
38+
import com.rabbitmq.client.AMQP;
39+
import com.rabbitmq.client.QueueingConsumer;
40+
41+
import com.rabbitmq.client.test.BrokerTestCase;
42+
43+
// Test queue auto-delete and exclusive semantics.
44+
public class QueueLifecycle extends BrokerTestCase
45+
{
46+
47+
void verifyQueueExists(String name) throws IOException {
48+
channel.queueDeclarePassive(name);
49+
}
50+
51+
void verifyQueueMissing(String name) throws IOException {
52+
// we can't in general check with a passive declare, since that
53+
// may return an IOException because of exclusivity. But we can
54+
// check that we can happily declare another with the same name:
55+
// the only circumstance in which this won't result in an error is
56+
// if it doesn't exist.
57+
try {
58+
channel.queueDeclare(name, false, false, false, null);
59+
}
60+
catch (IOException ioe) {
61+
fail("Queue.Declare threw an exception, probably meaning that the queue already exists");
62+
}
63+
// clean up
64+
channel.queueDelete(name);
65+
}
66+
67+
68+
/** Verify that a queue both exists and has the properties as given
69+
* @throws IOException if one of these conditions is not true
70+
*/
71+
void verifyQueue(String name,
72+
boolean durable,
73+
boolean exclusive,
74+
boolean autoDelete,
75+
Map<String,Object> args) throws IOException {
76+
verifyQueueExists(name);
77+
// use passive/equivalent rule to check that it has the same properties
78+
channel.queueDeclare(name, durable, exclusive, autoDelete, args);
79+
}
80+
81+
// NB the exception will close the connection
82+
void verifyNotEquivalent(boolean durable,
83+
boolean exclusive,
84+
boolean autoDelete) throws IOException {
85+
String q = "queue";
86+
AMQP.Queue.DeclareOk qok = channel.queueDeclare(q, false, false, false, null);
87+
try {
88+
verifyQueue(q, durable, exclusive, autoDelete, null);
89+
}
90+
catch (IOException ioe) {
91+
return;
92+
}
93+
fail("Queue.declare should have been rejected as not equivalent");
94+
}
95+
96+
// From amqp-0-9-1.xml, for "passive" property, "equivalent" rule:
97+
// "If not set and the queue exists, the server MUST check that the
98+
// existing queue has the same values for durable, exclusive,
99+
// auto-delete, and arguments fields. The server MUST respond with
100+
// Declare-Ok if the requested queue matches these fields, and MUST
101+
// raise a channel exception if not."
102+
public void testQueueEquivalence() throws IOException {
103+
String q = "queue";
104+
AMQP.Queue.DeclareOk qok = channel.queueDeclare(q, false, false, false, null);
105+
// equivalent
106+
verifyQueue(q, false, false, false, null);
107+
108+
// the spec says that the arguments table is matched on
109+
// being semantically equivalent.
110+
HashMap<String,Object> args = new HashMap();
111+
args.put("assumed-to-be-semantically-void", "bar");
112+
verifyQueue(q, false, false, false, args);
113+
114+
}
115+
116+
// not equivalent in various ways
117+
// We don't enforce this for 0-8
118+
// public void testQueueNonEquivalenceDurable() throws IOException {
119+
// verifyNotEquivalent(true, false, false);
120+
// }
121+
122+
public void testQueueNonEquivalenceExclusive() throws IOException {
123+
verifyNotEquivalent(false, true, false);
124+
}
125+
126+
// We don't enforce this for 0-8
127+
// public void testQueueNonEquivalenceAutoDelete() throws IOException {
128+
// verifyNotEquivalent(false, false, true);
129+
// }
130+
131+
// Note that this assumes that auto-deletion is synchronous with basic.cancel,
132+
// which is not actually in the spec. (If it isn't, there's a race here).
133+
public void testQueueAutoDelete() throws IOException {
134+
String name = "tempqueue";
135+
AMQP.Queue.DeclareOk qok = channel.queueDeclare(name, false, false, true, null);
136+
// now it's there
137+
verifyQueue(name, false, false, true, null);
138+
QueueingConsumer consumer = new QueueingConsumer(channel);
139+
channel.basicConsume(name, consumer);
140+
channel.basicCancel(consumer.getConsumerTag());
141+
// now it's not .. we hope
142+
try {
143+
verifyQueueExists(name);
144+
}
145+
catch (IOException ioe) {
146+
return;
147+
}
148+
fail("Queue should have been auto-deleted after we removed its only consumer");
149+
}
150+
151+
public void testExclusiveNotAutoDelete() throws IOException {
152+
String name = "exclusivequeue";
153+
AMQP.Queue.DeclareOk qok = channel.queueDeclare(name, false, true, false, null);
154+
// now it's there
155+
verifyQueue(name, false, true, false, null);
156+
QueueingConsumer consumer = new QueueingConsumer(channel);
157+
channel.basicConsume(name, consumer);
158+
channel.basicCancel(consumer.getConsumerTag());
159+
// and still there, because exclusive no longer implies autodelete
160+
verifyQueueExists(name);
161+
}
162+
163+
public void testExclusiveGoesWithConnection() throws IOException {
164+
String name = "exclusivequeue2";
165+
AMQP.Queue.DeclareOk qok = channel.queueDeclare(name, false, true, false, null);
166+
// now it's there
167+
verifyQueue(name, false, true, false, null);
168+
closeConnection();
169+
openConnection();
170+
openChannel();
171+
verifyQueueMissing(name);
172+
}
173+
174+
}

0 commit comments

Comments
 (0)