Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Added two more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jc89 committed Nov 15, 2016
1 parent f49f24a commit 78510d9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/test/java/com/spotify/dns/DnsSrvResolversIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public void shouldSucceedCreatingRetainingDnsResolver() throws Exception {
}
}

@Test
public void shouldSucceedCreatingNotUsingLookupCache() throws Exception {
DnsSrvResolvers.newBuilder().useLookupCache(false).build();
}

// TODO: it would be nice to be able to also test things like intermittent DNS failures, etc.,
// but that takes a lot of work setting up a DNS infrastructure that can be made to fail in a
// controlled way, so I'm skipping that.
Expand Down
50 changes: 33 additions & 17 deletions src/test/java/com/spotify/dns/NoCachingLookupFactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,59 @@
package com.spotify.dns;

import org.junit.Before;
import com.google.common.base.Throwables;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.xbill.DNS.DClass;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Message;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.DClass;
import org.xbill.DNS.Type;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class NoCachingLookupFactoryTest {
NoCachingLookupFactory factory;

@Rule
public ExpectedException thrown = ExpectedException.none();

@Mock
LookupFactory delegate;

@Before
public void setUp() throws Exception {
factory = new NoCachingLookupFactory(delegate);
}

@Test
public void shouldCallDelegate() throws Exception {
Mockito.when(delegate.forName(Mockito.anyString())).thenReturn(new Lookup("some.domain.", Type.SRV, DClass.IN));
public void shouldCallDelegateTwiceWhenResolvingADomainTwice() throws Exception {
String fdn = "_spotify-client._tcp.spotify.com.";

final Resolver resolver = Mockito.mock(Resolver.class);

// Lets fetch one record just to save some mocking code
Record someRecord = new SimpleLookupFactory().forName(fdn).run()[0];

Mockito.when(resolver.send(Mockito.any(Message.class))).thenReturn(Message.newQuery(someRecord));

delegate = new LookupFactory() {
@Override
public Lookup forName(String fqdn) {
try {
Lookup lookupResult = new Lookup(fqdn, Type.SRV, DClass.IN);
lookupResult.setResolver(resolver);
return lookupResult;
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
};

NoCachingLookupFactory factory = new NoCachingLookupFactory(delegate);

assertThat(factory.forName("some.domain."), is(notNullValue()));
factory.forName(fdn).run();
factory.forName(fdn).run();

Mockito.verify(delegate).forName(Mockito.eq("some.domain."));
Mockito.verify(resolver, Mockito.times(2)).send(Mockito.any(Message.class));
}

@Test
Expand All @@ -52,6 +68,6 @@ public void shouldPropagateWhenErrorOnDelegate() throws Exception {
Mockito.when(delegate.forName(Mockito.anyString())).thenThrow(new RuntimeException("Error resolving"));
thrown.expect(RuntimeException.class);
thrown.expectMessage("Error resolving");
factory.forName("some.domain.");
new NoCachingLookupFactory(delegate).forName("some.domain.");
}
}

0 comments on commit 78510d9

Please sign in to comment.