Skip to content

Commit

Permalink
Revert "chore: refactor code a bit"
Browse files Browse the repository at this point in the history
This reverts commit 60ad75a.
  • Loading branch information
tisonkun committed May 29, 2023
1 parent 6c4f2d8 commit 34077e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 34077e6

Please sign in to comment.