Skip to content

Commit caba8b8

Browse files
committed
Improve resilience of some tests
Those regularly fail on CI.
1 parent 139b4e7 commit caba8b8

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ private void sleep(long millis) {
583583
publication time + TTL + latency */
584584
private void checkPromptArrival(QueueingConsumer c,
585585
int count, long latency) throws Exception {
586-
long epsilon = TTL / 20;
586+
long epsilon = TTL / 10;
587587
for (int i = 0; i < count; i++) {
588588
Delivery d = c.nextDelivery(TTL + TTL + latency + epsilon);
589589
assertNotNull("message #" + i + " did not expire", d);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ protected void releaseResources() throws IOException {
156156
closeChannel();
157157
openChannel();
158158

159-
Thread.sleep(150);
159+
Thread.sleep(110);
160160
expectBodyAndRemainingMessages(MSG[1], 1);
161161
expectBodyAndRemainingMessages(MSG[2], 0);
162162
}

src/test/java/com/rabbitmq/client/test/server/XDeathHeaderGrowth.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.UUID;
2929
import java.util.concurrent.CountDownLatch;
3030
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.atomic.AtomicLong;
3132

3233
import org.junit.Test;
3334

@@ -40,24 +41,35 @@
4041
class RejectingConsumer extends DefaultConsumer {
4142
private CountDownLatch latch;
4243
private Map<String, Object> headers;
44+
private AtomicLong counter;
4345

4446
public RejectingConsumer(Channel channel, CountDownLatch latch) {
4547
super(channel);
4648
this.latch = latch;
49+
this.counter = new AtomicLong(latch.getCount());
4750
}
4851

4952
@Override
5053
public void handleDelivery(String consumerTag, Envelope envelope,
5154
AMQP.BasicProperties properties, byte[] body)
5255
throws IOException {
56+
5357
if(this.latch.getCount() > 0) {
5458
this.getChannel().basicReject(envelope.getDeliveryTag(), false);
5559
} else {
5660
if(this.getChannel().isOpen()) {
5761
this.getChannel().basicAck(envelope.getDeliveryTag(), false);
5862
}
5963
}
60-
this.headers = properties.getHeaders();
64+
if(this.counter.decrementAndGet() == 0) {
65+
// get headers only when the message has been redelivered
66+
// the expected number of times.
67+
// it looks like the message can be redelivered because
68+
// of the reject when handleDelivery isn't done yet or
69+
// before the latch releases the main thread. There's then
70+
// an additional delivery and the checks (transiently) fail.
71+
this.headers = properties.getHeaders();
72+
}
6173
latch.countDown();
6274
}
6375

@@ -88,7 +100,7 @@ public class XDeathHeaderGrowth extends BrokerTestCase {
88100
this.channel.queueBind(q3, x2, "");
89101

90102
final String qz = "issues.rabbitmq-server-78.destination";
91-
declareTransientQueue(qz, argumentsForDeadLetteringTo(x3));
103+
declareTransientQueue(qz, argumentsForDeadLetteringWithoutTtlTo(x3));
92104
this.channel.queueBind(qz, x3, "");
93105

94106
CountDownLatch latch = new CountDownLatch(10);
@@ -143,7 +155,7 @@ private void cleanUpQueues(String... qs) throws IOException {
143155
this.channel.queueBind(q2, x1, "");
144156

145157
final String qz = "issues.rabbitmq-server-152.destination";
146-
declareTransientQueue(qz, argumentsForDeadLetteringTo(x2));
158+
declareTransientQueue(qz, argumentsForDeadLetteringWithoutTtlTo(x2));
147159
this.channel.queueBind(qz, x2, "");
148160

149161
CountDownLatch latch = new CountDownLatch(10);
@@ -215,11 +227,17 @@ private Map<String, Object> argumentsForDeadLetteringTo(String dlx) {
215227
return argumentsForDeadLetteringTo(dlx, 1);
216228
}
217229

230+
private Map<String, Object> argumentsForDeadLetteringWithoutTtlTo(String dlx) {
231+
return argumentsForDeadLetteringTo(dlx, -1);
232+
}
233+
218234
private Map<String, Object> argumentsForDeadLetteringTo(String dlx, int ttl) {
219235
Map<String, Object> m = new HashMap<String, Object>();
220236
m.put("x-dead-letter-exchange", dlx);
221237
m.put("x-dead-letter-routing-key", "some-routing-key");
222-
m.put("x-message-ttl", ttl);
238+
if(ttl > 0) {
239+
m.put("x-message-ttl", ttl);
240+
}
223241
return m;
224242
}
225243
}

0 commit comments

Comments
 (0)