Skip to content

Commit

Permalink
chore: refactor code a bit
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed May 28, 2023
1 parent aa3bfcd commit 60ad75a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
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;
Expand Down Expand Up @@ -54,10 +56,6 @@ 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,29 @@
*/
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() {
EventLoop eventLoop = Mockito.mock(EventLoop.class);
DnsNameResolverBuilder dnsNameResolverBuilder = new DnsNameResolverBuilder(eventLoop);
public void testMaxTtl() throws Exception {
DnsNameResolverBuilder dnsNameResolverBuilder = new DnsNameResolverBuilder(Mockito.mock(EventLoop.class));
assertThat(dnsNameResolverBuilder).isNotNull();
DnsResolverUtil.applyJdkDnsCacheSettings(dnsNameResolverBuilder);
// If the maxTtl is <=0, it will throw IllegalArgumentException.
try {

CountDownLatch latch = new CountDownLatch(1);
try (MockedConstruction<?> ignore = Mockito.mockConstruction(
DnsNameResolver.class, (a, b) -> latch.countDown())) {
dnsNameResolverBuilder.build();
} catch (Exception ex) {
Assert.assertFalse(ex instanceof IllegalArgumentException);
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
}
}
}

0 comments on commit 60ad75a

Please sign in to comment.