From 26603f2874229e0697850134a436f5f2eb58bb37 Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sat, 14 Mar 2020 23:52:25 +0100 Subject: [PATCH 1/8] Drop jsr305 dependency --- pom.xml | 7 +------ src/main/java/com/spotify/dns/DnsException.java | 8 +++----- .../dns/ServiceResolvingChangeNotifier.java | 16 +++++++--------- .../dns/ServiceResolvingChangeNotifierTest.java | 5 +---- 4 files changed, 12 insertions(+), 24 deletions(-) diff --git a/pom.xml b/pom.xml index 9627e3f..5596c02 100644 --- a/pom.xml +++ b/pom.xml @@ -54,11 +54,6 @@ guava ${guava.version} - - com.google.code.findbugs - jsr305 - 2.0.1 - org.slf4j slf4j-api @@ -289,7 +284,7 @@ com.github.siom79.japicmp japicmp-maven-plugin - 0.6.1 + 0.14.3 diff --git a/src/main/java/com/spotify/dns/DnsException.java b/src/main/java/com/spotify/dns/DnsException.java index d886fc6..e86ef93 100644 --- a/src/main/java/com/spotify/dns/DnsException.java +++ b/src/main/java/com/spotify/dns/DnsException.java @@ -16,8 +16,6 @@ package com.spotify.dns; -import javax.annotation.Nullable; - /** * RuntimeException thrown by the Spotify DNS library. */ @@ -25,15 +23,15 @@ public class DnsException extends RuntimeException { public DnsException() { } - public DnsException(@Nullable String message) { + public DnsException(String message) { super(message); } - public DnsException(@Nullable String message, @Nullable Throwable cause) { + public DnsException(String message, Throwable cause) { super(message, cause); } - public DnsException(@Nullable Throwable cause) { + public DnsException(Throwable cause) { super(cause); } } diff --git a/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java b/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java index 30eb27e..b9578c0 100644 --- a/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java +++ b/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java @@ -16,16 +16,14 @@ package com.spotify.dns; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.annotation.Nullable; import java.util.List; import java.util.Set; - -import static com.google.common.base.Preconditions.checkNotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A {@link ChangeNotifier} that resolves and provides records using a {@link DnsSrvResolver}. @@ -41,9 +39,9 @@ class ServiceResolvingChangeNotifier extends AbstractChangeNotifier private final String fqdn; private final Function resultTransformer; - @Nullable private final ErrorHandler errorHandler; + private volatile Set records = ChangeNotifiers.initialEmptyDataInstance(); private volatile boolean waitingForFirstEvent = true; @@ -62,12 +60,12 @@ class ServiceResolvingChangeNotifier extends AbstractChangeNotifier * @param resolver The resolver to use. * @param fqdn The name to lookup SRV records for * @param resultTransformer The transform function - * @param errorHandler The error handler that will receive exceptions + * @param errorHandler The error handler that will receive exceptions (nullable) */ ServiceResolvingChangeNotifier(final DnsSrvResolver resolver, final String fqdn, final Function resultTransformer, - @Nullable final ErrorHandler errorHandler) { + final ErrorHandler errorHandler) { this.resolver = checkNotNull(resolver); this.fqdn = checkNotNull(fqdn, "fqdn"); diff --git a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java index 94983fd..e28e8cd 100644 --- a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java @@ -27,8 +27,6 @@ import java.util.List; -import javax.annotation.Nullable; - import static com.google.common.collect.ImmutableList.of; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -235,9 +233,8 @@ private ChangeNotifierFactory.RunnableChangeNotifier createNotifie private ChangeNotifierFactory.RunnableChangeNotifier createHostNotifier() { return createTransformingNotifier(new Function() { - @Nullable @Override - public String apply(@Nullable LookupResult input) { + public String apply(LookupResult input) { return input != null ? input.host() : null; } }); From 18c03df0dbf9bb3c1bf3cc9a734e8ec2ddc18348 Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 00:10:43 +0100 Subject: [PATCH 2/8] Upgrade to Java 9 build --- pom.xml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 5596c02..238411d 100644 --- a/pom.xml +++ b/pom.xml @@ -228,10 +228,11 @@ maven-compiler-plugin - 3.1 + 3.8.1 - 1.6 - 1.6 + 9 + 9 + 9 @@ -255,7 +256,7 @@ org.apache.felix maven-bundle-plugin - 3.0.1 + 4.2.1 true From e373b68e43b0716fa6ae76202831a20b9793c00e Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 01:42:17 +0100 Subject: [PATCH 3/8] Add JPMS module declaration --- pom.xml | 20 +++++-- .../spotify/dns/AbstractChangeNotifier.java | 20 +++---- .../dns/AggregatingChangeNotifier.java | 9 +-- .../com/spotify/dns/CachingLookupFactory.java | 24 ++------ .../java/com/spotify/dns/ChangeNotifiers.java | 37 +++++++------ .../com/spotify/dns/DirectChangeNotifier.java | 7 +-- .../java/com/spotify/dns/DnsSrvResolvers.java | 6 +- .../java/com/spotify/dns/DnsSrvWatchers.java | 55 +++++++++---------- .../java/com/spotify/dns/LookupResult.java | 4 +- .../spotify/dns/MeteredDnsSrvResolver.java | 8 +-- .../com/spotify/dns/PollingDnsSrvWatcher.java | 8 +-- .../spotify/dns/RetainingDnsSrvResolver.java | 8 ++- .../dns/ServiceResolvingChangeNotifier.java | 12 ++-- .../com/spotify/dns/XBillDnsSrvResolver.java | 7 ++- src/main/java/module-info.java | 9 +++ .../ServiceResolvingChangeNotifierTest.java | 13 +---- 16 files changed, 121 insertions(+), 126 deletions(-) create mode 100644 src/main/java/module-info.java diff --git a/pom.xml b/pom.xml index 238411d..ecd409f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.spotify dns bundle - 3.1.6-SNAPSHOT + 3.2.0-SNAPSHOT Spotify DNS wrapper library A thin wrapper around dnsjava for some features related to SRV lookups. @@ -12,7 +12,7 @@ UTF-8 - 12.0 + 28.2-jre @@ -47,7 +47,7 @@ dnsjava dnsjava - 2.1.8 + 3.0.1 com.google.guava @@ -57,12 +57,12 @@ org.slf4j slf4j-api - 1.7.5 + 1.7.30 junit junit - 4.11 + 4.12 test @@ -86,7 +86,7 @@ org.slf4j slf4j-simple - 1.7.5 + 1.7.30 test @@ -235,6 +235,14 @@ 9 + + maven-surefire-plugin + 2.22.2 + + + --add-opens com.spotify.dns/com.spotify.dns=ALL-UNNAMED + + org.apache.maven.plugins maven-enforcer-plugin diff --git a/src/main/java/com/spotify/dns/AbstractChangeNotifier.java b/src/main/java/com/spotify/dns/AbstractChangeNotifier.java index 30647e2..0403982 100644 --- a/src/main/java/com/spotify/dns/AbstractChangeNotifier.java +++ b/src/main/java/com/spotify/dns/AbstractChangeNotifier.java @@ -16,6 +16,8 @@ package com.spotify.dns; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.ImmutableSet; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -26,8 +28,6 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.ReentrantLock; -import static com.google.common.base.Preconditions.checkNotNull; - /** * A helper for implementing the {@link ChangeNotifier} interface. */ @@ -35,7 +35,7 @@ abstract class AbstractChangeNotifier implements ChangeNotifier { private static final Logger log = LoggerFactory.getLogger(AbstractChangeNotifier.class); - private final AtomicReference> listenerRef = new AtomicReference>(); + private final AtomicReference> listenerRef = new AtomicReference<>(); private final AtomicBoolean listenerNotified = new AtomicBoolean(false); @@ -43,16 +43,16 @@ abstract class AbstractChangeNotifier implements ChangeNotifier { @Override public void setListener(final Listener listener, final boolean fire) { - checkNotNull(listener, "listener"); + requireNonNull(listener, "listener"); lock.lock(); try { - if (!listenerRef.compareAndSet(null, listener)) { + if (!listenerRef.compareAndSet(null, listener)) { throw new IllegalStateException("Listener already set!"); } if (fire) { - notifyListener(newChangeNotification(current(), Collections.emptySet()), true); + notifyListener(newChangeNotification(current(), Set.of()), true); } } finally { lock.unlock(); @@ -81,7 +81,7 @@ protected final void fireRecordsUpdated(ChangeNotification changeNotification private void notifyListener(ChangeNotification changeNotification, boolean newListener) { lock.lock(); try { - checkNotNull(changeNotification, "changeNotification"); + requireNonNull(changeNotification, "changeNotification"); final Listener listener = listenerRef.get(); if (listener != null) { @@ -100,10 +100,10 @@ private void notifyListener(ChangeNotification changeNotification, boolean ne } protected final ChangeNotification newChangeNotification(Set current, Set previous) { - checkNotNull(current, "current"); - checkNotNull(previous, "previous"); + requireNonNull(current, "current"); + requireNonNull(previous, "previous"); - return new ChangeNotificationImpl(current, previous); + return new ChangeNotificationImpl<>(current, previous); } private static class ChangeNotificationImpl implements ChangeNotification { diff --git a/src/main/java/com/spotify/dns/AggregatingChangeNotifier.java b/src/main/java/com/spotify/dns/AggregatingChangeNotifier.java index d69dd81..3810020 100644 --- a/src/main/java/com/spotify/dns/AggregatingChangeNotifier.java +++ b/src/main/java/com/spotify/dns/AggregatingChangeNotifier.java @@ -29,7 +29,7 @@ class AggregatingChangeNotifier extends AbstractChangeNotifier { private final List> changeNotifiers; - private volatile Set records = ChangeNotifiers.initialEmptyDataInstance(); + private volatile Set records; /** * Create a new aggregating {@link ChangeNotifier}. @@ -41,12 +41,7 @@ class AggregatingChangeNotifier extends AbstractChangeNotifier { // Set up forwarding of listeners for (final ChangeNotifier changeNotifier : this.changeNotifiers) { - changeNotifier.setListener(new Listener() { - @Override - public void onChange(final ChangeNotification ignored) { - checkChange(); - } - }, false); + changeNotifier.setListener(ignored -> checkChange(), false); } records = aggregateSet(); diff --git a/src/main/java/com/spotify/dns/CachingLookupFactory.java b/src/main/java/com/spotify/dns/CachingLookupFactory.java index 56f8e85..98656e1 100644 --- a/src/main/java/com/spotify/dns/CachingLookupFactory.java +++ b/src/main/java/com/spotify/dns/CachingLookupFactory.java @@ -16,15 +16,13 @@ package com.spotify.dns; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.util.concurrent.UncheckedExecutionException; - -import org.xbill.DNS.Lookup; - -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; +import org.xbill.DNS.Lookup; /** * Caches Lookup instances using a per-thread cache; this is so that different threads will never @@ -35,14 +33,9 @@ class CachingLookupFactory implements LookupFactory { private final ThreadLocal> cacheHolder; CachingLookupFactory(LookupFactory delegate) { - this.delegate = Preconditions.checkNotNull(delegate, "delegate"); + this.delegate = requireNonNull(delegate, "delegate"); cacheHolder = - new ThreadLocal>() { - @Override - protected Cache initialValue() { - return CacheBuilder.newBuilder().build(); - } - }; + ThreadLocal.withInitial(() -> CacheBuilder.newBuilder().build()); } @Override @@ -50,12 +43,7 @@ public Lookup forName(final String fqdn) { try { return cacheHolder.get().get( fqdn, - new Callable() { - @Override - public Lookup call() { - return delegate.forName(fqdn); - } - } + () -> delegate.forName(fqdn) ); } catch (ExecutionException e) { throw new DnsException(e); diff --git a/src/main/java/com/spotify/dns/ChangeNotifiers.java b/src/main/java/com/spotify/dns/ChangeNotifiers.java index fedb238..239f442 100644 --- a/src/main/java/com/spotify/dns/ChangeNotifiers.java +++ b/src/main/java/com/spotify/dns/ChangeNotifiers.java @@ -16,17 +16,15 @@ package com.spotify.dns; -import com.google.common.base.Supplier; -import com.google.common.collect.Sets; +import static com.spotify.dns.ChangeNotifierFactory.RunnableChangeNotifier; +import static java.util.Objects.requireNonNull; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.spotify.dns.ChangeNotifierFactory.RunnableChangeNotifier; +import java.util.function.Supplier; public final class ChangeNotifiers { @@ -37,7 +35,7 @@ public final class ChangeNotifiers { * This is needed to distinguishing the initial state of change notifiers from * when they have gotten proper data. */ - private static final Set INITIAL_EMPTY_DATA = Collections.unmodifiableSet(new HashSet()); + private static final Set INITIAL_EMPTY_DATA = Collections.unmodifiableSet(new HashSet<>()); private ChangeNotifiers() { } @@ -77,7 +75,7 @@ public static ChangeNotifier aggregate(ChangeNotifier... notifiers) { } public static ChangeNotifier aggregate(Iterable> notifiers) { - return new AggregatingChangeNotifier(notifiers); + return new AggregatingChangeNotifier<>(notifiers); } /** @@ -93,11 +91,11 @@ public static ChangeNotifier aggregate(Iterable> notifi * @return A notifier with a static set of records */ public static ChangeNotifier staticRecords(T... records) { - return staticRecords(Sets.newHashSet(records)); + return staticRecords(Set.of(records)); } public static ChangeNotifier staticRecords(Set records) { - return new StaticChangeNotifier(records); + return new StaticChangeNotifier<>(records); } /** @@ -115,20 +113,23 @@ public static ChangeNotifier staticRecords(Set records) { * @return A runnable notifier */ public static RunnableChangeNotifier direct(Supplier> recordsSupplier) { - return new DirectChangeNotifier(recordsSupplier); + return new DirectChangeNotifier<>(recordsSupplier); + } + + /** + * @deprecated Use {@link #direct(java.util.function.Supplier)} + */ + @Deprecated(since = "3.1.6") + public static RunnableChangeNotifier direct(com.google.common.base.Supplier> recordsSupplier) { + return new DirectChangeNotifier<>(recordsSupplier); } public static RunnableChangeNotifier direct(AtomicReference> recordsHolder) { - return new DirectChangeNotifier(supplierFromRef(recordsHolder)); + return new DirectChangeNotifier<>(supplierFromRef(recordsHolder)); } private static Supplier> supplierFromRef(final AtomicReference> ref) { - checkNotNull(ref, "ref"); - return new Supplier>() { - @Override - public Set get() { - return ref.get(); - } - }; + requireNonNull(ref, "ref"); + return ref::get; } } diff --git a/src/main/java/com/spotify/dns/DirectChangeNotifier.java b/src/main/java/com/spotify/dns/DirectChangeNotifier.java index 792da42..b675a2b 100644 --- a/src/main/java/com/spotify/dns/DirectChangeNotifier.java +++ b/src/main/java/com/spotify/dns/DirectChangeNotifier.java @@ -16,11 +16,10 @@ package com.spotify.dns; -import com.google.common.base.Supplier; +import static java.util.Objects.requireNonNull; import java.util.Set; - -import static com.google.common.base.Preconditions.checkNotNull; +import java.util.function.Supplier; class DirectChangeNotifier extends AbstractChangeNotifier implements ChangeNotifierFactory.RunnableChangeNotifier { @@ -31,7 +30,7 @@ class DirectChangeNotifier extends AbstractChangeNotifier private volatile boolean run = true; public DirectChangeNotifier(Supplier> recordsSupplier) { - this.recordsSupplier = checkNotNull(recordsSupplier, "recordsSupplier"); + this.recordsSupplier = requireNonNull(recordsSupplier, "recordsSupplier"); } @Override diff --git a/src/main/java/com/spotify/dns/DnsSrvResolvers.java b/src/main/java/com/spotify/dns/DnsSrvResolvers.java index 37d821f..fb50f78 100644 --- a/src/main/java/com/spotify/dns/DnsSrvResolvers.java +++ b/src/main/java/com/spotify/dns/DnsSrvResolvers.java @@ -16,7 +16,7 @@ package com.spotify.dns; -import static com.google.common.primitives.Ints.checkedCast; +import static java.lang.Math.toIntExact; import static java.util.concurrent.TimeUnit.HOURS; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; @@ -86,8 +86,8 @@ public DnsSrvResolver build() { } // Configure the Resolver to use our timeouts. - int timeoutSecs = checkedCast(MILLISECONDS.toSeconds(dnsLookupTimeoutMillis)); - int millisRemainder = checkedCast(dnsLookupTimeoutMillis - SECONDS.toMillis(timeoutSecs)); + int timeoutSecs = toIntExact(MILLISECONDS.toSeconds(dnsLookupTimeoutMillis)); + int millisRemainder = toIntExact(dnsLookupTimeoutMillis - SECONDS.toMillis(timeoutSecs)); resolver.setTimeout(timeoutSecs, millisRemainder); LookupFactory lookupFactory = new SimpleLookupFactory(resolver); diff --git a/src/main/java/com/spotify/dns/DnsSrvWatchers.java b/src/main/java/com/spotify/dns/DnsSrvWatchers.java index c1ec0ba..02f9085 100644 --- a/src/main/java/com/spotify/dns/DnsSrvWatchers.java +++ b/src/main/java/com/spotify/dns/DnsSrvWatchers.java @@ -16,18 +16,17 @@ package com.spotify.dns; -import com.google.common.base.Function; -import com.google.common.base.Functions; import com.google.common.util.concurrent.MoreExecutors; import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.function.Function; import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.requireNonNull; import static java.util.concurrent.TimeUnit.SECONDS; /** @@ -46,9 +45,9 @@ public final class DnsSrvWatchers { * @return a builder for further configuring the watcher */ public static DnsSrvWatcherBuilder newBuilder(DnsSrvResolver resolver) { - checkNotNull(resolver, "resolver"); + requireNonNull(resolver, "resolver"); - return new DnsSrvWatcherBuilder(resolver, Functions.identity()); + return new DnsSrvWatcherBuilder<>(resolver, Function.identity()); } /** @@ -69,10 +68,20 @@ public static DnsSrvWatcherBuilder newBuilder( DnsSrvResolver resolver, Function resultTransformer) { - checkNotNull(resolver, "resolver"); - checkNotNull(resultTransformer, "resultTransformer"); + requireNonNull(resolver, "resolver"); + requireNonNull(resultTransformer, "resultTransformer"); - return new DnsSrvWatcherBuilder(resolver, resultTransformer); + return new DnsSrvWatcherBuilder<>(resolver, resultTransformer); + } + + /** + * @deprecated Use {@link #newBuilder(DnsSrvResolver, java.util.function.Function)} + */ + @Deprecated(since = "3.1.6") + public static DnsSrvWatcherBuilder newBuilder( + DnsSrvResolver resolver, + com.google.common.base.Function resultTransformer) { + return newBuilder(resolver, resultTransformer); } public static final class DnsSrvWatcherBuilder { @@ -118,15 +127,6 @@ private DnsSrvWatcherBuilder( public DnsSrvWatcher build() { checkState(polling ^ dnsSrvWatcherFactory != null, "specify either polling or custom trigger"); - final ChangeNotifierFactory changeNotifierFactory = - new ChangeNotifierFactory() { - @Override - public RunnableChangeNotifier create(String fqdn) { - return new ServiceResolvingChangeNotifier(resolver, fqdn, resultTransformer, - errorHandler); - } - }; - DnsSrvWatcherFactory watcherFactory; if (polling) { final ScheduledExecutorService executor = @@ -137,23 +137,22 @@ public RunnableChangeNotifier create(String fqdn) { 1, new ThreadFactoryBuilder().setNameFormat("dns-lookup-%d").build()), 0, SECONDS); - watcherFactory = new DnsSrvWatcherFactory() { - @Override - public DnsSrvWatcher create(ChangeNotifierFactory changeNotifierFactory) { - return new PollingDnsSrvWatcher(changeNotifierFactory, executor, pollingInterval, - pollingIntervalUnit); - } - }; + watcherFactory = + cnf -> new PollingDnsSrvWatcher<>(cnf, executor, pollingInterval, pollingIntervalUnit); } else { - watcherFactory = checkNotNull(dnsSrvWatcherFactory); + watcherFactory = requireNonNull(dnsSrvWatcherFactory, "dnsSrvWatcherFactory"); } + final ChangeNotifierFactory changeNotifierFactory = + fqdn -> new ServiceResolvingChangeNotifier<>( + resolver, fqdn, resultTransformer, errorHandler); + return watcherFactory.create(changeNotifierFactory); } public DnsSrvWatcherBuilder polling(long pollingInterval, TimeUnit pollingIntervalUnit) { checkArgument(pollingInterval > 0); - checkNotNull(pollingIntervalUnit, "pollingIntervalUnit"); + requireNonNull(pollingIntervalUnit, "pollingIntervalUnit"); return new DnsSrvWatcherBuilder(resolver, resultTransformer, true, pollingInterval, pollingIntervalUnit, errorHandler, dnsSrvWatcherFactory, @@ -167,7 +166,7 @@ public DnsSrvWatcherBuilder usingExecutor(ScheduledExecutorService scheduledE } public DnsSrvWatcherBuilder customTrigger(DnsSrvWatcherFactory watcherFactory) { - checkNotNull(watcherFactory, "watcherFactory"); + requireNonNull(watcherFactory, "watcherFactory"); return new DnsSrvWatcherBuilder(resolver, resultTransformer, true, pollingInterval, pollingIntervalUnit, errorHandler, watcherFactory, @@ -175,7 +174,7 @@ public DnsSrvWatcherBuilder customTrigger(DnsSrvWatcherFactory watcherFact } public DnsSrvWatcherBuilder withErrorHandler(ErrorHandler errorHandler) { - checkNotNull(errorHandler, "errorHandler"); + requireNonNull(errorHandler, "errorHandler"); return new DnsSrvWatcherBuilder(resolver, resultTransformer, true, pollingInterval, pollingIntervalUnit, errorHandler, dnsSrvWatcherFactory, diff --git a/src/main/java/com/spotify/dns/LookupResult.java b/src/main/java/com/spotify/dns/LookupResult.java index 7a34a3f..a67d444 100644 --- a/src/main/java/com/spotify/dns/LookupResult.java +++ b/src/main/java/com/spotify/dns/LookupResult.java @@ -1,6 +1,6 @@ package com.spotify.dns; -import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.Objects.requireNonNull; /** * Immutable data object with the relevant parts of an SRV record. @@ -15,7 +15,7 @@ public class LookupResult { private LookupResult(final String host, final int port, final int priority, final int weight, final long ttl) { - this.host = checkNotNull(host, "host"); + this.host = requireNonNull(host, "host"); this.port = port; this.priority = priority; this.weight = weight; diff --git a/src/main/java/com/spotify/dns/MeteredDnsSrvResolver.java b/src/main/java/com/spotify/dns/MeteredDnsSrvResolver.java index c1a8b92..6fcf6aa 100644 --- a/src/main/java/com/spotify/dns/MeteredDnsSrvResolver.java +++ b/src/main/java/com/spotify/dns/MeteredDnsSrvResolver.java @@ -16,13 +16,13 @@ package com.spotify.dns; +import static java.util.Objects.requireNonNull; + import com.spotify.dns.statistics.DnsReporter; import com.spotify.dns.statistics.DnsTimingContext; import java.util.List; -import static com.google.common.base.Preconditions.checkNotNull; - /** * Tracks metrics for DnsSrvResolver calls. */ @@ -31,8 +31,8 @@ class MeteredDnsSrvResolver implements DnsSrvResolver { private final DnsReporter reporter; MeteredDnsSrvResolver(DnsSrvResolver delegate, DnsReporter reporter) { - this.delegate = checkNotNull(delegate, "delegate"); - this.reporter = checkNotNull(reporter, "reporter"); + this.delegate = requireNonNull(delegate, "delegate"); + this.reporter = requireNonNull(reporter, "reporter"); } @Override diff --git a/src/main/java/com/spotify/dns/PollingDnsSrvWatcher.java b/src/main/java/com/spotify/dns/PollingDnsSrvWatcher.java index 76ad468..81bacaf 100644 --- a/src/main/java/com/spotify/dns/PollingDnsSrvWatcher.java +++ b/src/main/java/com/spotify/dns/PollingDnsSrvWatcher.java @@ -21,8 +21,8 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import static com.google.common.base.Preconditions.checkNotNull; import static com.spotify.dns.ChangeNotifierFactory.RunnableChangeNotifier; +import static java.util.Objects.requireNonNull; class PollingDnsSrvWatcher implements DnsSrvWatcher { @@ -37,10 +37,10 @@ class PollingDnsSrvWatcher implements DnsSrvWatcher { ScheduledExecutorService executor, long pollingInterval, TimeUnit pollingIntervalUnit) { - this.changeNotifierFactory = checkNotNull(changeNotifierFactory, "changeNotifierFactory"); - this.executor = checkNotNull(executor, "executor"); + this.changeNotifierFactory = requireNonNull(changeNotifierFactory, "changeNotifierFactory"); + this.executor = requireNonNull(executor, "executor"); this.pollingInterval = pollingInterval; - this.pollingIntervalUnit = checkNotNull(pollingIntervalUnit, "pollingIntervalUnit"); + this.pollingIntervalUnit = requireNonNull(pollingIntervalUnit, "pollingIntervalUnit"); } @Override diff --git a/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java b/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java index 77e56d6..992bcf1 100644 --- a/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java +++ b/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java @@ -16,6 +16,8 @@ package com.spotify.dns; +import static java.util.Objects.requireNonNull; + import com.google.common.base.Preconditions; import com.google.common.base.Throwables; import com.google.common.cache.Cache; @@ -36,9 +38,9 @@ class RetainingDnsSrvResolver implements DnsSrvResolver { RetainingDnsSrvResolver(DnsSrvResolver delegate, long retentionTimeMillis) { Preconditions.checkArgument(retentionTimeMillis > 0L, - "retention time must be positive, was %d",retentionTimeMillis); + "retention time must be positive, was %d", retentionTimeMillis); - this.delegate = Preconditions.checkNotNull(delegate, "delegate"); + this.delegate = requireNonNull(delegate, "delegate"); cache = CacheBuilder.newBuilder() .expireAfterWrite(retentionTimeMillis, TimeUnit.MILLISECONDS) .build(); @@ -46,7 +48,7 @@ class RetainingDnsSrvResolver implements DnsSrvResolver { @Override public List resolve(final String fqdn) { - Preconditions.checkNotNull(fqdn, "fqdn"); + requireNonNull(fqdn, "fqdn"); try { final List nodes = delegate.resolve(fqdn); diff --git a/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java b/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java index b9578c0..b5fbd57 100644 --- a/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java +++ b/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java @@ -16,12 +16,12 @@ package com.spotify.dns; -import static com.google.common.base.Preconditions.checkNotNull; +import static java.util.Objects.requireNonNull; -import com.google.common.base.Function; import com.google.common.collect.ImmutableSet; import java.util.List; import java.util.Set; +import java.util.function.Function; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,9 +67,9 @@ class ServiceResolvingChangeNotifier extends AbstractChangeNotifier final Function resultTransformer, final ErrorHandler errorHandler) { - this.resolver = checkNotNull(resolver); - this.fqdn = checkNotNull(fqdn, "fqdn"); - this.resultTransformer = checkNotNull(resultTransformer, "resultTransformer"); + this.resolver = requireNonNull(resolver, "resolver"); + this.fqdn = requireNonNull(fqdn, "fqdn"); + this.resultTransformer = requireNonNull(resultTransformer, "resultTransformer"); this.errorHandler = errorHandler; } @@ -110,7 +110,7 @@ public void run() { ImmutableSet.Builder builder = ImmutableSet.builder(); for (LookupResult node : nodes) { T transformed = resultTransformer.apply(node); - builder.add(checkNotNull(transformed, "transformed")); + builder.add(requireNonNull(transformed, "transformed")); } current = builder.build(); } catch (Exception e) { diff --git a/src/main/java/com/spotify/dns/XBillDnsSrvResolver.java b/src/main/java/com/spotify/dns/XBillDnsSrvResolver.java index e0167de..ccc5f83 100644 --- a/src/main/java/com/spotify/dns/XBillDnsSrvResolver.java +++ b/src/main/java/com/spotify/dns/XBillDnsSrvResolver.java @@ -16,7 +16,8 @@ package com.spotify.dns; -import com.google.common.base.Preconditions; +import static java.util.Objects.requireNonNull; + import com.google.common.collect.ImmutableList; import org.slf4j.Logger; @@ -33,11 +34,11 @@ */ class XBillDnsSrvResolver implements DnsSrvResolver { private static final Logger LOG = LoggerFactory.getLogger(XBillDnsSrvResolver.class); - + private final LookupFactory lookupFactory; XBillDnsSrvResolver(LookupFactory lookupFactory) { - this.lookupFactory = Preconditions.checkNotNull(lookupFactory, "lookupFactory"); + this.lookupFactory = requireNonNull(lookupFactory, "lookupFactory"); } @Override diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..8a9eabd --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,9 @@ +module com.spotify.dns { + + requires org.dnsjava; + requires com.google.common; + requires org.slf4j; + + exports com.spotify.dns; + exports com.spotify.dns.statistics; +} diff --git a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java index e28e8cd..2e9f215 100644 --- a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java @@ -16,9 +16,7 @@ package com.spotify.dns; -import com.google.common.base.Function; -import com.google.common.base.Functions; - +import java.util.function.Function; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -228,16 +226,11 @@ public void shouldCallErrorHandlerOnResolveErrors() throws Exception { } private ChangeNotifierFactory.RunnableChangeNotifier createNotifier() { - return createTransformingNotifier(Functions.identity()); + return createTransformingNotifier(Function.identity()); } private ChangeNotifierFactory.RunnableChangeNotifier createHostNotifier() { - return createTransformingNotifier(new Function() { - @Override - public String apply(LookupResult input) { - return input != null ? input.host() : null; - } - }); + return createTransformingNotifier(input -> input != null ? input.host() : null); } private ChangeNotifierFactory.RunnableChangeNotifier createTransformingNotifier( From f51c9afeb4a7c060dc35e11a47eeb5c1f6f73f96 Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 01:45:35 +0100 Subject: [PATCH 4/8] Add @FunctionalInterface annotation on suitable interfaces --- src/main/java/com/spotify/dns/ChangeNotifier.java | 1 + src/main/java/com/spotify/dns/DnsSrvWatcherFactory.java | 1 + src/main/java/com/spotify/dns/statistics/DnsTimingContext.java | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main/java/com/spotify/dns/ChangeNotifier.java b/src/main/java/com/spotify/dns/ChangeNotifier.java index dc32dae..2ae5ec7 100644 --- a/src/main/java/com/spotify/dns/ChangeNotifier.java +++ b/src/main/java/com/spotify/dns/ChangeNotifier.java @@ -59,6 +59,7 @@ public interface ChangeNotifier { /** * A listener which will be called when the set of records change */ + @FunctionalInterface interface Listener { /** diff --git a/src/main/java/com/spotify/dns/DnsSrvWatcherFactory.java b/src/main/java/com/spotify/dns/DnsSrvWatcherFactory.java index 67d1d57..4493b8a 100644 --- a/src/main/java/com/spotify/dns/DnsSrvWatcherFactory.java +++ b/src/main/java/com/spotify/dns/DnsSrvWatcherFactory.java @@ -25,6 +25,7 @@ * * @param The record type */ +@FunctionalInterface public interface DnsSrvWatcherFactory { /** diff --git a/src/main/java/com/spotify/dns/statistics/DnsTimingContext.java b/src/main/java/com/spotify/dns/statistics/DnsTimingContext.java index 2b73980..fb0bc27 100644 --- a/src/main/java/com/spotify/dns/statistics/DnsTimingContext.java +++ b/src/main/java/com/spotify/dns/statistics/DnsTimingContext.java @@ -19,6 +19,7 @@ /** * Implement to handle timings when performing dns requests. */ +@FunctionalInterface public interface DnsTimingContext { void stop(); } From 0bed71c51b84cd6794e35a712e41b0680a97a05b Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 02:08:01 +0100 Subject: [PATCH 5/8] Freshen up tests with java 9 code --- .../dns/AbstractChangeNotifierTest.java | 39 +++++----- .../dns/AggregatingChangeNotifierTest.java | 16 ++--- .../spotify/dns/CachingLookupFactoryTest.java | 27 +++---- .../com/spotify/dns/DnsSrvResolversIT.java | 71 ++++++++----------- .../com/spotify/dns/DnsSrvWatchersTest.java | 15 ++-- .../java/com/spotify/dns/DnsTestUtil.java | 18 ++--- .../dns/MeteredDnsSrvResolverTest.java | 16 ++--- .../dns/RetainingDnsSrvResolverTest.java | 31 ++++---- .../ServiceResolvingChangeNotifierTest.java | 35 +++++---- .../spotify/dns/SimpleLookupFactoryTest.java | 18 ++--- .../spotify/dns/XBillDnsSrvResolverTest.java | 60 +++++++--------- .../com/spotify/dns/examples/BasicUsage.java | 1 - .../spotify/dns/examples/PollingUsage.java | 2 - 13 files changed, 150 insertions(+), 199 deletions(-) diff --git a/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java b/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java index 73e16d4..b8bb0a6 100644 --- a/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java @@ -16,18 +16,6 @@ package com.spotify.dns; -import com.google.common.collect.Sets; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.util.Set; - import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertThat; @@ -37,6 +25,15 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import java.util.Set; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + public class AbstractChangeNotifierTest { @Mock @@ -51,13 +48,13 @@ public class AbstractChangeNotifierTest { AbstractChangeNotifier sut; @Before - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); - sut = new AbstractChangeNotifier() { + sut = new AbstractChangeNotifier<>() { @Override public Set current() { - return Sets.newHashSet("foo", "bar"); + return Set.of("foo", "bar"); } @Override @@ -67,7 +64,7 @@ public void closeImplementation() { } @Test - public void shouldRegisterListener() throws Exception { + public void shouldRegisterListener() { sut.setListener(listener, false); sut.fireRecordsUpdated(changeNotification); @@ -76,7 +73,7 @@ public void shouldRegisterListener() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldNotFireImmediatelyIfFalse() throws Exception { + public void shouldNotFireImmediatelyIfFalse() { sut.setListener(listener, false); verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class)); @@ -84,7 +81,7 @@ public void shouldNotFireImmediatelyIfFalse() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldFireImmediatelyIfTrue() throws Exception { + public void shouldFireImmediatelyIfTrue() { sut.setListener(listener, true); final ArgumentCaptor captor = @@ -99,7 +96,7 @@ public void shouldFireImmediatelyIfTrue() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldNotFireAfterClose() throws Exception { + public void shouldNotFireAfterClose() { sut.setListener(listener, false); sut.close(); sut.fireRecordsUpdated(changeNotification); @@ -109,7 +106,7 @@ public void shouldNotFireAfterClose() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldNotAllowMultipleListeners() throws Exception { + public void shouldNotAllowMultipleListeners() { sut.setListener(listener, false); thrown.expect(IllegalStateException.class); @@ -118,7 +115,7 @@ public void shouldNotAllowMultipleListeners() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldIgnoreListenerExceptions() throws Exception { + public void shouldIgnoreListenerExceptions() { doThrow(new RuntimeException("stupid listener")) .when(listener) .onChange(any(ChangeNotifier.ChangeNotification.class)); diff --git a/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java b/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java index db17b06..1e28902 100644 --- a/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java @@ -15,30 +15,28 @@ */ package com.spotify.dns; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import org.junit.Test; - -import java.util.Set; - import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import java.util.List; +import java.util.Set; +import org.junit.Test; + public class AggregatingChangeNotifierTest { @Test - public void testEmptySet() throws Exception { + public void testEmptySet() { MyNotifier childNotifier = new MyNotifier(); - AggregatingChangeNotifier notifier = new AggregatingChangeNotifier(ImmutableList.>of(childNotifier)); + AggregatingChangeNotifier notifier = new AggregatingChangeNotifier<>(List.of(childNotifier)); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); notifier.setListener(listener, false); verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class)); - childNotifier.set(ImmutableSet.of()); + childNotifier.set(Set.of()); verifyNoMoreInteractions(listener); } diff --git a/src/test/java/com/spotify/dns/CachingLookupFactoryTest.java b/src/test/java/com/spotify/dns/CachingLookupFactoryTest.java index de203e1..478d9fa 100644 --- a/src/test/java/com/spotify/dns/CachingLookupFactoryTest.java +++ b/src/test/java/com/spotify/dns/CachingLookupFactoryTest.java @@ -16,14 +16,6 @@ package com.spotify.dns; -import org.junit.Before; -import org.junit.Test; -import org.xbill.DNS.Lookup; - -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; @@ -31,6 +23,12 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.junit.Before; +import org.junit.Test; +import org.xbill.DNS.Lookup; + public class CachingLookupFactoryTest { CachingLookupFactory factory; @@ -50,14 +48,14 @@ public void setUp() throws Exception { } @Test - public void shouldReturnResultsFromDelegate() throws Exception { + public void shouldReturnResultsFromDelegate() { when(delegate.forName("a name")).thenReturn(lookup); assertThat(factory.forName("a name"), equalTo(lookup)); } @Test - public void shouldCacheResultsForSubsequentQueries() throws Exception { + public void shouldCacheResultsForSubsequentQueries() { when(delegate.forName("hej")).thenReturn(lookup, lookup2); Lookup first = factory.forName("hej"); @@ -67,7 +65,7 @@ public void shouldCacheResultsForSubsequentQueries() throws Exception { } @Test - public void shouldReturnDifferentForDifferentQueries() throws Exception { + public void shouldReturnDifferentForDifferentQueries() { when(delegate.forName("hej")).thenReturn(lookup); when(delegate.forName("hopp")).thenReturn(lookup2); @@ -83,12 +81,7 @@ public void shouldReturnDifferentForDifferentThreads() throws Exception { factory = new CachingLookupFactory(new SimpleLookupFactory()); Lookup first = factory.forName("hej"); - Lookup second = executorService.submit(new Callable() { - @Override - public Lookup call() throws Exception { - return factory.forName("hej"); - } - }).get(); + Lookup second = executorService.submit(() -> factory.forName("hej")).get(); assertThat(second, not(equalTo(first))); } diff --git a/src/test/java/com/spotify/dns/DnsSrvResolversIT.java b/src/test/java/com/spotify/dns/DnsSrvResolversIT.java index b19edfc..aa00966 100644 --- a/src/test/java/com/spotify/dns/DnsSrvResolversIT.java +++ b/src/test/java/com/spotify/dns/DnsSrvResolversIT.java @@ -16,27 +16,9 @@ package com.spotify.dns; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; -import com.jayway.awaitility.Awaitility; -import com.spotify.dns.statistics.DnsReporter; -import com.spotify.dns.statistics.DnsTimingContext; -import java.util.Arrays; -import org.junit.Before; -import org.junit.Test; - -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.TimeUnit; -import org.xbill.DNS.ExtendedResolver; -import org.xbill.DNS.Lookup; -import org.xbill.DNS.SimpleResolver; - import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.isA; @@ -46,6 +28,19 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.jayway.awaitility.Awaitility; +import com.spotify.dns.statistics.DnsReporter; +import com.spotify.dns.statistics.DnsTimingContext; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; +import org.xbill.DNS.SimpleResolver; + /** * Integration tests for the DnsSrvResolversIT class. */ @@ -54,45 +49,37 @@ public class DnsSrvResolversIT { private DnsSrvResolver resolver; @Before - public void setUp() throws Exception { + public void setUp() { resolver = DnsSrvResolvers.newBuilder().build(); } @Test - public void shouldReturnResultsForValidQuery() throws Exception { + public void shouldReturnResultsForValidQuery() { assertThat(resolver.resolve("_spotify-client._tcp.spotify.com").isEmpty(), is(false)); } @Test - public void testCorrectSequenceOfNotifications() throws Exception { + public void testCorrectSequenceOfNotifications() { ChangeNotifier notifier = ChangeNotifiers.aggregate( DnsSrvWatchers.newBuilder(resolver) .polling(100, TimeUnit.MILLISECONDS) .build().watch("_spotify-client._tcp.spotify.com")); - final List changes = Collections.synchronizedList(Lists.newArrayList()); + final List changes = Collections.synchronizedList(new ArrayList<>()); - notifier.setListener(new ChangeNotifier.Listener() { - @Override - public void onChange(ChangeNotifier.ChangeNotification changeNotification) { - Set current = changeNotification.current(); - if (!ChangeNotifiers.isInitialEmptyData(current)) { - changes.add(current.isEmpty() ? "empty" : "data"); - } + notifier.setListener(changeNotification -> { + Set current = changeNotification.current(); + if (!ChangeNotifiers.isInitialEmptyData(current)) { + changes.add(current.isEmpty() ? "empty" : "data"); } }, true); - assertEquals(ImmutableList.of(), changes); - Awaitility.await().atMost(2, TimeUnit.SECONDS).until(new Callable() { - @Override - public Boolean call() throws Exception { - return changes.size() >= 1; - } - }); - assertEquals(ImmutableList.of("data"), changes); + assertThat(changes, Matchers.empty()); + Awaitility.await().atMost(2, TimeUnit.SECONDS).until(() -> changes.size() >= 1); + assertThat(changes, containsInAnyOrder("data")); } @Test - public void shouldTrackMetricsWhenToldTo() throws Exception { + public void shouldTrackMetricsWhenToldTo() { final DnsReporter reporter = mock(DnsReporter.class); final DnsTimingContext timingReporter = mock(DnsTimingContext.class); @@ -108,7 +95,7 @@ public void shouldTrackMetricsWhenToldTo() throws Exception { } @Test - public void shouldFailForBadHostNames() throws Exception { + public void shouldFailForBadHostNames() { try { resolver.resolve("nonexistenthost"); } @@ -122,13 +109,13 @@ public void shouldReturnResultsUsingSpecifiedServers() throws Exception { final String server = new SimpleResolver().getAddress().getHostName(); final DnsSrvResolver resolver = DnsSrvResolvers .newBuilder() - .servers(ImmutableList.of(server)) + .servers(List.of(server)) .build(); assertThat(resolver.resolve("_spotify-client._tcp.spotify.com").isEmpty(), is(false)); } @Test - public void shouldSucceedCreatingRetainingDnsResolver() throws Exception { + public void shouldSucceedCreatingRetainingDnsResolver() { try { resolver = DnsSrvResolvers.newBuilder().retainingDataOnFailures(true).build(); } diff --git a/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java b/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java index 0c1328e..d4a440e 100644 --- a/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java +++ b/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java @@ -1,18 +1,15 @@ package com.spotify.dns; -import com.google.common.collect.ImmutableList; - -import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.is; import java.util.List; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.is; +import org.junit.Test; public class DnsSrvWatchersTest { @@ -28,7 +25,7 @@ private void resolve() throws Exception { final DnsSrvResolver srvResolver = new FakeResolver( "horse.sto3.spotify.net", LookupResult.create("localhost", 1, 0, 0, 0)); - final AtomicReference> hosts = new AtomicReference>(); + final AtomicReference> hosts = new AtomicReference<>(); final CountDownLatch latch = new CountDownLatch(1); final ChangeNotifier.Listener listener = new FakeListener(hosts, latch); @@ -80,7 +77,7 @@ public FakeResolver(String fqdn, LookupResult result) { @Override public List resolve(String fqdn) { if (this.fqdn.equals(fqdn)) { - return ImmutableList.of(result); + return List.of(result); } else { return null; } diff --git a/src/test/java/com/spotify/dns/DnsTestUtil.java b/src/test/java/com/spotify/dns/DnsTestUtil.java index 65b5f7b..9d3c503 100644 --- a/src/test/java/com/spotify/dns/DnsTestUtil.java +++ b/src/test/java/com/spotify/dns/DnsTestUtil.java @@ -16,25 +16,17 @@ package com.spotify.dns; -import com.google.common.base.Function; -import com.google.common.collect.Lists; - -import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Utility functions that are shared between tests. */ public class DnsTestUtil { static List nodes(String... nodeNames) { - return Lists.transform( - Arrays.asList(nodeNames), - new Function() { - @Override - public LookupResult apply(String input) { - return LookupResult.create(input, 8080, 1, 2, 999); - } - } - ); + return Stream.of(nodeNames) + .map(input -> LookupResult.create(input, 8080, 1, 2, 999)) + .collect(Collectors.toList()); } } diff --git a/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java b/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java index 6c887b7..4986201 100644 --- a/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java +++ b/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java @@ -16,15 +16,6 @@ package com.spotify.dns; -import com.spotify.dns.statistics.DnsReporter; -import com.spotify.dns.statistics.DnsTimingContext; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -32,6 +23,13 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.spotify.dns.statistics.DnsReporter; +import com.spotify.dns.statistics.DnsTimingContext; +import java.util.List; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + public class MeteredDnsSrvResolverTest { private static final String FQDN = "nĂ¥nting"; private static final RuntimeException RUNTIME_EXCEPTION = new RuntimeException(); diff --git a/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java b/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java index 8b0cc59..0f0ef59 100644 --- a/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java +++ b/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java @@ -16,13 +16,6 @@ package com.spotify.dns; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.util.List; - import static com.spotify.dns.DnsTestUtil.nodes; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.equalTo; @@ -30,6 +23,12 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.util.List; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + public class RetainingDnsSrvResolverTest { private static final String FQDN = "heythere"; private static final long RETENTION_TIME_MILLIS = 50L; @@ -45,7 +44,7 @@ public class RetainingDnsSrvResolverTest { public ExpectedException thrown = ExpectedException.none(); @Before - public void setUp() throws Exception { + public void setUp() { delegate = mock(DnsSrvResolver.class); resolver = new RetainingDnsSrvResolver(delegate, RETENTION_TIME_MILLIS); @@ -55,14 +54,14 @@ public void setUp() throws Exception { } @Test - public void shouldReturnResultsFromDelegate() throws Exception { + public void shouldReturnResultsFromDelegate() { when(delegate.resolve(FQDN)).thenReturn(nodes1); assertThat(resolver.resolve(FQDN), equalTo(nodes1)); } @Test - public void shouldReturnResultsFromDelegateEachTime() throws Exception { + public void shouldReturnResultsFromDelegateEachTime() { when(delegate.resolve(FQDN)).thenReturn(nodes1).thenReturn(nodes2); resolver.resolve(FQDN); @@ -71,7 +70,7 @@ public void shouldReturnResultsFromDelegateEachTime() throws Exception { } @Test - public void shouldRetainDataIfNewResultEmpty() throws Exception { + public void shouldRetainDataIfNewResultEmpty() { when(delegate.resolve(FQDN)).thenReturn(nodes1).thenReturn(nodes()); resolver.resolve(FQDN); @@ -80,7 +79,7 @@ public void shouldRetainDataIfNewResultEmpty() throws Exception { } @Test - public void shouldRetainDataOnFailure() throws Exception { + public void shouldRetainDataOnFailure() { when(delegate.resolve(FQDN)) .thenReturn(nodes1) .thenThrow(new DnsException("expected")); @@ -91,7 +90,7 @@ public void shouldRetainDataOnFailure() throws Exception { } @Test - public void shouldThrowOnFailureAndNoDataAvailable() throws Exception { + public void shouldThrowOnFailureAndNoDataAvailable() { when(delegate.resolve(FQDN)).thenThrow(new DnsException("expected")); thrown.expect(DnsException.class); @@ -101,14 +100,14 @@ public void shouldThrowOnFailureAndNoDataAvailable() throws Exception { } @Test - public void shouldReturnEmptyOnEmptyAndNoDataAvailable() throws Exception { + public void shouldReturnEmptyOnEmptyAndNoDataAvailable() { when(delegate.resolve(FQDN)).thenReturn(nodes()); assertThat(resolver.resolve(FQDN).isEmpty(), is(true)); } @Test - public void shouldNotStoreEmptyResults() throws Exception { + public void shouldNotStoreEmptyResults() { when(delegate.resolve(FQDN)) .thenReturn(nodes()) .thenThrow(new DnsException("expected")); @@ -153,7 +152,7 @@ public void shouldNotRetainPastEndOfRetentionOnException() throws Exception { } @Test - public void shouldThrowIfRetentionNegative() throws Exception { + public void shouldThrowIfRetentionNegative() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("-4787"); diff --git a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java index 2e9f215..2044c45 100644 --- a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java @@ -16,16 +16,7 @@ package com.spotify.dns; -import java.util.function.Function; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.util.List; - -import static com.google.common.collect.ImmutableList.of; +import static java.util.List.of; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertThat; @@ -38,6 +29,14 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; +import java.util.List; +import java.util.function.Function; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + public class ServiceResolvingChangeNotifierTest { private static final String FQDN = "example.com"; @@ -49,13 +48,13 @@ public class ServiceResolvingChangeNotifierTest { ErrorHandler errorHandler; @Before - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); } @Test @SuppressWarnings("unchecked") - public void shouldCallListenerOnChange() throws Exception { + public void shouldCallListenerOnChange() { ChangeNotifierFactory.RunnableChangeNotifier sut = createNotifier(); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); sut.setListener(listener, false); @@ -89,7 +88,7 @@ public void shouldCallListenerOnChange() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldCallListenerOnSet() throws Exception { + public void shouldCallListenerOnSet() { ChangeNotifierFactory.RunnableChangeNotifier sut = createNotifier(); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); @@ -112,7 +111,7 @@ public void shouldCallListenerOnSet() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldReturnImmutableSets() throws Exception { + public void shouldReturnImmutableSets() { ChangeNotifierFactory.RunnableChangeNotifier sut = createNotifier(); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); @@ -145,7 +144,7 @@ public void shouldReturnImmutableSets() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldOnlyChangeIfTransformedValuesChange() throws Exception { + public void shouldOnlyChangeIfTransformedValuesChange() { ChangeNotifierFactory.RunnableChangeNotifier sut = createHostNotifier(); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); sut.setListener(listener, false); @@ -170,7 +169,7 @@ public void shouldOnlyChangeIfTransformedValuesChange() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldStopResolvingAfterClose() throws Exception { + public void shouldStopResolvingAfterClose() { ChangeNotifierFactory.RunnableChangeNotifier sut = createNotifier(); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); sut.setListener(listener, false); @@ -184,7 +183,7 @@ public void shouldStopResolvingAfterClose() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldDoSomethingWithNulls() throws Exception { + public void shouldDoSomethingWithNulls() { Function f = mock(Function.class); ChangeNotifierFactory.RunnableChangeNotifier sut = createTransformingNotifier(f); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); @@ -207,7 +206,7 @@ public void shouldDoSomethingWithNulls() throws Exception { @Test @SuppressWarnings("unchecked") - public void shouldCallErrorHandlerOnResolveErrors() throws Exception { + public void shouldCallErrorHandlerOnResolveErrors() { Function f = mock(Function.class); ChangeNotifierFactory.RunnableChangeNotifier sut = createTransformingNotifier(f); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); diff --git a/src/test/java/com/spotify/dns/SimpleLookupFactoryTest.java b/src/test/java/com/spotify/dns/SimpleLookupFactoryTest.java index ab7d58f..1630e3a 100644 --- a/src/test/java/com/spotify/dns/SimpleLookupFactoryTest.java +++ b/src/test/java/com/spotify/dns/SimpleLookupFactoryTest.java @@ -16,6 +16,11 @@ package com.spotify.dns; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -23,11 +28,6 @@ import org.xbill.DNS.Lookup; import org.xbill.DNS.TextParseException; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.isA; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; - public class SimpleLookupFactoryTest { SimpleLookupFactory factory; @@ -36,17 +36,17 @@ public class SimpleLookupFactoryTest { public ExpectedException thrown = ExpectedException.none(); @Before - public void setUp() throws Exception { + public void setUp() { factory = new SimpleLookupFactory(); } @Test - public void shouldCreateLookups() throws Exception { + public void shouldCreateLookups() { assertThat(factory.forName("some.domain."), is(notNullValue())); } @Test - public void shouldCreateNewLookupsEachTime() throws Exception { + public void shouldCreateNewLookupsEachTime() { Lookup first = factory.forName("some.other.name."); Lookup second = factory.forName("some.other.name."); @@ -54,7 +54,7 @@ public void shouldCreateNewLookupsEachTime() throws Exception { } @Test - public void shouldRethrowXBillExceptions() throws Exception { + public void shouldRethrowXBillExceptions() { thrown.expect(DnsException.class); thrown.expectCause(isA(TextParseException.class)); diff --git a/src/test/java/com/spotify/dns/XBillDnsSrvResolverTest.java b/src/test/java/com/spotify/dns/XBillDnsSrvResolverTest.java index 75aa1a3..ad17540 100644 --- a/src/test/java/com/spotify/dns/XBillDnsSrvResolverTest.java +++ b/src/test/java/com/spotify/dns/XBillDnsSrvResolverTest.java @@ -16,9 +16,17 @@ package com.spotify.dns; -import com.google.common.base.Function; -import com.google.common.collect.Lists; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -36,29 +44,16 @@ import org.xbill.DNS.TextParseException; import org.xbill.DNS.Type; -import java.io.IOException; -import java.util.HashSet; -import java.util.List; - -import static java.util.Arrays.asList; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - public class XBillDnsSrvResolverTest { XBillDnsSrvResolver resolver; LookupFactory lookupFactory; Resolver xbillResolver; - @Rule - public ExpectedException thrown = ExpectedException.none(); + @Rule public ExpectedException thrown = ExpectedException.none(); @Before - public void setUp() throws Exception { + public void setUp() { lookupFactory = mock(LookupFactory.class); resolver = new XBillDnsSrvResolver(lookupFactory); @@ -67,27 +62,22 @@ public void setUp() throws Exception { } @After - public void tearDown() throws Exception { + public void tearDown() { Lookup.refreshDefault(); } @Test public void shouldReturnResultsFromLookup() throws Exception { String fqdn = "thefqdn."; - List resultNodes = asList("node1.domain.", "node2.domain."); + String[] resultNodes = new String[] { "node1.domain.", "node2.domain." }; setupResponseForQuery(fqdn, fqdn, resultNodes); List actual = resolver.resolve(fqdn); - HashSet nodeNames = new HashSet(Lists.transform(actual, new Function() { - @Override - public String apply(LookupResult input) { - return input.host(); - } - })); + Set nodeNames = actual.stream().map(LookupResult::host).collect(Collectors.toSet()); - assertThat(nodeNames, equalTo(new HashSet(resultNodes))); + assertThat(nodeNames, containsInAnyOrder(resultNodes)); } @Test @@ -96,7 +86,7 @@ public void shouldIndicateCauseFromXBillIfLookupFails() throws Exception { thrown.expectMessage("response does not match query"); String fqdn = "thefqdn."; - setupResponseForQuery(fqdn, "somethingelse.", asList("node1.domain.", "node2.domain.")); + setupResponseForQuery(fqdn, "somethingelse.", "node1.domain.", "node2.domain."); resolver.resolve(fqdn); } @@ -107,7 +97,7 @@ public void shouldIndicateNameIfLookupFails() throws Exception { thrown.expectMessage("thefqdn."); String fqdn = "thefqdn."; - setupResponseForQuery(fqdn, "somethingelse.", asList("node1.domain.", "node2.domain.")); + setupResponseForQuery(fqdn, "somethingelse.", "node1.domain.", "node2.domain."); resolver.resolve(fqdn); } @@ -137,9 +127,11 @@ private Message messageWithRCode(String query, int rcode) throws TextParseExcept return result; } - private void setupResponseForQuery(String queryFqdn, String responseFqdn, List results) throws IOException { + private void setupResponseForQuery(String queryFqdn, String responseFqdn, String... results) + throws IOException { when(lookupFactory.forName(queryFqdn)).thenReturn(testLookup(queryFqdn)); - when(xbillResolver.send(any(Message.class))).thenReturn(messageWithNodes(responseFqdn, results)); + when(xbillResolver.send(any(Message.class))) + .thenReturn(messageWithNodes(responseFqdn, results)); } private Lookup testLookup(String thefqdn) throws TextParseException { @@ -150,7 +142,7 @@ private Lookup testLookup(String thefqdn) throws TextParseException { return result; } - private Message messageWithNodes(String query, Iterable names) throws TextParseException { + private Message messageWithNodes(String query, String[] names) throws TextParseException { Name queryName = Name.fromString(query); Record question = Record.newRecord(queryName, Type.SRV, DClass.IN); Message queryMessage = Message.newQuery(question); @@ -158,8 +150,10 @@ private Message messageWithNodes(String query, Iterable names) throws Te result.setHeader(queryMessage.getHeader()); result.addRecord(question, Section.QUESTION); - for (String name1 : names){ - result.addRecord(new SRVRecord(queryName, DClass.IN, 1, 1, 1, 8080, Name.fromString(name1)), Section.ANSWER); + for (String name1 : names) { + result.addRecord( + new SRVRecord(queryName, DClass.IN, 1, 1, 1, 8080, Name.fromString(name1)), + Section.ANSWER); } return result; diff --git a/src/test/java/com/spotify/dns/examples/BasicUsage.java b/src/test/java/com/spotify/dns/examples/BasicUsage.java index 5ff4cff..07636cc 100644 --- a/src/test/java/com/spotify/dns/examples/BasicUsage.java +++ b/src/test/java/com/spotify/dns/examples/BasicUsage.java @@ -22,7 +22,6 @@ import com.spotify.dns.LookupResult; import com.spotify.dns.statistics.DnsReporter; import com.spotify.dns.statistics.DnsTimingContext; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; diff --git a/src/test/java/com/spotify/dns/examples/PollingUsage.java b/src/test/java/com/spotify/dns/examples/PollingUsage.java index 93175f2..f6c917b 100644 --- a/src/test/java/com/spotify/dns/examples/PollingUsage.java +++ b/src/test/java/com/spotify/dns/examples/PollingUsage.java @@ -17,7 +17,6 @@ package com.spotify.dns.examples; import com.google.common.collect.Sets; - import com.spotify.dns.ChangeNotifier; import com.spotify.dns.DnsException; import com.spotify.dns.DnsSrvResolver; @@ -26,7 +25,6 @@ import com.spotify.dns.DnsSrvWatchers; import com.spotify.dns.ErrorHandler; import com.spotify.dns.LookupResult; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; From 3db3e292cb5eb8eda714d1777d8d75c9b3ac1455 Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 02:27:06 +0100 Subject: [PATCH 6/8] Bump deprecation version --- src/main/java/com/spotify/dns/ChangeNotifiers.java | 2 +- src/main/java/com/spotify/dns/DnsSrvWatchers.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/spotify/dns/ChangeNotifiers.java b/src/main/java/com/spotify/dns/ChangeNotifiers.java index 239f442..f17cd68 100644 --- a/src/main/java/com/spotify/dns/ChangeNotifiers.java +++ b/src/main/java/com/spotify/dns/ChangeNotifiers.java @@ -119,7 +119,7 @@ public static RunnableChangeNotifier direct(Supplier> recordsSuppl /** * @deprecated Use {@link #direct(java.util.function.Supplier)} */ - @Deprecated(since = "3.1.6") + @Deprecated(since = "3.2.0") public static RunnableChangeNotifier direct(com.google.common.base.Supplier> recordsSupplier) { return new DirectChangeNotifier<>(recordsSupplier); } diff --git a/src/main/java/com/spotify/dns/DnsSrvWatchers.java b/src/main/java/com/spotify/dns/DnsSrvWatchers.java index 02f9085..4616513 100644 --- a/src/main/java/com/spotify/dns/DnsSrvWatchers.java +++ b/src/main/java/com/spotify/dns/DnsSrvWatchers.java @@ -77,7 +77,7 @@ public static DnsSrvWatcherBuilder newBuilder( /** * @deprecated Use {@link #newBuilder(DnsSrvResolver, java.util.function.Function)} */ - @Deprecated(since = "3.1.6") + @Deprecated(since = "3.2.0") public static DnsSrvWatcherBuilder newBuilder( DnsSrvResolver resolver, com.google.common.base.Function resultTransformer) { From f857e8e6393cba0e051127994b2dfd945a2d4cf6 Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 20:31:52 +0100 Subject: [PATCH 7/8] Remove empty line --- .../java/com/spotify/dns/ServiceResolvingChangeNotifier.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java b/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java index b5fbd57..a91d4a4 100644 --- a/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java +++ b/src/main/java/com/spotify/dns/ServiceResolvingChangeNotifier.java @@ -41,7 +41,6 @@ class ServiceResolvingChangeNotifier extends AbstractChangeNotifier private final ErrorHandler errorHandler; - private volatile Set records = ChangeNotifiers.initialEmptyDataInstance(); private volatile boolean waitingForFirstEvent = true; From 4f0df1f9e47723585b898d46e9bdb3e91979e24e Mon Sep 17 00:00:00 2001 From: Rouzbeh Delavari Date: Sun, 15 Mar 2020 22:00:09 +0100 Subject: [PATCH 8/8] Remove use of deprecated apis --- src/main/java/com/spotify/dns/DnsSrvResolvers.java | 8 +++----- .../java/com/spotify/dns/RetainingDnsSrvResolver.java | 6 +++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/spotify/dns/DnsSrvResolvers.java b/src/main/java/com/spotify/dns/DnsSrvResolvers.java index fb50f78..c23f77a 100644 --- a/src/main/java/com/spotify/dns/DnsSrvResolvers.java +++ b/src/main/java/com/spotify/dns/DnsSrvResolvers.java @@ -16,13 +16,12 @@ package com.spotify.dns; -import static java.lang.Math.toIntExact; import static java.util.concurrent.TimeUnit.HOURS; -import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import com.spotify.dns.statistics.DnsReporter; import java.net.UnknownHostException; +import java.time.Duration; import java.util.List; import org.xbill.DNS.ExtendedResolver; import org.xbill.DNS.Resolver; @@ -86,9 +85,8 @@ public DnsSrvResolver build() { } // Configure the Resolver to use our timeouts. - int timeoutSecs = toIntExact(MILLISECONDS.toSeconds(dnsLookupTimeoutMillis)); - int millisRemainder = toIntExact(dnsLookupTimeoutMillis - SECONDS.toMillis(timeoutSecs)); - resolver.setTimeout(timeoutSecs, millisRemainder); + final Duration timeoutDuration = Duration.ofMillis(dnsLookupTimeoutMillis); + resolver.setTimeout(timeoutDuration); LookupFactory lookupFactory = new SimpleLookupFactory(resolver); diff --git a/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java b/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java index 992bcf1..653f3aa 100644 --- a/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java +++ b/src/main/java/com/spotify/dns/RetainingDnsSrvResolver.java @@ -16,13 +16,12 @@ package com.spotify.dns; +import static com.google.common.base.Throwables.throwIfUnchecked; import static java.util.Objects.requireNonNull; import com.google.common.base.Preconditions; -import com.google.common.base.Throwables; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; - import java.util.List; import java.util.concurrent.TimeUnit; @@ -67,7 +66,8 @@ public List resolve(final String fqdn) { return cache.getIfPresent(fqdn); } - throw Throwables.propagate(e); + throwIfUnchecked(e); + throw new RuntimeException(e); } } }