Search in sources :

Example 6 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method retryDefaultListenerTest.

@Test
public void retryDefaultListenerTest() throws Exception {
    int retriesNextServer = 1;
    int retriesSameServer = 1;
    boolean retryable = true;
    boolean retryOnAllOps = true;
    String serviceName = "listener";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.POST;
    URI uri = new URI("http://" + host + ":" + port);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    doReturn(200).when(statusLine).getStatusCode();
    doReturn(statusLine).when(response).getStatusLine();
    doThrow(new IOException("boom")).doThrow(new IOException("boom again")).doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
    ILoadBalancer lb = mock(ILoadBalancer.class);
    MyBackOffPolicy myBackOffPolicy = new MyBackOffPolicy();
    MyRetryListener myRetryListener = new MyRetryListener();
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", myBackOffPolicy, false, new RetryListener[] {});
    RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
    doReturn(method).when(request).getMethod();
    doReturn(uri).when(request).getURI();
    doReturn(request).when(request).withNewUri(any(URI.class));
    HttpUriRequest uriRequest = mock(HttpUriRequest.class);
    doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
    RibbonApacheHttpResponse returnedResponse = client.execute(request, null);
    verify(response, times(0)).close();
    verify(delegate, times(3)).execute(any(HttpUriRequest.class));
    verify(lb, times(2)).chooseServer(eq(serviceName));
    assertEquals(2, myBackOffPolicy.getCount());
    assertEquals(0, myRetryListener.getOnError());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 7 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testRetryFail.

@Test
public void testRetryFail() throws Exception {
    int retriesNextServer = 0;
    int retriesSameServer = 1;
    boolean retryable = true;
    boolean retryOnAllOps = false;
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    URI uri = new URI("http://" + host + ":" + port);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    StatusLine fourOFourStatusLine = mock(StatusLine.class);
    CloseableHttpResponse fourOFourResponse = mock(CloseableHttpResponse.class);
    Locale locale = new Locale("en");
    doReturn(locale).when(fourOFourResponse).getLocale();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            HttpEntity entity = mock(HttpEntity.class);
            doReturn(new ByteArrayInputStream("test".getBytes())).when(entity).getContent();
            return entity;
        }
    }).when(fourOFourResponse).getEntity();
    doReturn(404).when(fourOFourStatusLine).getStatusCode();
    doReturn(fourOFourStatusLine).when(fourOFourResponse).getStatusLine();
    doReturn(locale).when(fourOFourResponse).getLocale();
    doReturn(fourOFourResponse).when(delegate).execute(any(HttpUriRequest.class));
    ILoadBalancer lb = mock(ILoadBalancer.class);
    MyBackOffPolicy myBackOffPolicy = new MyBackOffPolicy();
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "404", myBackOffPolicy);
    RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
    doReturn(uri).when(request).getURI();
    doReturn(method).when(request).getMethod();
    doReturn(request).when(request).withNewUri(any(URI.class));
    HttpUriRequest uriRequest = mock(HttpUriRequest.class);
    doReturn(uri).when(uriRequest).getURI();
    doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
    RibbonApacheHttpResponse returnedResponse = client.execute(request, null);
    verify(delegate, times(2)).execute(any(HttpUriRequest.class));
    byte[] buf = new byte[100];
    InputStream inputStream = returnedResponse.getInputStream();
    int read = inputStream.read(buf);
    assertThat(new String(buf, 0, read), is("test"));
}
Also used : Locale(java.util.Locale) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StatusLine(org.apache.http.StatusLine) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 8 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testRetrySameServerOnly.

@Test
public void testRetrySameServerOnly() throws Exception {
    int retriesNextServer = 0;
    int retriesSameServer = 1;
    boolean retryable = true;
    boolean retryOnAllOps = false;
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    URI uri = new URI("http://" + host + ":" + port);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    doReturn(200).when(statusLine).getStatusCode();
    doReturn(statusLine).when(response).getStatusLine();
    doThrow(new IOException("boom")).doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
    ILoadBalancer lb = mock(ILoadBalancer.class);
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", null);
    RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
    doReturn(uri).when(request).getURI();
    doReturn(method).when(request).getMethod();
    doReturn(request).when(request).withNewUri(any(URI.class));
    HttpUriRequest uriRequest = mock(HttpUriRequest.class);
    doReturn(uri).when(uriRequest).getURI();
    doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
    RibbonApacheHttpResponse returnedResponse = client.execute(request, null);
    verify(delegate, times(2)).execute(any(HttpUriRequest.class));
    verify(lb, times(1)).chooseServer(eq(serviceName));
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 9 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonLoadBalancingHttpClientTests method testRetryNextServer.

@Test
public void testRetryNextServer() throws Exception {
    int retriesNextServer = 1;
    int retriesSameServer = 1;
    boolean retryable = true;
    boolean retryOnAllOps = false;
    String serviceName = "foo";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.GET;
    URI uri = new URI("http://" + host + ":" + port);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    StatusLine statusLine = mock(StatusLine.class);
    doReturn(200).when(statusLine).getStatusCode();
    doReturn(statusLine).when(response).getStatusLine();
    doThrow(new IOException("boom")).doThrow(new IOException("boom again")).doReturn(response).when(delegate).execute(any(HttpUriRequest.class));
    ILoadBalancer lb = mock(ILoadBalancer.class);
    MyBackOffPolicy myBackOffPolicy = new MyBackOffPolicy();
    RetryableRibbonLoadBalancingHttpClient client = setupClientForRetry(retriesNextServer, retriesSameServer, retryable, retryOnAllOps, serviceName, host, port, delegate, lb, "", myBackOffPolicy);
    RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
    doReturn(uri).when(request).getURI();
    doReturn(method).when(request).getMethod();
    doReturn(request).when(request).withNewUri(any(URI.class));
    HttpUriRequest uriRequest = mock(HttpUriRequest.class);
    doReturn(uri).when(uriRequest).getURI();
    doReturn(uriRequest).when(request).toRequest(any(RequestConfig.class));
    client.execute(request, null);
    verify(delegate, times(3)).execute(any(HttpUriRequest.class));
    verify(lb, times(2)).chooseServer(eq(serviceName));
    assertEquals(2, myBackOffPolicy.getCount());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 10 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project spring-cloud-netflix by spring-cloud.

the class RibbonClientConfigurationIntegrationTests method testLoadBalancerConstruction.

@Test
public void testLoadBalancerConstruction() {
    ILoadBalancer loadBalancer = clientFactory.getInstance("test", ILoadBalancer.class);
    assertThat(loadBalancer, is(instanceOf(ZoneAwareLoadBalancer.class)));
    ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) loadBalancer;
    ServerListUpdater serverListUpdater = (PollingServerListUpdater) ReflectionTestUtils.getField(loadBalancer, "serverListUpdater");
    Long refreshIntervalMs = (Long) ReflectionTestUtils.getField(serverListUpdater, "refreshIntervalMs");
    // assertThat(refreshIntervalMs, equalTo(999L));
    ServerListUpdater updater = clientFactory.getInstance("test", ServerListUpdater.class);
    assertThat(updater, is(sameInstance(serverListUpdater)));
}
Also used : PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) ZoneAwareLoadBalancer(com.netflix.loadbalancer.ZoneAwareLoadBalancer) PollingServerListUpdater(com.netflix.loadbalancer.PollingServerListUpdater) ServerListUpdater(com.netflix.loadbalancer.ServerListUpdater) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ILoadBalancer (com.netflix.loadbalancer.ILoadBalancer)25 Test (org.junit.Test)18 URI (java.net.URI)15 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 HttpMethod (org.springframework.http.HttpMethod)15 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)13 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)13 RequestConfig (org.apache.http.client.config.RequestConfig)12 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)11 StatusLine (org.apache.http.StatusLine)10 IOException (java.io.IOException)8 Server (com.netflix.loadbalancer.Server)6 ClientException (com.netflix.client.ClientException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)2 PollingServerListUpdater (com.netflix.loadbalancer.PollingServerListUpdater)2 ZoneAwareLoadBalancer (com.netflix.loadbalancer.ZoneAwareLoadBalancer)2 Locale (java.util.Locale)2 OkHttpClient (okhttp3.OkHttpClient)2