Search in sources :

Example 6 with AwsEndpoint

use of com.netflix.discovery.shared.resolver.aws.AwsEndpoint in project eureka by Netflix.

the class EurekaHttpClientsTest method testCompositeBootstrapResolver.

@Test
public void testCompositeBootstrapResolver() throws Exception {
    Applications applications = InstanceInfoGenerator.newBuilder(5, "eurekaWrite", "someOther").build().toApplications();
    Applications applications2 = InstanceInfoGenerator.newBuilder(2, "eurekaWrite", "someOther").build().toApplications();
    String vipAddress = applications.getRegisteredApplications("eurekaWrite").getInstances().get(0).getVIPAddress();
    // setup client config to use fixed root ips for testing
    when(clientConfig.shouldUseDnsForFetchingServiceUrls()).thenReturn(false);
    // can use anything here
    when(clientConfig.getEurekaServerServiceUrls(anyString())).thenReturn(Arrays.asList("http://foo:0"));
    when(clientConfig.getRegion()).thenReturn("us-east-1");
    when(transportConfig.getWriteClusterVip()).thenReturn(vipAddress);
    when(transportConfig.getAsyncExecutorThreadPoolSize()).thenReturn(4);
    when(transportConfig.getAsyncResolverRefreshIntervalMs()).thenReturn(400);
    when(transportConfig.getAsyncResolverWarmUpTimeoutMs()).thenReturn(400);
    ApplicationsResolver.ApplicationsSource applicationsSource = mock(ApplicationsResolver.ApplicationsSource.class);
    when(applicationsSource.getApplications(anyInt(), eq(TimeUnit.SECONDS))).thenReturn(// first time
    null).thenReturn(// second time
    applications).thenReturn(// subsequent times
    null);
    EurekaHttpClient mockHttpClient = mock(EurekaHttpClient.class);
    when(mockHttpClient.getVip(eq(vipAddress))).thenReturn(anEurekaHttpResponse(200, applications).build()).thenReturn(// contains diff number of servers
    anEurekaHttpResponse(200, applications2).build());
    TransportClientFactory transportClientFactory = mock(TransportClientFactory.class);
    when(transportClientFactory.newClient(any(EurekaEndpoint.class))).thenReturn(mockHttpClient);
    ClosableResolver<AwsEndpoint> resolver = null;
    try {
        resolver = EurekaHttpClients.compositeBootstrapResolver(clientConfig, transportConfig, transportClientFactory, applicationInfoManager.getInfo(), applicationsSource);
        List endpoints = resolver.getClusterEndpoints();
        assertThat(endpoints.size(), equalTo(applications.getInstancesByVirtualHostName(vipAddress).size()));
        // wait for the second cycle that hits the app source
        verify(applicationsSource, timeout(3000).times(2)).getApplications(anyInt(), eq(TimeUnit.SECONDS));
        endpoints = resolver.getClusterEndpoints();
        assertThat(endpoints.size(), equalTo(applications.getInstancesByVirtualHostName(vipAddress).size()));
        // wait for the third cycle that triggers the mock http client (which is the third resolver cycle)
        // for the third cycle we have mocked the application resolver to return null data so should fall back
        // to calling the remote resolver again (which should return applications2)
        verify(mockHttpClient, timeout(3000).times(3)).getVip(anyString());
        endpoints = resolver.getClusterEndpoints();
        assertThat(endpoints.size(), equalTo(applications2.getInstancesByVirtualHostName(vipAddress).size()));
    } finally {
        if (resolver != null) {
            resolver.shutdown();
        }
    }
}
Also used : ApplicationsResolver(com.netflix.discovery.shared.resolver.aws.ApplicationsResolver) Applications(com.netflix.discovery.shared.Applications) AwsEndpoint(com.netflix.discovery.shared.resolver.aws.AwsEndpoint) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) EurekaEndpoint(com.netflix.discovery.shared.resolver.EurekaEndpoint) Test(org.junit.Test)

Aggregations

AwsEndpoint (com.netflix.discovery.shared.resolver.aws.AwsEndpoint)6 AsyncResolver (com.netflix.discovery.shared.resolver.AsyncResolver)3 ConfigClusterResolver (com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver)3 ZoneAffinityClusterResolver (com.netflix.discovery.shared.resolver.aws.ZoneAffinityClusterResolver)3 ClusterResolver (com.netflix.discovery.shared.resolver.ClusterResolver)2 ApplicationsResolver (com.netflix.discovery.shared.resolver.aws.ApplicationsResolver)2 List (java.util.List)2 AmazonInfo (com.netflix.appinfo.AmazonInfo)1 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)1 Applications (com.netflix.discovery.shared.Applications)1 EurekaEndpoint (com.netflix.discovery.shared.resolver.EurekaEndpoint)1 EurekaHttpResolver (com.netflix.discovery.shared.resolver.aws.EurekaHttpResolver)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1