Skip to content

Commit 886b830

Browse files
author
Tim Watson
committed
reduce duplication again; test choice of ttl between per-queue and per-message
1 parent b1bf961 commit 886b830

File tree

5 files changed

+56
-40
lines changed

5 files changed

+56
-40
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ protected void basicPublishVolatile(byte[] msg, String x, String routingKey) thr
203203
basicPublishVolatile(msg, x, routingKey, MessageProperties.TEXT_PLAIN);
204204
}
205205

206-
protected void basicPublishVolatile(byte[] msg, String x, String routingKey,
206+
public void basicPublishVolatile(byte[] msg, String x, String routingKey,
207207
AMQP.BasicProperties properties) throws IOException {
208208
channel.basicPublish(x, routingKey, properties, msg);
209209
}

test/src/com/rabbitmq/client/test/functional/PerMessageTTL.java renamed to test/src/com/rabbitmq/client/test/functional/PerMessageTTLHandling.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,9 @@
2121

2222
import java.io.IOException;
2323

24-
public class PerMessageTTL extends TTLHandling {
24+
public abstract class PerMessageTTLHandling extends TTLHandling {
2525

26-
private Object sessionTTL;
27-
28-
@Override
29-
public void testInvalidTypeUsedInTTL() throws Exception {
30-
try {
31-
super.testInvalidTypeUsedInTTL();
32-
} catch (IOException e) {
33-
checkShutdownSignal(AMQP.INTERNAL_ERROR, e);
34-
}
35-
}
36-
37-
@Override
38-
public void testTTLMustBePositive() throws Exception {
39-
try {
40-
super.testTTLMustBePositive();
41-
} catch (IOException e) {
42-
checkShutdownSignal(AMQP.INTERNAL_ERROR, e);
43-
}
44-
}
26+
protected Object sessionTTL;
4527

4628
@Override
4729
protected void publish(String msg) throws IOException {
@@ -57,4 +39,5 @@ protected AMQP.Queue.DeclareOk declareQueue(String name, Object ttlValue) throws
5739
this.sessionTTL = ttlValue;
5840
return this.channel.queueDeclare(name, false, true, false, null);
5941
}
42+
6043
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,29 @@
1818
package com.rabbitmq.client.test.functional;
1919

2020
import com.rabbitmq.client.AMQP;
21+
import com.rabbitmq.client.MessageProperties;
2122

2223
import java.io.IOException;
2324
import java.util.Collections;
2425
import java.util.Map;
2526

2627
public class PerQueueTTL extends TTLHandling {
2728

28-
private static final String TTL_ARG = "x-message-ttl";
29+
protected static final String TTL_ARG = "x-message-ttl";
2930

30-
@Override
3131
public void testInvalidTypeUsedInTTL() throws Exception {
3232
try {
33-
super.testInvalidTypeUsedInTTL();
33+
declareQueue(TTL_INVALID_QUEUE_NAME, "foobar");
34+
fail("Should not be able to use a non-long value for x-message-ttl");
3435
} catch (IOException e) {
3536
checkShutdownSignal(AMQP.PRECONDITION_FAILED, e);
3637
}
3738
}
3839

39-
@Override
4040
public void testTTLMustBePositive() throws Exception {
4141
try {
42-
super.testTTLMustBePositive();
42+
declareQueue(TTL_INVALID_QUEUE_NAME, -10);
43+
fail("Should not be able to use negative values for x-message-ttl");
4344
} catch (IOException e) {
4445
checkShutdownSignal(AMQP.PRECONDITION_FAILED, e);
4546
}
@@ -78,4 +79,11 @@ public void testQueueReDeclareSemanticNonEquivalence() throws Exception {
7879
}
7980
}
8081

82+
protected void publishWithExpiration(String msg, Object sessionTTL) throws IOException {
83+
basicPublishVolatile(msg.getBytes(), TTL_EXCHANGE, TTL_QUEUE_NAME,
84+
MessageProperties.TEXT_PLAIN
85+
.builder()
86+
.expiration(String.valueOf(sessionTTL))
87+
.build());
88+
}
8189
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.rabbitmq.client.test.functional;
2+
3+
import com.rabbitmq.client.AMQP;
4+
5+
import java.io.IOException;
6+
import java.util.Collections;
7+
import java.util.Map;
8+
9+
public class PerQueueVsPerMessageTTL extends PerMessageTTLHandling {
10+
11+
public void testSmallerPerQueueExpiryWins() throws IOException, InterruptedException {
12+
declareAndBindQueue(10);
13+
14+
sessionTTL = 1000;
15+
publish("message1");
16+
17+
Thread.sleep(10);
18+
19+
assertNull("per-queue ttl should have removed message after 10ms!", get());
20+
}
21+
22+
public void testSmallerPerMessageExpiryWins() throws IOException, InterruptedException {
23+
declareAndBindQueue(5000);
24+
25+
sessionTTL = 10;
26+
publish("message2");
27+
28+
Thread.sleep(1000);
29+
30+
assertNull("per-message ttl should have removed message after 10ms!", get());
31+
}
32+
33+
@Override
34+
protected AMQP.Queue.DeclareOk declareQueue(String name, Object ttlValue) throws IOException {
35+
this.sessionTTL = ttlValue;
36+
Map<String, Object> argMap = Collections.singletonMap(PerQueueTTL.TTL_ARG, ttlValue);
37+
return this.channel.queueDeclare(name, false, true, false, argMap);
38+
}
39+
}

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import com.rabbitmq.client.test.BrokerTestCase;
77

88
import java.io.IOException;
9-
import java.util.Collections;
10-
import java.util.Map;
119

1210
public abstract class TTLHandling extends BrokerTestCase {
1311

@@ -49,18 +47,6 @@ public void testTTLAllowZero() throws Exception {
4947
}
5048
}
5149

52-
public void testInvalidTypeUsedInTTL() throws Exception {
53-
declareQueue(TTL_INVALID_QUEUE_NAME, "foobar");
54-
publishAndSync(MSG[0]);
55-
fail("Should not be able to use a non-long value for ttl");
56-
}
57-
58-
public void testTTLMustBePositive() throws Exception {
59-
declareQueue(TTL_INVALID_QUEUE_NAME, -10);
60-
publishAndSync(MSG[0]);
61-
fail("Should not be able to use negative values for ttl");
62-
}
63-
6450
public void testMessagesExpireWhenUsingBasicGet() throws Exception {
6551
declareAndBindQueue(200);
6652
publish(MSG[0]);

0 commit comments

Comments
 (0)