Skip to content

Commit 2aaeff1

Browse files
committed
merge default into bug14647
2 parents 50ab31f + 2d80614 commit 2aaeff1

File tree

4 files changed

+204
-11
lines changed

4 files changed

+204
-11
lines changed

test/src/com/rabbitmq/client/test/BrokerTestCase.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
import com.rabbitmq.client.GetResponse;
4343
import com.rabbitmq.client.MessageProperties;
4444
import com.rabbitmq.client.ShutdownSignalException;
45-
45+
import com.rabbitmq.client.AlreadyClosedException;
46+
import com.rabbitmq.client.impl.ShutdownNotifierComponent;
4647
import com.rabbitmq.client.AMQP;
4748

4849
public class BrokerTestCase extends TestCase
@@ -128,6 +129,16 @@ public void closeChannel()
128129

129130
public void checkShutdownSignal(int expectedCode, IOException ioe) {
130131
ShutdownSignalException sse = (ShutdownSignalException) ioe.getCause();
132+
checkShutdownSignal(expectedCode, sse);
133+
}
134+
135+
public void checkShutdownSignal(int expectedCode, AlreadyClosedException ace) {
136+
ShutdownNotifierComponent snc = (ShutdownNotifierComponent) ace.getReference();
137+
ShutdownSignalException sse = snc.getCloseReason();
138+
checkShutdownSignal(expectedCode, sse);
139+
}
140+
141+
public void checkShutdownSignal(int expectedCode, ShutdownSignalException sse) {
131142
Command closeCommand = (Command) sse.getReason();
132143
channel = null;
133144
if (sse.isHardError()) {
@@ -140,6 +151,20 @@ public void checkShutdownSignal(int expectedCode, IOException ioe) {
140151
}
141152
}
142153

154+
public void expectChannelError(int error) {
155+
try {
156+
channel.basicQos(0);
157+
fail("Expected channel error " + error);
158+
} catch (IOException ioe) {
159+
// If we get a channel close back when flushing it with the
160+
// synchronous basicQos above.
161+
checkShutdownSignal(error, ioe);
162+
} catch (AlreadyClosedException ace) {
163+
// If it has already closed of its own accord before we got there.
164+
checkShutdownSignal(error, ace);
165+
}
166+
}
167+
143168
protected void assertDelivered(String q, int count) throws IOException {
144169
assertDelivered(q, count, false);
145170
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public static TestSuite suite() {
5858
suite.addTestSuite(ExchangeDeclare.class);
5959
suite.addTestSuite(FrameMax.class);
6060
suite.addTestSuite(QueueLifecycle.class);
61+
suite.addTestSuite(QueueLease.class);
6162
suite.addTestSuite(QueueExclusivity.class);
6263
suite.addTestSuite(InvalidAcks.class);
6364
suite.addTestSuite(InvalidAcksTx.class);

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.rabbitmq.client.test.functional;
22

33
import com.rabbitmq.client.AMQP;
4-
import com.rabbitmq.client.GetResponse;
54
import com.rabbitmq.client.test.BrokerTestCase;
65

76
import java.io.IOException;
@@ -32,15 +31,6 @@ public void testDoubleAck()
3231
expectChannelError(AMQP.PRECONDITION_FAILED);
3332
}
3433

35-
private void expectChannelError(int error) {
36-
try {
37-
channel.queueDeclare();
38-
fail("Expected channel error " + error);
39-
} catch (IOException e) {
40-
checkShutdownSignal(error, e);
41-
}
42-
}
43-
4434
public void testCrazyAck()
4535
throws IOException
4636
{
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)