diff --git a/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java b/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java index bc460b8adb027..f49a6453c72b3 100644 --- a/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java +++ b/pulsar-common/src/main/java/org/apache/pulsar/common/util/netty/DnsResolverUtil.java @@ -21,11 +21,9 @@ import io.netty.resolver.dns.DnsNameResolverBuilder; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; @Slf4j -@UtilityClass public class DnsResolverUtil { private static final int MIN_TTL = 0; private static final int TTL; @@ -56,6 +54,10 @@ public class DnsResolverUtil { NEGATIVE_TTL = negativeTtl < 0 ? DEFAULT_NEGATIVE_TTL : negativeTtl; } + private DnsResolverUtil() { + // utility class with static methods, prevent instantiation + } + /** * Configure Netty's {@link DnsNameResolverBuilder}'s ttl and negativeTtl to match the JDK's DNS caching settings. * If the JDK setting for TTL is forever (-1), the TTL will be set to 60 seconds. diff --git a/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java b/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java index 1dc60f918e2c1..0ccb960e79887 100644 --- a/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java +++ b/pulsar-common/src/test/java/org/apache/pulsar/common/util/netty/DnsResolverTest.java @@ -18,29 +18,24 @@ */ package org.apache.pulsar.common.util.netty; -import static org.assertj.core.api.Assertions.assertThat; import io.netty.channel.EventLoop; -import io.netty.resolver.dns.DnsNameResolver; import io.netty.resolver.dns.DnsNameResolverBuilder; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import org.mockito.MockedConstruction; import org.mockito.Mockito; +import org.testng.Assert; import org.testng.annotations.Test; public class DnsResolverTest { @Test - public void testMaxTtl() throws Exception { - DnsNameResolverBuilder dnsNameResolverBuilder = new DnsNameResolverBuilder(Mockito.mock(EventLoop.class)); - assertThat(dnsNameResolverBuilder).isNotNull(); + public void testMaxTtl() { + EventLoop eventLoop = Mockito.mock(EventLoop.class); + DnsNameResolverBuilder dnsNameResolverBuilder = new DnsNameResolverBuilder(eventLoop); DnsResolverUtil.applyJdkDnsCacheSettings(dnsNameResolverBuilder); - - CountDownLatch latch = new CountDownLatch(1); - try (MockedConstruction ignore = Mockito.mockConstruction( - DnsNameResolver.class, (a, b) -> latch.countDown())) { + // If the maxTtl is <=0, it will throw IllegalArgumentException. + try { dnsNameResolverBuilder.build(); - assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue(); + } catch (Exception ex) { + Assert.assertFalse(ex instanceof IllegalArgumentException); } } }