Skip to content

Commit c195514

Browse files
author
Ask Solem
committed
Initial commit of Ask's work
1 parent 5b0c630 commit c195514

File tree

2 files changed

+169
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)