Skip to content

Commit 1d6c4cf

Browse files
committed
simplifying refactor
1 parent bc499b9 commit 1d6c4cf

File tree

1 file changed

+57
-80
lines changed

1 file changed

+57
-80
lines changed

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

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
*/
3131
public class PerQueueTTL extends BrokerTestCase {
3232

33-
private static final String TTL_EXCHANGE = "ttl.exchange";
34-
35-
private static final String TTL_ARG = "x-message-ttl";
36-
37-
private static final String TTL_QUEUE_NAME = "queue.ttl";
38-
33+
private static final String TTL_EXCHANGE = "ttl.exchange";
34+
private static final String TTL_ARG = "x-message-ttl";
35+
private static final String TTL_QUEUE_NAME = "queue.ttl";
3936
private static final String TTL_INVALID_QUEUE_NAME = "invalid.queue.ttl";
4037

38+
private static final String[] MSG = {"one", "two", "three"};
39+
4140
@Override
4241
protected void createResources() throws IOException {
4342
this.channel.exchangeDeclare(TTL_EXCHANGE, "direct");
@@ -48,33 +47,15 @@ protected void releaseResources() throws IOException {
4847
this.channel.exchangeDelete(TTL_EXCHANGE);
4948
}
5049

51-
public void testCreateQueueWithByteTTL() throws IOException {
52-
try {
53-
declareQueue(TTL_QUEUE_NAME, (byte)200);
54-
} catch(IOException ex) {
55-
fail("Should be able to use byte for queue TTL");
56-
}
57-
}
58-
public void testCreateQueueWithShortTTL() throws IOException {
59-
try {
60-
declareQueue(TTL_QUEUE_NAME, (short)200);
61-
} catch(IOException ex) {
62-
fail("Should be able to use short for queue TTL");
63-
}
64-
}
65-
public void testCreateQueueWithIntTTL() throws IOException {
66-
try {
67-
declareQueue(TTL_QUEUE_NAME, 200);
68-
} catch(IOException ex) {
69-
fail("Should be able to use int for queue TTL");
70-
}
71-
}
72-
73-
public void testCreateQueueWithLongTTL() throws IOException {
74-
try {
75-
declareQueue(TTL_QUEUE_NAME, 200L);
76-
} catch(IOException ex) {
77-
fail("Should be able to use long for queue TTL");
50+
public void testCreateQueueTTLTypes() throws IOException {
51+
Object[] args = { (byte)200, (short)200, 200, 200L };
52+
for (Object ttl : args) {
53+
try {
54+
declareQueue(ttl);
55+
} catch(IOException ex) {
56+
fail("Should be able to use " + ttl.getClass().getName() +
57+
" for x-message-ttl");
58+
}
7859
}
7960
}
8061

@@ -99,34 +80,34 @@ public void testTTLMustBeGtZero() throws Exception {
9980
public void testTTLMustBePositive() throws Exception {
10081
try {
10182
declareQueue(TTL_INVALID_QUEUE_NAME, -10);
102-
fail("Should not be able to declare a queue with zero for x-message-ttl");
83+
fail("Should not be able to declare a queue with negative value for x-message-ttl");
10384
} catch (IOException e) {
10485
checkShutdownSignal(AMQP.PRECONDITION_FAILED, e);
10586
}
10687
}
10788

10889
public void testQueueRedeclareEquivalence() throws Exception {
109-
declareQueue(TTL_QUEUE_NAME, 10);
90+
declareQueue(10);
11091
try {
111-
declareQueue(TTL_QUEUE_NAME, 20);
112-
fail("Should not be able to redeclare with different TTL");
92+
declareQueue(20);
93+
fail("Should not be able to redeclare with different x-message-ttl");
11394
} catch(IOException ex) {
11495
checkShutdownSignal(AMQP.PRECONDITION_FAILED, ex);
11596
}
11697
}
11798

11899
public void testQueueRedeclareSemanticEquivalence() throws Exception {
119-
declareQueue(TTL_QUEUE_NAME, (byte)10);
120-
declareQueue(TTL_QUEUE_NAME, 10);
121-
declareQueue(TTL_QUEUE_NAME, (short)10);
122-
declareQueue(TTL_QUEUE_NAME, 10L);
100+
declareQueue((byte)10);
101+
declareQueue(10);
102+
declareQueue((short)10);
103+
declareQueue(10L);
123104
}
124105

125106
public void testQueueRedeclareSemanticNonEquivalence() throws Exception {
126-
declareQueue(TTL_QUEUE_NAME, 10);
107+
declareQueue(10);
127108
try {
128-
declareQueue(TTL_QUEUE_NAME, 10.0);
129-
fail("Should not be able to redeclare with argument of different type");
109+
declareQueue(10.0);
110+
fail("Should not be able to redeclare with x-message-ttl argument of different type");
130111
} catch(IOException ex) {
131112
checkShutdownSignal(AMQP.PRECONDITION_FAILED, ex);
132113
}
@@ -136,48 +117,37 @@ public void testQueueRedeclareSemanticNonEquivalence() throws Exception {
136117
* Test messages expire when using basic get.
137118
*/
138119
public void testPublishAndGetWithExpiry() throws Exception {
139-
long ttl = 2000;
140-
declareQueue(TTL_QUEUE_NAME, ttl);
141-
this.channel.queueBind(TTL_QUEUE_NAME, TTL_EXCHANGE, TTL_QUEUE_NAME);
120+
declareAndBindQueue(2000);
142121

143-
byte[] msg1 = "one".getBytes();
144-
byte[] msg2 = "two".getBytes();
145-
byte[] msg3 = "three".getBytes();
146-
147-
basicPublishVolatile(msg1, TTL_EXCHANGE, TTL_QUEUE_NAME);
122+
publish(MSG[0]);
148123
Thread.sleep(1500);
149124

150-
basicPublishVolatile(msg2, TTL_EXCHANGE, TTL_QUEUE_NAME);
125+
publish(MSG[1]);
151126
Thread.sleep(1000);
152127

153-
basicPublishVolatile(msg3, TTL_EXCHANGE, TTL_QUEUE_NAME);
128+
publish(MSG[2]);
154129

155-
assertEquals("two", new String(get()));
156-
assertEquals("three", new String(get()));
130+
assertEquals(MSG[1], new String(get()));
131+
assertEquals(MSG[2], new String(get()));
157132

158133
}
159-
134+
160135
/*
161136
* Test get expiry for messages sent under a transaction
162137
*/
163138
public void testTransactionalPublishWithGet() throws Exception {
164-
long ttl = 1000;
165-
declareQueue(TTL_QUEUE_NAME, ttl);
166-
this.channel.queueBind(TTL_QUEUE_NAME, TTL_EXCHANGE, TTL_QUEUE_NAME);
167-
168-
byte[] msg1 = "one".getBytes();
169-
byte[] msg2 = "two".getBytes();
139+
declareAndBindQueue(1000);
170140

171141
this.channel.txSelect();
172142

173-
basicPublishVolatile(msg1, TTL_EXCHANGE, TTL_QUEUE_NAME);
143+
publish(MSG[0]);
174144
Thread.sleep(1500);
175145

176-
basicPublishVolatile(msg2, TTL_EXCHANGE, TTL_QUEUE_NAME);
146+
publish(MSG[1]);
177147
this.channel.txCommit();
178148
Thread.sleep(500);
179149

180-
assertEquals("one", new String(get()));
150+
assertEquals(MSG[0], new String(get()));
181151
Thread.sleep(800);
182152

183153
assertNull(get());
@@ -187,28 +157,22 @@ public void testTransactionalPublishWithGet() throws Exception {
187157
* Test expiry of requeued messages
188158
*/
189159
public void testExpiryWithRequeue() throws Exception {
190-
long ttl = 1000;
191-
declareQueue(TTL_QUEUE_NAME, ttl);
192-
this.channel.queueBind(TTL_QUEUE_NAME, TTL_EXCHANGE, TTL_QUEUE_NAME);
193-
194-
byte[] msg1 = "one".getBytes();
195-
byte[] msg2 = "two".getBytes();
196-
byte[] msg3 = "three".getBytes();
160+
declareAndBindQueue(1000);
197161

198-
basicPublishVolatile(msg1, TTL_EXCHANGE, TTL_QUEUE_NAME);
162+
publish(MSG[0]);
199163
Thread.sleep(500);
200-
basicPublishVolatile(msg2, TTL_EXCHANGE, TTL_QUEUE_NAME);
201-
basicPublishVolatile(msg3, TTL_EXCHANGE, TTL_QUEUE_NAME);
164+
publish(MSG[1]);
165+
publish(MSG[2]);
202166

203-
expectBodyAndRemainingMessages("one", 2);
204-
expectBodyAndRemainingMessages("two", 1);
167+
expectBodyAndRemainingMessages(MSG[0], 2);
168+
expectBodyAndRemainingMessages(MSG[1], 1);
205169

206170
closeChannel();
207171
openChannel();
208172

209173
Thread.sleep(600);
210-
expectBodyAndRemainingMessages("two", 1);
211-
expectBodyAndRemainingMessages("three", 0);
174+
expectBodyAndRemainingMessages(MSG[1], 1);
175+
expectBodyAndRemainingMessages(MSG[2], 0);
212176
}
213177

214178

@@ -220,6 +184,19 @@ private byte[] get() throws IOException {
220184
return response.getBody();
221185
}
222186

187+
private void publish(String msg) throws IOException {
188+
basicPublishVolatile(msg.getBytes(), TTL_EXCHANGE, TTL_QUEUE_NAME);
189+
}
190+
191+
private void declareAndBindQueue(Object ttlValue) throws IOException {
192+
declareQueue(ttlValue);
193+
this.channel.queueBind(TTL_QUEUE_NAME, TTL_EXCHANGE, TTL_QUEUE_NAME);
194+
}
195+
196+
private AMQP.Queue.DeclareOk declareQueue(Object ttlValue) throws IOException {
197+
return declareQueue(TTL_QUEUE_NAME, ttlValue);
198+
}
199+
223200
private AMQP.Queue.DeclareOk declareQueue(String name, Object ttlValue) throws IOException {
224201
Map<String, Object> argMap = Collections.singletonMap(TTL_ARG, ttlValue);
225202
return this.channel.queueDeclare(name, false, true, false, argMap);

0 commit comments

Comments
 (0)