Skip to content

Commit a1a30ef

Browse files
committed
Generate queue names in tests, delete test queues after test
(cherry picked from commit 28b576a)
1 parent b8ac0eb commit a1a30ef

File tree

5 files changed

+91
-29
lines changed

5 files changed

+91
-29
lines changed

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

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -13,11 +13,11 @@
1313
// If you have any questions regarding licensing, please contact us at
1414
1515

16-
1716
package com.rabbitmq.client.test;
1817

1918
import com.rabbitmq.client.*;
2019
import com.rabbitmq.client.impl.nio.NioParams;
20+
import com.rabbitmq.client.test.TestUtils.TestDescription;
2121
import com.rabbitmq.tools.Host;
2222
import org.junit.After;
2323
import org.junit.Before;
@@ -38,6 +38,9 @@
3838

3939
public class BrokerTestCase {
4040

41+
@Rule
42+
public TestDescription testDescription = new TestDescription();
43+
4144
private static final Logger LOGGER = LoggerFactory.getLogger(BrokerTestCase.class);
4245

4346
@Rule
@@ -339,11 +342,22 @@ protected void unblock() throws IOException, InterruptedException {
339342
}
340343

341344
protected String generateQueueName() {
342-
return "queue" + UUID.randomUUID().toString();
345+
return name("queue", this.testDescription.getDescription().getTestClass(),
346+
this.testDescription.getDescription().getMethodName());
343347
}
344348

345349
protected String generateExchangeName() {
346-
return "exchange" + UUID.randomUUID().toString();
350+
return name("exchange", this.testDescription.getDescription().getTestClass(),
351+
this.testDescription.getDescription().getMethodName());
347352
}
348353

354+
private static String name(String prefix, Class<?> testClass, String testMethodName) {
355+
String uuid = UUID.randomUUID().toString();
356+
return String.format(
357+
"%s_%s_%s%s",
358+
prefix, testClass.getSimpleName(), testMethodName, uuid.substring(uuid.length() / 2));
359+
}
360+
361+
362+
349363
}

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

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -28,6 +28,7 @@
2828
import org.assertj.core.api.Assertions;
2929
import org.junit.AssumptionViolatedException;
3030
import org.junit.rules.TestRule;
31+
import org.junit.rules.TestWatcher;
3132
import org.junit.runner.Description;
3233
import org.junit.runner.Runner;
3334
import org.junit.runner.notification.RunNotifier;
@@ -531,4 +532,30 @@ private static int javaMajorVersion() {
531532
}
532533
throw new IllegalStateException("Could not determine Java major version");
533534
}
535+
536+
public static void safeDelete(Connection connection, String queue) {
537+
try {
538+
Channel ch = connection.createChannel();
539+
ch.queueDelete(queue);
540+
ch.close();
541+
} catch (Exception e) {
542+
// OK
543+
}
544+
}
545+
546+
public static class TestDescription extends TestWatcher {
547+
548+
private volatile Description description;
549+
550+
@Override
551+
protected void starting(Description d) {
552+
description = d;
553+
}
554+
555+
public Description getDescription() {
556+
return description;
557+
}
558+
}
559+
560+
534561
}

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

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -26,11 +26,13 @@
2626
import java.util.*;
2727
import java.util.concurrent.*;
2828

29+
import static com.rabbitmq.client.test.TestUtils.safeDelete;
2930
import static com.rabbitmq.client.test.TestUtils.waitAtMost;
3031
import static java.time.Duration.ofSeconds;
3132
import static org.junit.Assert.*;
3233

3334
public class DeadLetterExchange extends BrokerTestCase {
35+
3436
public static final String DLX = "dead.letter.exchange";
3537
private static final String DLX_ARG = "x-dead-letter-exchange";
3638
private static final String DLX_RK_ARG = "x-dead-letter-routing-key";
@@ -344,21 +346,28 @@ protected void releaseResources() throws IOException {
344346
// messages in pure-expiry cycles. So we just need to test that
345347
// non-pure-expiry cycles do not drop messages.
346348

347-
declareQueue("queue1", "", "queue2", null, 1);
348-
declareQueue("queue2", "", "queue1", null, 0);
349-
350-
channel.basicPublish("", "queue1", MessageProperties.BASIC, "".getBytes());
351-
final CountDownLatch latch = new CountDownLatch(10);
352-
channel.basicConsume("queue2", false,
353-
new DefaultConsumer(channel) {
354-
@Override
355-
public void handleDelivery(String consumerTag, Envelope envelope,
356-
AMQP.BasicProperties properties, byte[] body) throws IOException {
357-
channel.basicReject(envelope.getDeliveryTag(), false);
358-
latch.countDown();
359-
}
360-
});
361-
assertTrue(latch.await(10, TimeUnit.SECONDS));
349+
String queue1 = generateQueueName();
350+
String queue2 = generateQueueName();
351+
try {
352+
declareQueue(queue1, "", queue2, null, 1);
353+
declareQueue(queue2, "", queue1, null, 0);
354+
355+
channel.basicPublish("", queue1, MessageProperties.BASIC, "".getBytes());
356+
final CountDownLatch latch = new CountDownLatch(10);
357+
channel.basicConsume(queue2, false,
358+
new DefaultConsumer(channel) {
359+
@Override
360+
public void handleDelivery(String consumerTag, Envelope envelope,
361+
AMQP.BasicProperties properties, byte[] body) throws IOException {
362+
channel.basicReject(envelope.getDeliveryTag(), false);
363+
latch.countDown();
364+
}
365+
});
366+
assertTrue(latch.await(10, TimeUnit.SECONDS));
367+
} finally {
368+
safeDelete(connection, queue1);
369+
safeDelete(connection, queue2);
370+
}
362371
}
363372

364373
@SuppressWarnings("unchecked")

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
1+
// Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -16,6 +16,7 @@
1616

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

19+
import static com.rabbitmq.client.test.TestUtils.safeDelete;
1920
import static org.junit.Assert.fail;
2021

2122
import java.io.IOException;
@@ -41,8 +42,7 @@ protected boolean isAutomaticRecoveryEnabled() {
4142
return false;
4243
}
4344

44-
void verifyQueueMissing(Channel channel, String queueName)
45-
throws IOException {
45+
void verifyQueueMissing(Channel channel, String queueName) {
4646
try {
4747
channel.queueDeclare(queueName, false, false, false, null);
4848
} catch (IOException ioe) {
@@ -54,9 +54,15 @@ void verifyQueueMissing(Channel channel, String queueName)
5454
// 1) connection and queue are on same node, node restarts -> queue
5555
// should no longer exist
5656
@Test public void connectionQueueSameNode() throws Exception {
57-
channel.queueDeclare("scenario1", true, true, false, null);
58-
restartPrimaryAbruptly();
59-
verifyQueueMissing(channel, "scenario1");
57+
String queueName = generateQueueName();
58+
safeDelete(connection, queueName);
59+
try {
60+
channel.queueDeclare(queueName, true, true, false, null);
61+
restartPrimaryAbruptly();
62+
verifyQueueMissing(channel, queueName);
63+
} finally {
64+
safeDelete(connection, queueName);
65+
}
6066
}
6167

6268
private void restartPrimaryAbruptly() throws IOException, TimeoutException {
@@ -80,4 +86,5 @@ private void restartPrimaryAbruptly() throws IOException, TimeoutException {
8086
* tied to nodes, so one can't engineer a situation in which a connection
8187
* and its exclusive queue are on different nodes.
8288
*/
89+
8390
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import com.rabbitmq.client.test.AbstractRMQTestSuite;
1919
import com.rabbitmq.client.test.RequiredPropertiesSuite;
20+
import com.rabbitmq.client.test.functional.DeadLetterExchange;
21+
import com.rabbitmq.client.test.functional.DurableOnTransient;
2022
import com.rabbitmq.client.test.functional.FunctionalTests;
23+
import com.rabbitmq.client.test.functional.QosTests;
2124
import com.rabbitmq.tools.Host;
2225
import org.junit.Test;
2326
import org.junit.runner.RunWith;
@@ -26,8 +29,10 @@
2629
@RunWith(RequiredPropertiesSuite.class)
2730
@Suite.SuiteClasses({
2831
HATests.SetUp.class,
29-
FunctionalTests.class,
30-
ServerTests.class,
32+
// FunctionalTests.class,
33+
// ServerTests.class,
34+
ExclusiveQueueDurability.class,
35+
DeadLetterExchange.class,
3136
HATests.TearDown.class
3237
})
3338
public class HATests {

0 commit comments

Comments
 (0)