Skip to content

Commit d3e957f

Browse files
committed
Fix dead lettering flaky test
It was using Thread.sleep() statements, which are not reliable, using polling for assertion instead. (cherry picked from commit ea8aa89)
1 parent 2def016 commit d3e957f

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,20 @@ public static ConnectionFactory connectionFactory() {
5656
return connectionFactory;
5757
}
5858

59-
public static void waitAtMost(Duration timeout, BooleanSupplier condition) {
60-
if (condition.getAsBoolean()) {
61-
return;
59+
@FunctionalInterface
60+
public interface CallableBooleanSupplier {
61+
62+
boolean getAsBoolean() throws Exception;
63+
64+
}
65+
66+
public static void waitAtMost(Duration timeout, CallableBooleanSupplier condition) {
67+
try {
68+
if (condition.getAsBoolean()) {
69+
return;
70+
}
71+
} catch (Exception e) {
72+
throw new RuntimeException(e);
6273
}
6374
int waitTime = 100;
6475
int waitedTime = 0;
@@ -70,8 +81,12 @@ public static void waitAtMost(Duration timeout, BooleanSupplier condition) {
7081
Thread.currentThread().interrupt();
7182
throw new RuntimeException(e);
7283
}
73-
if (condition.getAsBoolean()) {
74-
return;
84+
try {
85+
if (condition.getAsBoolean()) {
86+
return;
87+
}
88+
} catch (Exception e) {
89+
throw new RuntimeException(e);
7590
}
7691
waitedTime += waitTime;
7792
}

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919
import com.rabbitmq.client.AMQP.BasicProperties;
2020
import com.rabbitmq.client.test.BrokerTestCase;
2121
import com.rabbitmq.client.test.TestUtils;
22+
import java.util.concurrent.atomic.AtomicReference;
2223
import org.junit.Test;
2324

2425
import java.io.IOException;
2526
import java.util.*;
2627
import java.util.concurrent.*;
2728

29+
import static com.rabbitmq.client.test.TestUtils.waitAtMost;
30+
import static java.time.Duration.ofSeconds;
2831
import static org.junit.Assert.*;
2932

3033
public class DeadLetterExchange extends BrokerTestCase {
@@ -403,9 +406,15 @@ public void handleDelivery(String consumerTag, Envelope envelope,
403406
channel.queueBind(DLQ, DLX, "test");
404407
publishN(1);
405408

406-
sleep(200);
407-
408-
GetResponse getResponse = channel.basicGet(DLQ, true);
409+
AtomicReference<GetResponse> responseRefeference = new AtomicReference<>();
410+
waitAtMost(
411+
ofSeconds(1),
412+
() -> {
413+
GetResponse response = channel.basicGet(DLQ, true);
414+
responseRefeference.set(response);
415+
return responseRefeference.get() != null;
416+
});
417+
GetResponse getResponse = responseRefeference.get();
409418
assertNotNull("Message not dead-lettered",
410419
getResponse);
411420
assertEquals("test message", new String(getResponse.getBody()));
@@ -432,9 +441,15 @@ public void handleDelivery(String consumerTag, Envelope envelope,
432441
.headers(headers)
433442
.build(), "test message".getBytes());
434443

435-
sleep(100);
436-
437-
getResponse = channel.basicGet(DLQ, true);
444+
responseRefeference.set(null);
445+
waitAtMost(
446+
ofSeconds(1),
447+
() -> {
448+
GetResponse response = channel.basicGet(DLQ, true);
449+
responseRefeference.set(response);
450+
return responseRefeference.get() != null;
451+
});
452+
getResponse = responseRefeference.get();
438453
assertNotNull("Message not dead-lettered", getResponse);
439454
assertEquals("test message", new String(getResponse.getBody()));
440455
headers = getResponse.getProps().getHeaders();
@@ -453,9 +468,17 @@ public void handleDelivery(String consumerTag, Envelope envelope,
453468
new AMQP.BasicProperties.Builder()
454469
.headers(headers)
455470
.build(), "test message".getBytes());
456-
sleep(100);
457471

458-
getResponse = channel.basicGet(DLQ, true);
472+
responseRefeference.set(null);
473+
waitAtMost(
474+
ofSeconds(1),
475+
() -> {
476+
GetResponse response = channel.basicGet(DLQ, true);
477+
responseRefeference.set(response);
478+
return responseRefeference.get() != null;
479+
});
480+
getResponse = responseRefeference.get();
481+
459482
assertNotNull("Message not dead-lettered", getResponse);
460483
assertEquals("test message", new String(getResponse.getBody()));
461484
headers = getResponse.getProps().getHeaders();

0 commit comments

Comments
 (0)