Skip to content

Commit 261d1cc

Browse files
committed
Make sure queue has been deleted before redeclaring it
1 parent 8fab1c8 commit 261d1cc

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616

1717
package com.rabbitmq.client.test.functional;
1818

19+
import static com.rabbitmq.client.test.TestUtils.waitAtMost;
1920
import static org.junit.jupiter.api.Assertions.assertNotNull;
2021
import static org.junit.jupiter.api.Assertions.assertNull;
2122
import static org.junit.jupiter.api.Assertions.fail;
2223

2324
import java.io.IOException;
25+
import java.util.ArrayList;
26+
import java.util.List;
2427
import java.util.concurrent.TimeoutException;
2528

2629
import com.rabbitmq.client.AMQP;
30+
import com.rabbitmq.client.Channel;
2731
import com.rabbitmq.client.GetResponse;
2832
import com.rabbitmq.client.QueueingConsumer;
2933

@@ -63,40 +67,47 @@ protected void deleteExchangeAndQueue(Binding binding) throws IOException {
6367
}
6468

6569
protected void doAutoDelete(boolean durable, int queues) throws IOException, TimeoutException {
66-
String[] queueNames = null;
70+
List<String> queueNames = new ArrayList<>();
6771
Binding binding = Binding.randomBinding();
6872
channel.exchangeDeclare(binding.x, "direct", durable, true, null);
6973
channel.queueDeclare(binding.q, durable, false, true, null);
7074
channel.queueBind(binding.q, binding.x, binding.k);
7175
if (queues > 1) {
7276
int j = queues - 1;
73-
queueNames = new String[j];
7477
for (int i = 0; i < j; i++) {
75-
queueNames[i] = randomString();
76-
channel.queueDeclare(queueNames[i], durable, false, false, null);
77-
channel.queueBind(queueNames[i], binding.x, binding.k);
78-
channel.basicConsume(queueNames[i], true, new QueueingConsumer(channel));
78+
queueNames.add(randomString());
79+
channel.queueDeclare(queueNames.get(i), durable, false, false, null);
80+
channel.queueBind(queueNames.get(i), binding.x, binding.k);
81+
channel.basicConsume(queueNames.get(i), true, new QueueingConsumer(channel));
7982
}
8083
}
8184
subscribeSendUnsubscribe(binding);
8285
if (durable) {
8386
restart();
8487
}
85-
if (queues > 1 && queueNames != null) {
86-
for (String s : queueNames) {
87-
channel.basicConsume(s, true, new QueueingConsumer(channel));
88-
Binding tmp = new Binding(s, binding.x, binding.k);
88+
if (queues > 1) {
89+
for (String q : queueNames) {
90+
channel.basicConsume(q, true, new QueueingConsumer(channel));
91+
Binding tmp = new Binding(q, binding.x, binding.k);
8992
sendUnroutable(tmp);
9093
}
9194
}
95+
waitAtMost(() -> {
96+
Channel ch = connection.createChannel();
97+
try {
98+
ch.queueDeclarePassive(binding.q);
99+
} catch (IOException e) {
100+
return true;
101+
}
102+
return false;
103+
});
92104
channel.queueDeclare(binding.q, durable, true, true, null);
93105
// if (queues == 1): Because the exchange does not exist, this
94106
// bind should fail
95107
try {
96108
channel.queueBind(binding.q, binding.x, binding.k);
97109
sendRoutable(binding);
98-
}
99-
catch (IOException e) {
110+
} catch (IOException e) {
100111
checkShutdownSignal(AMQP.NOT_FOUND, e);
101112
channel = null;
102113
return;
@@ -106,7 +117,7 @@ protected void doAutoDelete(boolean durable, int queues) throws IOException, Tim
106117
fail("Queue bind should have failed");
107118
}
108119
// Do some cleanup
109-
if (queues > 1 && queueNames != null) {
120+
if (queues > 1) {
110121
for (String q : queueNames) {
111122
channel.queueDelete(q);
112123
}

0 commit comments

Comments
 (0)