Skip to content
This repository was archived by the owner on Aug 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<guava.version>28.2-jre</guava.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<scm>
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/spotify/dns/AbstractChangeNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,10 +53,10 @@ public class AbstractChangeNotifierTest {
public void setUp() {
MockitoAnnotations.initMocks(this);

sut = new AbstractChangeNotifier<>() {
sut = new AbstractChangeNotifier<String>() {
@Override
public Set<String> current() {
return Set.of("foo", "bar");
return Sets.newHashSet("foo", "bar");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> notifier = new AggregatingChangeNotifier<>(List.of(childNotifier));
AggregatingChangeNotifier<String> 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);

}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/spotify/dns/DnsLookupPerformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +30,7 @@ public class DnsLookupPerformanceTest {
public void runTest() throws InterruptedException {
int numThreads = 3;
final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
List<String> records = List.of(
List<String> records = Arrays.asList(
"_spotify-noop._http.services.gew1.spotify.net.",
"_spotify-noop._http.services.guc3.spotify.net.",
"_spotify-noop._http.services.gae2.spotify.net.",
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/spotify/dns/DnsSrvResolversIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/com/spotify/dns/DnsSrvWatchersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +81,7 @@ public FakeResolver(String fqdn, LookupResult result) {
@Override
public List<LookupResult> resolve(String fqdn) {
if (this.fqdn.equals(fqdn)) {
return List.of(result);
return Arrays.asList(result);
} else {
return null;
}
Expand All @@ -89,9 +90,9 @@ public List<LookupResult> resolve(String fqdn) {
@Override
public CompletionStage<List<LookupResult>> 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));
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/com/spotify/dns/DnsTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,4 +30,22 @@ static List<LookupResult> 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<List<LookupResult>> failedFuture(Exception ex) {
CompletableFuture<List<LookupResult>> future = new CompletableFuture<>();
future.completeExceptionally(ex);
return future;
}

/**
* method to replace CompletableFuture.failedFuture() from Java 9 in Java 8
*/
static CompletableFuture<List<LookupResult>> failedFuture(Error error) {
CompletableFuture<List<LookupResult>> future = new CompletableFuture<>();
future.completeExceptionally(error);
return future;
}
}
6 changes: 4 additions & 2 deletions src/test/java/com/spotify/dns/MeteredDnsSrvResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<LookupResult>> future = new CompletableFuture<>();
future.completeExceptionally(RUNTIME_EXCEPTION);
when(delegate.resolveAsync(FQDN)).thenReturn(future);

try {
resolver.resolveAsync(FQDN).toCompletableFuture().get();
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -197,12 +201,12 @@ public void shouldDoSomethingWithNulls() {
ChangeNotifier.Listener<String> 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))));
Expand All @@ -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();
Expand Down