2222import org .junit .Before ;
2323import org .junit .Test ;
2424import org .junit .runner .RunWith ;
25- import org .mockito .Mock ;
25+ import org .mockito .invocation .InvocationOnMock ;
26+ import org .mockito .stubbing .Answer ;
2627import org .powermock .api .mockito .PowerMockito ;
2728import org .powermock .core .classloader .annotations .PrepareForTest ;
2829import org .powermock .modules .junit4 .PowerMockRunner ;
2930
30- import javax .naming .NameNotFoundException ;
31- import javax .naming .NamingEnumeration ;
32- import javax .naming .directory .Attribute ;
33- import javax .naming .directory .Attributes ;
34- import javax .naming .directory .DirContext ;
3531import java .net .InetAddress ;
32+ import java .net .UnknownHostException ;
3633import java .util .HashSet ;
3734import java .util .List ;
3835import java .util .Set ;
3936
4037import static org .junit .Assert .assertEquals ;
38+ import static org .mockito .Mockito .any ;
39+ import static org .mockito .Mockito .anyString ;
4140import static org .mockito .Mockito .mock ;
41+ import static org .mockito .Mockito .never ;
42+ import static org .mockito .Mockito .verify ;
4243import static org .mockito .Mockito .when ;
4344
4445@ RunWith (PowerMockRunner .class )
@@ -47,45 +48,30 @@ public class DnsEndpointResolverTest {
4748 private static final ILogger LOGGER = new NoLogFactory ().getLogger ("no" );
4849
4950 private static final String SERVICE_DNS = "my-release-hazelcast.default.svc.cluster.local" ;
51+ private static final int DEFAULT_SERVICE_DNS_TIMEOUT_SECONDS = 5 ;
52+ private static final int TEST_DNS_TIMEOUT_SECONDS = 1 ;
5053 private static final int UNSET_PORT = 0 ;
5154 private static final int DEFAULT_PORT = 5701 ;
5255 private static final int CUSTOM_PORT = 5702 ;
53- private static final String DNS_SERVER_1 = String .format ("12345.%s" , SERVICE_DNS );
54- private static final String DNS_SERVER_2 = String .format ("6789.%s" , SERVICE_DNS );
55- private static final String DNS_ENTRY_SERVER_1 = String .format ("10 25 0 %s" , DNS_SERVER_1 );
56- private static final String DNS_ENTRY_SERVER_2 = String .format ("10 25 0 %s" , DNS_SERVER_2 );
5756 private static final String IP_SERVER_1 = "192.168.0.5" ;
5857 private static final String IP_SERVER_2 = "192.168.0.6" ;
5958
60- @ Mock
61- private NamingEnumeration servers ;
62- @ Mock
63- private DirContext dirContext ;
64-
6559 @ Before
6660 public void setUp ()
6761 throws Exception {
6862 PowerMockito .mockStatic (InetAddress .class );
6963
70- Attributes attributes = mock (Attributes .class );
71- when (dirContext .getAttributes (SERVICE_DNS , new String []{"SRV" })).thenReturn (attributes );
72- Attribute attribute = mock (Attribute .class );
73- when (attributes .get ("srv" )).thenReturn (attribute );
74- when (attribute .getAll ()).thenReturn (servers );
75- when (servers .next ()).thenReturn (DNS_ENTRY_SERVER_1 , DNS_ENTRY_SERVER_2 );
76- when (servers .hasMore ()).thenReturn (true , true , false );
7764 InetAddress address1 = mock (InetAddress .class );
78- PowerMockito .when (InetAddress .getByName (DNS_SERVER_1 )).thenReturn (address1 );
7965 InetAddress address2 = mock (InetAddress .class );
80- PowerMockito .when (InetAddress .getByName (DNS_SERVER_2 )).thenReturn (address2 );
8166 when (address1 .getHostAddress ()).thenReturn (IP_SERVER_1 );
8267 when (address2 .getHostAddress ()).thenReturn (IP_SERVER_2 );
68+ PowerMockito .when (InetAddress .getAllByName (SERVICE_DNS )).thenReturn (new InetAddress []{address1 , address2 });
8369 }
8470
8571 @ Test
8672 public void resolve () {
8773 // given
88- DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , UNSET_PORT , dirContext );
74+ DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , UNSET_PORT , DEFAULT_SERVICE_DNS_TIMEOUT_SECONDS );
8975
9076 // when
9177 List <DiscoveryNode > result = dnsEndpointResolver .resolve ();
@@ -101,7 +87,7 @@ public void resolve() {
10187 @ Test
10288 public void resolveCustomPort () {
10389 // given
104- DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , CUSTOM_PORT , dirContext );
90+ DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , CUSTOM_PORT , DEFAULT_SERVICE_DNS_TIMEOUT_SECONDS );
10591
10692 // when
10793 List <DiscoveryNode > result = dnsEndpointResolver .resolve ();
@@ -118,28 +104,58 @@ public void resolveCustomPort() {
118104 public void resolveException ()
119105 throws Exception {
120106 // given
121- when (dirContext .getAttributes (SERVICE_DNS , new String []{"SRV" })).thenThrow (new NameNotFoundException ());
122- DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , UNSET_PORT , dirContext );
107+ ILogger logger = mock (ILogger .class );
108+ PowerMockito .when (InetAddress .getAllByName (SERVICE_DNS )).thenThrow (new UnknownHostException ());
109+ DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (logger , SERVICE_DNS , UNSET_PORT , DEFAULT_SERVICE_DNS_TIMEOUT_SECONDS );
123110
124111 // when
125112 List <DiscoveryNode > result = dnsEndpointResolver .resolve ();
126113
127114 // then
128115 assertEquals (0 , result .size ());
116+ verify (logger ).warning (String .format ("DNS lookup for serviceDns '%s' failed: unknown host" , SERVICE_DNS ));
117+ verify (logger , never ()).warning (anyString (), any (Throwable .class ));
129118 }
130119
131120 @ Test
132121 public void resolveNotFound ()
133122 throws Exception {
134123 // given
135- when (servers .hasMore ()).thenReturn (false );
136- DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , UNSET_PORT , dirContext );
124+ PowerMockito .when (InetAddress .getAllByName (SERVICE_DNS )).thenReturn (new InetAddress [0 ]);
125+ DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (LOGGER , SERVICE_DNS , UNSET_PORT , DEFAULT_SERVICE_DNS_TIMEOUT_SECONDS );
126+
127+ // when
128+ List <DiscoveryNode > result = dnsEndpointResolver .resolve ();
129+
130+ // then
131+ assertEquals (0 , result .size ());
132+ }
133+
134+ @ Test
135+ public void resolveTimeout ()
136+ throws Exception {
137+ // given
138+ ILogger logger = mock (ILogger .class );
139+ PowerMockito .when (InetAddress .getAllByName (SERVICE_DNS )).then (waitAndAnswer ());
140+ DnsEndpointResolver dnsEndpointResolver = new DnsEndpointResolver (logger , SERVICE_DNS , UNSET_PORT , TEST_DNS_TIMEOUT_SECONDS );
137141
138142 // when
139143 List <DiscoveryNode > result = dnsEndpointResolver .resolve ();
140144
141145 // then
142146 assertEquals (0 , result .size ());
147+ verify (logger ).warning (String .format ("DNS lookup for serviceDns '%s' failed: DNS resolution timeout" , SERVICE_DNS ));
148+ verify (logger , never ()).warning (anyString (), any (Throwable .class ));
149+ }
150+
151+ private static Answer <InetAddress []> waitAndAnswer () {
152+ return new Answer <InetAddress []>() {
153+ @ Override
154+ public InetAddress [] answer (InvocationOnMock invocation ) throws Throwable {
155+ Thread .sleep (TEST_DNS_TIMEOUT_SECONDS * 5 * 1000 );
156+ return new InetAddress [0 ];
157+ }
158+ };
143159 }
144160
145161 private static Set <?> setOf (Object ... objects ) {
0 commit comments