Skip to content

Commit 4856d62

Browse files
committed
Close Netty event loop group asynchronously in tests
Should speed up test suite a bit.
1 parent 86e78a8 commit 4856d62

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/test/java/com/rabbitmq/stream/impl/ShutdownListenerTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void shutdownListenerShouldBeCalledOnServerClosing() throws Exception {
6767
.isNotNull()
6868
.isEqualTo(Client.ShutdownContext.ShutdownReason.SERVER_CLOSE);
6969
assertThat(client.isOpen()).isFalse();
70+
client.close();
7071
}
7172

7273
@Test
@@ -89,5 +90,6 @@ void shutdownListenerShouldBeCalledWhenConnectionIsKilled() throws Exception {
8990
assertThat(shutdownLatch.await(10, TimeUnit.SECONDS)).isTrue();
9091
assertThat(reason.get()).isNotNull().isEqualTo(Client.ShutdownContext.ShutdownReason.UNKNOWN);
9192
assertThat(client.isOpen()).isFalse();
93+
client.close();
9294
}
9395
}

src/test/java/com/rabbitmq/stream/impl/TestUtils.java

+40-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
import java.util.UUID;
5858
import java.util.concurrent.ConcurrentHashMap;
5959
import java.util.concurrent.CountDownLatch;
60+
import java.util.concurrent.ExecutorService;
61+
import java.util.concurrent.Executors;
6062
import java.util.concurrent.TimeUnit;
6163
import java.util.concurrent.atomic.AtomicLong;
6264
import java.util.concurrent.atomic.AtomicReference;
@@ -75,12 +77,17 @@
7577
import org.junit.jupiter.api.extension.ExecutionCondition;
7678
import org.junit.jupiter.api.extension.ExtendWith;
7779
import org.junit.jupiter.api.extension.ExtensionContext;
80+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
81+
import org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource;
7882
import org.mockito.invocation.InvocationOnMock;
7983
import org.mockito.stubbing.Answer;
84+
import org.slf4j.Logger;
8085
import org.slf4j.LoggerFactory;
8186

8287
public final class TestUtils {
8388

89+
private static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class);
90+
8491
private TestUtils() {}
8592

8693
public static Duration waitAtMost(CallableBooleanSupplier condition) throws Exception {
@@ -574,9 +581,39 @@ public void afterEach(ExtensionContext context) throws Exception {
574581
}
575582

576583
@Override
577-
public void afterAll(ExtensionContext context) throws Exception {
584+
public void afterAll(ExtensionContext context) {
578585
EventLoopGroup eventLoopGroup = eventLoopGroup(context);
579-
eventLoopGroup.shutdownGracefully(1, 10, SECONDS).get(10, SECONDS);
586+
ExecutorServiceCloseableResourceWrapper wrapper =
587+
context
588+
.getRoot()
589+
.getStore(Namespace.GLOBAL)
590+
.getOrComputeIfAbsent(ExecutorServiceCloseableResourceWrapper.class);
591+
wrapper.executorService.submit(
592+
() -> {
593+
try {
594+
eventLoopGroup.shutdownGracefully(1, 10, SECONDS).get(10, SECONDS);
595+
} catch (InterruptedException e) {
596+
// happens at the end of the test suite
597+
LOGGER.debug("Error while asynchronously closing Netty event loop group", e);
598+
Thread.currentThread().interrupt();
599+
} catch (Exception e) {
600+
LOGGER.warn("Error while asynchronously closing Netty event loop group", e);
601+
}
602+
});
603+
}
604+
605+
private static class ExecutorServiceCloseableResourceWrapper implements CloseableResource {
606+
607+
private final ExecutorService executorService;
608+
609+
private ExecutorServiceCloseableResourceWrapper() {
610+
this.executorService = Executors.newCachedThreadPool();
611+
}
612+
613+
@Override
614+
public void close() throws Throwable {
615+
this.executorService.shutdownNow();
616+
}
580617
}
581618
}
582619

@@ -700,6 +737,7 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
700737
Client client = new Client(new ClientParameters().eventLoopGroup(eventLoopGroup));
701738
String expectedVersion = annotation.value();
702739
String brokerVersion = client.brokerVersion();
740+
client.close();
703741
if (atLeastVersion(expectedVersion, brokerVersion)) {
704742
return ConditionEvaluationResult.enabled(
705743
"Broker version requirement met, expected "

0 commit comments

Comments
 (0)