From 663f48694b404a3bd37b8ac425814c170bc3ea8d Mon Sep 17 00:00:00 2001 From: Sophy Cao Date: Tue, 21 Sep 2021 11:31:45 +0200 Subject: [PATCH 1/2] Make tests compatible with jdk 8 --- pom.xml | 2 ++ .../dns/AbstractChangeNotifierTest.java | 6 ++-- .../dns/AggregatingChangeNotifierTest.java | 7 +++-- .../spotify/dns/DnsLookupPerformanceTest.java | 3 +- .../com/spotify/dns/DnsSrvResolversIT.java | 3 +- .../com/spotify/dns/DnsSrvWatchersTest.java | 7 +++-- .../java/com/spotify/dns/DnsTestUtil.java | 19 ++++++++++++ .../dns/MeteredDnsSrvResolverTest.java | 6 ++-- .../dns/RetainingDnsSrvResolverTest.java | 8 ++--- .../ServiceResolvingChangeNotifierTest.java | 30 +++++++++++-------- 10 files changed, 63 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 0a42d59..a8552c3 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,8 @@ UTF-8 28.2-jre + 8 + 8 diff --git a/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java b/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java index b8bb0a6..8ffd357 100644 --- a/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java @@ -26,6 +26,8 @@ import static org.mockito.Mockito.verify; import java.util.Set; + +import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -51,10 +53,10 @@ public class AbstractChangeNotifierTest { public void setUp() { MockitoAnnotations.initMocks(this); - sut = new AbstractChangeNotifier<>() { + sut = new AbstractChangeNotifier() { @Override public Set current() { - return Set.of("foo", "bar"); + return Sets.newHashSet("foo", "bar"); } @Override diff --git a/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java b/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java index 1e28902..5f58553 100644 --- a/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/AggregatingChangeNotifierTest.java @@ -21,22 +21,25 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; +import java.util.Arrays; import java.util.List; import java.util.Set; + +import com.google.common.collect.Sets; import org.junit.Test; public class AggregatingChangeNotifierTest { @Test public void testEmptySet() { MyNotifier childNotifier = new MyNotifier(); - AggregatingChangeNotifier notifier = new AggregatingChangeNotifier<>(List.of(childNotifier)); + AggregatingChangeNotifier notifier = new AggregatingChangeNotifier<>(Arrays.asList(childNotifier)); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); notifier.setListener(listener, false); verify(listener, never()).onChange(any(ChangeNotifier.ChangeNotification.class)); - childNotifier.set(Set.of()); + childNotifier.set(Sets.newHashSet()); verifyNoMoreInteractions(listener); } diff --git a/src/test/java/com/spotify/dns/DnsLookupPerformanceTest.java b/src/test/java/com/spotify/dns/DnsLookupPerformanceTest.java index fad3c90..06cadc2 100644 --- a/src/test/java/com/spotify/dns/DnsLookupPerformanceTest.java +++ b/src/test/java/com/spotify/dns/DnsLookupPerformanceTest.java @@ -3,6 +3,7 @@ import org.junit.Ignore; import org.junit.Test; +import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; @@ -29,7 +30,7 @@ public class DnsLookupPerformanceTest { public void runTest() throws InterruptedException { int numThreads = 3; final ExecutorService executorService = Executors.newFixedThreadPool(numThreads); - List records = List.of( + List records = Arrays.asList( "_spotify-noop._http.services.gew1.spotify.net.", "_spotify-noop._http.services.guc3.spotify.net.", "_spotify-noop._http.services.gae2.spotify.net.", diff --git a/src/test/java/com/spotify/dns/DnsSrvResolversIT.java b/src/test/java/com/spotify/dns/DnsSrvResolversIT.java index 919db52..e40d23c 100644 --- a/src/test/java/com/spotify/dns/DnsSrvResolversIT.java +++ b/src/test/java/com/spotify/dns/DnsSrvResolversIT.java @@ -32,6 +32,7 @@ import com.spotify.dns.statistics.DnsReporter; import com.spotify.dns.statistics.DnsTimingContext; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Set; @@ -121,7 +122,7 @@ public void shouldReturnResultsUsingSpecifiedServers() throws Exception { final String server = new SimpleResolver().getAddress().getHostName(); final DnsSrvResolver resolver = DnsSrvResolvers .newBuilder() - .servers(List.of(server)) + .servers(Arrays.asList(server)) .build(); assertThat(resolver.resolve("_spotify-client._tcp.spotify.com").isEmpty(), is(false)); assertThat(resolver.resolveAsync("_spotify-client._tcp.spotify.com").toCompletableFuture().get().isEmpty(), is(false)); diff --git a/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java b/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java index 5965e60..211d759 100644 --- a/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java +++ b/src/test/java/com/spotify/dns/DnsSrvWatchersTest.java @@ -4,6 +4,7 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; +import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.concurrent.CompletableFuture; @@ -80,7 +81,7 @@ public FakeResolver(String fqdn, LookupResult result) { @Override public List resolve(String fqdn) { if (this.fqdn.equals(fqdn)) { - return List.of(result); + return Arrays.asList(result); } else { return null; } @@ -89,9 +90,9 @@ public List resolve(String fqdn) { @Override public CompletionStage> resolveAsync(String fqdn) { if (this.fqdn.equals(fqdn)) { - return CompletableFuture.completedFuture(List.of(result)); + return CompletableFuture.completedFuture(Arrays.asList(result)); } else { - return CompletableFuture.failedFuture(new DnsException(this.fqdn + " != " + fqdn)); + return DnsTestUtil.failedFuture(new DnsException(this.fqdn + " != " + fqdn)); } } } diff --git a/src/test/java/com/spotify/dns/DnsTestUtil.java b/src/test/java/com/spotify/dns/DnsTestUtil.java index 9d3c503..d56cce7 100644 --- a/src/test/java/com/spotify/dns/DnsTestUtil.java +++ b/src/test/java/com/spotify/dns/DnsTestUtil.java @@ -17,6 +17,7 @@ package com.spotify.dns; import java.util.List; +import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -29,4 +30,22 @@ static List nodes(String... nodeNames) { .map(input -> LookupResult.create(input, 8080, 1, 2, 999)) .collect(Collectors.toList()); } + + /** + * method to replace CompletableFuture.failedFuture() from Java 9 in Java 8 + */ + static CompletableFuture> failedFuture(Exception ex) { + CompletableFuture> future = new CompletableFuture<>(); + future.completeExceptionally(ex); + return future; + } + + /** + * method to replace CompletableFuture.failedFuture() from Java 9 in Java 8 + */ + static CompletableFuture> failedFuture(Error error) { + CompletableFuture> future = new CompletableFuture<>(); + future.completeExceptionally(error); + return future; + } } diff --git a/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java b/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java index acc582e..09e1c8e 100644 --- a/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java +++ b/src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java @@ -131,7 +131,9 @@ public void shouldReportRuntimeException() throws Exception { @Test public void shouldReportRuntimeExceptionAsync() throws Exception { - when(delegate.resolveAsync(FQDN)).thenReturn(CompletableFuture.failedFuture((RUNTIME_EXCEPTION))); + CompletableFuture> future = new CompletableFuture<>(); + future.completeExceptionally(RUNTIME_EXCEPTION); + when(delegate.resolveAsync(FQDN)).thenReturn(future); try { resolver.resolveAsync(FQDN).toCompletableFuture().get(); @@ -161,7 +163,7 @@ public void shouldNotReportError() throws Exception { @Test public void shouldNotReportErrorAsync() throws Exception { - when(delegate.resolveAsync(FQDN)).thenReturn(CompletableFuture.failedFuture(ERROR)); + when(delegate.resolveAsync(FQDN)).thenReturn(DnsTestUtil.failedFuture(ERROR)); try { resolver.resolveAsync(FQDN).toCompletableFuture().get(); diff --git a/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java b/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java index fb245ab..fed3283 100644 --- a/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java +++ b/src/test/java/com/spotify/dns/RetainingDnsSrvResolverTest.java @@ -125,7 +125,7 @@ public void shouldRetainDataOnFailure() { public void shouldRetainDataOnFailureAsync() throws ExecutionException, InterruptedException { when(delegate.resolveAsync(FQDN)) .thenReturn(CompletableFuture.completedFuture(nodes1)) - .thenReturn(CompletableFuture.failedFuture(new DnsException("expected"))); + .thenReturn(DnsTestUtil.failedFuture(new DnsException("expected"))); resolver.resolveAsync(FQDN).toCompletableFuture().get(); @@ -145,7 +145,7 @@ public void shouldThrowOnFailureAndNoDataAvailable() { @Test public void shouldThrowOnFailureAndNoDataAvailableAsync() throws ExecutionException, InterruptedException { DnsException cause = new DnsException("expected"); - when(delegate.resolveAsync(FQDN)).thenReturn(CompletableFuture.failedFuture(cause)); + when(delegate.resolveAsync(FQDN)).thenReturn(DnsTestUtil.failedFuture(cause)); thrown.expect(ExecutionException.class); thrown.expectCause(is(cause)); @@ -186,7 +186,7 @@ public void shouldNotStoreEmptyResultsAsync() throws ExecutionException, Interru DnsException cause = new DnsException("expected"); when(delegate.resolveAsync(FQDN)) .thenReturn(CompletableFuture.completedFuture(nodes())) - .thenReturn(CompletableFuture.failedFuture(cause)); + .thenReturn(DnsTestUtil.failedFuture(cause)); resolver.resolveAsync(FQDN).toCompletableFuture().get(); @@ -246,7 +246,7 @@ public void shouldNotRetainPastEndOfRetentionOnExceptionAsync() throws Exception DnsException expected = new DnsException("expected"); when(delegate.resolveAsync(FQDN)) .thenReturn(CompletableFuture.completedFuture(nodes("aresult"))) - .thenReturn(CompletableFuture.failedFuture(expected)); + .thenReturn(DnsTestUtil.failedFuture(expected)); resolver.resolveAsync(FQDN).toCompletableFuture().get(); diff --git a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java index 9e01fc7..91b9107 100644 --- a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java @@ -16,7 +16,6 @@ package com.spotify.dns; -import static java.util.List.of; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.junit.Assert.assertThat; @@ -29,8 +28,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; +import java.util.Arrays; import java.util.List; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; import java.util.function.Function; import org.junit.Before; import org.junit.Test; @@ -62,9 +63,10 @@ public void shouldCallListenerOnChange() { LookupResult result1 = result("host", 1234); LookupResult result2 = result("host", 4321); - when(resolver.resolve(FQDN)).thenReturn(of(result1), of(result1, result2)); + when(resolver.resolve(FQDN)).thenReturn(Arrays.asList(result1), Arrays.asList(result1, result2)); when(resolver.resolveAsync(FQDN)) - .thenReturn(CompletableFuture.completedFuture(of(result1)), CompletableFuture.completedFuture(of(result1, result2))); + .thenReturn(CompletableFuture.completedFuture(Arrays.asList(result1)), + CompletableFuture.completedFuture(Arrays.asList(result1, result2))); sut.run(); sut.run(); @@ -96,9 +98,9 @@ public void shouldCallListenerOnSet() { LookupResult result = result("host", 1234); when(resolver.resolve(FQDN)) - .thenReturn(of(result)); + .thenReturn(Arrays.asList(result)); when(resolver.resolveAsync(FQDN)) - .thenReturn(CompletableFuture.completedFuture(of(result))); + .thenReturn(CompletableFuture.completedFuture(Arrays.asList(result))); sut.run(); sut.setListener(listener, true); @@ -122,9 +124,10 @@ public void shouldReturnImmutableSets() { LookupResult result1 = result("host", 1234); LookupResult result2 = result("host", 4321); when(resolver.resolve(FQDN)) - .thenReturn(of(result1), of(result1, result2)); + .thenReturn(Arrays.asList(result1), Arrays.asList(result1, result2)); when(resolver.resolveAsync(FQDN)) - .thenReturn(CompletableFuture.completedFuture(of(result1)), CompletableFuture.completedFuture(of(result1, result2))); + .thenReturn(CompletableFuture.completedFuture(Arrays.asList(result1)), + CompletableFuture.completedFuture(Arrays.asList(result1, result2))); sut.run(); sut.setListener(listener, true); @@ -158,9 +161,10 @@ public void shouldOnlyChangeIfTransformedValuesChange() { LookupResult result1 = result("host", 1234); LookupResult result2 = result("host", 4321); when(resolver.resolve(FQDN)) - .thenReturn(of(result1), of(result1, result2)); + .thenReturn(Arrays.asList(result1), Arrays.asList(result1, result2)); when(resolver.resolveAsync(FQDN)) - .thenReturn(CompletableFuture.completedFuture(of(result1)), CompletableFuture.completedFuture(of(result1, result2))); + .thenReturn(CompletableFuture.completedFuture(Arrays.asList(result1)), + CompletableFuture.completedFuture(Arrays.asList(result1, result2))); sut.run(); sut.run(); @@ -197,12 +201,12 @@ public void shouldDoSomethingWithNulls() { ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); when(resolver.resolve(FQDN)) - .thenReturn(of( + .thenReturn(Arrays.asList( result("host1", 1234), result("host2", 1234), result("host3", 1234))); when(resolver.resolveAsync(FQDN)) - .thenReturn(CompletableFuture.completedFuture(of( + .thenReturn(CompletableFuture.completedFuture(Arrays.asList( result("host1", 1234), result("host2", 1234), result("host3", 1234)))); @@ -219,7 +223,7 @@ public void shouldDoSomethingWithNulls() { @Test @SuppressWarnings("unchecked") - public void shouldCallErrorHandlerOnResolveErrors() { + public void shouldCallErrorHandlerOnResolveErrors() throws ExecutionException, InterruptedException { Function f = mock(Function.class); ChangeNotifierFactory.RunnableChangeNotifier sut = createTransformingNotifier(f); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class); @@ -228,7 +232,7 @@ public void shouldCallErrorHandlerOnResolveErrors() { when(resolver.resolve(FQDN)) .thenThrow(exception); when(resolver.resolveAsync(FQDN)) - .thenReturn(CompletableFuture.failedFuture(exception)); + .thenReturn(DnsTestUtil.failedFuture(exception)); sut.setListener(listener, false); sut.run(); From 892aed2354ccc17c9df898c31c40f867e1e9538c Mon Sep 17 00:00:00 2001 From: Sophy Cao Date: Tue, 21 Sep 2021 11:57:04 +0200 Subject: [PATCH 2/2] Remove unnecessary exceptions --- .../com/spotify/dns/ServiceResolvingChangeNotifierTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java index 91b9107..ebb06ea 100644 --- a/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java +++ b/src/test/java/com/spotify/dns/ServiceResolvingChangeNotifierTest.java @@ -223,7 +223,7 @@ public void shouldDoSomethingWithNulls() { @Test @SuppressWarnings("unchecked") - public void shouldCallErrorHandlerOnResolveErrors() throws ExecutionException, InterruptedException { + public void shouldCallErrorHandlerOnResolveErrors() { Function f = mock(Function.class); ChangeNotifierFactory.RunnableChangeNotifier sut = createTransformingNotifier(f); ChangeNotifier.Listener listener = mock(ChangeNotifier.Listener.class);