Skip to content

Commit a99c82f

Browse files
committed
Use queue-declare-passive to check queue existence in test
1 parent 5ea3e25 commit a99c82f

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.rabbitmq.client.test.functional;
1818

1919
import com.rabbitmq.client.AMQP;
20+
import com.rabbitmq.client.Channel;
2021
import com.rabbitmq.client.Consumer;
2122
import com.rabbitmq.client.DefaultConsumer;
2223
import com.rabbitmq.client.test.BrokerTestCase;
@@ -27,6 +28,7 @@
2728
import java.util.Map;
2829
import java.util.concurrent.TimeoutException;
2930

31+
import static com.rabbitmq.client.test.TestUtils.waitAtMost;
3032
import static org.junit.jupiter.api.Assertions.fail;
3133

3234
/**
@@ -38,19 +40,21 @@ void verifyQueueExists(String name) throws IOException {
3840
channel.queueDeclarePassive(name);
3941
}
4042

41-
void verifyQueueMissing(String name) throws IOException {
42-
// we can't in general check with a passive declare, since that
43-
// may return an IOException because of exclusivity. But we can
44-
// check that we can happily declare another with the same name:
45-
// the only circumstance in which this won't result in an error is
46-
// if it doesn't exist.
47-
try {
48-
channel.queueDeclare(name, false, false, false, null);
49-
} catch (IOException ioe) {
50-
fail("Queue.Declare threw an exception, probably meaning that the queue already exists");
51-
}
52-
// clean up
53-
channel.queueDelete(name);
43+
void verifyQueueMissing(String name) {
44+
waitAtMost(
45+
() -> {
46+
Channel ch = connection.createChannel();
47+
boolean result;
48+
// the queue should be gone, declare-passive should fail
49+
try {
50+
ch.queueDeclarePassive(name);
51+
ch.close();
52+
result = false;
53+
} catch (IOException e) {
54+
result = true;
55+
}
56+
return result;
57+
});
5458
}
5559

5660
/**
@@ -139,7 +143,7 @@ void verifyNotEquivalent(boolean durable, boolean exclusive,
139143
}
140144

141145
@Test public void exclusiveNotAutoDelete() throws IOException {
142-
String name = "exclusivequeue";
146+
String name = generateQueueName();
143147
channel.queueDeclare(name, false, true, false, null);
144148
// now it's there
145149
verifyQueue(name, false, true, false, null);
@@ -151,7 +155,7 @@ void verifyNotEquivalent(boolean durable, boolean exclusive,
151155
}
152156

153157
@Test public void exclusiveGoesWithConnection() throws IOException, TimeoutException {
154-
String name = "exclusivequeue2";
158+
String name = generateQueueName();
155159
channel.queueDeclare(name, false, true, false, null);
156160
// now it's there
157161
verifyQueue(name, false, true, false, null);

0 commit comments

Comments
 (0)