Skip to content

Commit b5302a1

Browse files
author
Simon MacMullen
committed
Merge bug21922 into default.
2 parents c7c12d9 + fa08487 commit b5302a1

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public static TestSuite suite() {
5757
suite.addTestSuite(ExchangeDeclare.class);
5858
suite.addTestSuite(FrameMax.class);
5959
suite.addTestSuite(QueueLifecycle.class);
60+
suite.addTestSuite(QueueLease.class);
6061
suite.addTestSuite(QueueExclusivity.class);
6162
suite.addTestSuite(InvalidAcks.class);
6263
suite.addTestSuite(InvalidAcksTx.class);
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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-2010 LShift
22+
// Ltd. Portions created by Cohesive Financial Technologies LLC are
23+
// Copyright (C) 2007-2010 Cohesive Financial Technologies
24+
// LLC. Portions created by Rabbit Technologies Ltd are Copyright
25+
// (C) 2007-2010 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.io.IOException;
35+
import java.util.HashMap;
36+
import java.util.Map;
37+
38+
import com.rabbitmq.client.AMQP;
39+
import com.rabbitmq.client.test.BrokerTestCase;
40+
41+
public class QueueLease extends BrokerTestCase {
42+
43+
private final static String TEST_EXPIRE_QUEUE = "leaseq";
44+
private final static String TEST_NORMAL_QUEUE = "noleaseq";
45+
private final static String TEST_EXPIRE_REDECLARE_QUEUE = "equivexpire";
46+
47+
// Currently the expiration timer is very responsive but this may
48+
// very well change in the future, so tweak accordingly.
49+
private final static long QUEUE_EXPIRES = 1000L; // msecs
50+
private final static int SHOULD_EXPIRE_WITHIN = 2000;
51+
52+
/**
53+
* Verify that a queue with the 'x-expires` flag is actually deleted within
54+
* a sensible period of time after expiry.
55+
*/
56+
public void testQueueExpires() throws IOException, InterruptedException {
57+
verifyQueueExpires(TEST_EXPIRE_QUEUE, true);
58+
}
59+
60+
/**
61+
* Verify that the server does not delete normal queues... ;)
62+
*/
63+
public void testDoesNotExpireOthers() throws IOException,
64+
InterruptedException {
65+
verifyQueueExpires(TEST_NORMAL_QUEUE, false);
66+
}
67+
68+
/**
69+
* Verify that the server throws an error if the type of x-expires is not
70+
* long.
71+
*/
72+
public void testExpireMustBeLong() throws IOException {
73+
Map<String, Object> args = new HashMap<String, Object>();
74+
args.put("x-expires", 100);
75+
76+
try {
77+
channel
78+
.queueDeclare("expiresMustBeLong", false, false, false,
79+
args);
80+
fail("server accepted x-expires not of type long");
81+
} catch (IOException e) {
82+
checkShutdownSignal(AMQP.PRECONDITION_FAILED, e);
83+
}
84+
}
85+
86+
public void testExpireMustBeGtZero() throws IOException {
87+
Map<String, Object> args = new HashMap<String, Object>();
88+
args.put("x-expires", 0L);
89+
90+
try {
91+
channel.queueDeclare("expiresMustBeGtZero", false, false, false,
92+
args);
93+
fail("server accepted x-expires of zero ms.");
94+
} catch (IOException e) {
95+
checkShutdownSignal(AMQP.PRECONDITION_FAILED, e);
96+
}
97+
}
98+
99+
public void testExpireMustBePositive() throws IOException {
100+
Map<String, Object> args = new HashMap<String, Object>();
101+
args.put("x-expires", -10L);
102+
103+
try {
104+
channel.queueDeclare("expiresMustBePositive", false, false, false,
105+
args);
106+
fail("server accepted negative x-expires.");
107+
} catch (IOException e) {
108+
checkShutdownSignal(AMQP.PRECONDITION_FAILED, e);
109+
}
110+
}
111+
112+
/**
113+
* Verify that the server throws an error if the client redeclares a queue
114+
* with mismatching 'x-expires' values.
115+
*/
116+
public void testQueueRedeclareEquivalence() throws IOException {
117+
Map<String, Object> args1 = new HashMap<String, Object>();
118+
args1.put("x-expires", 10000L);
119+
Map<String, Object> args2 = new HashMap<String, Object>();
120+
args2.put("x-expires", 20000L);
121+
122+
channel.queueDeclare(TEST_EXPIRE_REDECLARE_QUEUE, false, false, false,
123+
args1);
124+
125+
try {
126+
channel.queueDeclare(TEST_EXPIRE_REDECLARE_QUEUE, false, false,
127+
false, args2);
128+
fail("Able to redeclare queue with mismatching expire flags.");
129+
} catch (IOException e) {
130+
checkShutdownSignal(AMQP.NOT_ALLOWED, e);
131+
}
132+
}
133+
134+
void verifyQueueExpires(String name, boolean expire) throws IOException,
135+
InterruptedException {
136+
Map<String, Object> args = new HashMap<String, Object>();
137+
if (expire) {
138+
args.put("x-expires", QUEUE_EXPIRES);
139+
}
140+
141+
channel.queueDeclare(name, false, false, false, args);
142+
143+
Thread.sleep(SHOULD_EXPIRE_WITHIN / 4);
144+
145+
try {
146+
channel.queueDeclarePassive(name);
147+
} catch (IOException e) {
148+
fail("Queue expired before deadline.");
149+
}
150+
151+
Thread.sleep(SHOULD_EXPIRE_WITHIN); // be on the safe side
152+
153+
try {
154+
channel.queueDeclarePassive(name);
155+
if (expire) {
156+
fail("Queue should have been expired by now.");
157+
}
158+
} catch (IOException e) {
159+
if (expire) {
160+
checkShutdownSignal(AMQP.NOT_FOUND, e);
161+
} else {
162+
fail("Queue without expire flag deleted.");
163+
}
164+
}
165+
}
166+
167+
protected void releaseResources() throws IOException {
168+
try {
169+
channel.queueDelete(TEST_NORMAL_QUEUE);
170+
channel.queueDelete(TEST_EXPIRE_QUEUE);
171+
channel.queueDelete(TEST_EXPIRE_REDECLARE_QUEUE);
172+
} catch (IOException e) {
173+
}
174+
175+
super.releaseResources();
176+
}
177+
}

0 commit comments

Comments
 (0)