Search in sources :

Example 21 with ILoadBalancer

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

the class RibbonLoadBalancingHttpClientTests method noServersFoundTest.

@Test
public void noServersFoundTest() throws Exception {
    String serviceName = "noservers";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.POST;
    URI uri = new URI("http://" + host + ":" + port);
    CloseableHttpClient delegate = mock(CloseableHttpClient.class);
    ILoadBalancer lb = mock(ILoadBalancer.class);
    RetryableRibbonLoadBalancingHttpClient client = setupClientForServerValidation(serviceName, host, port, delegate, lb);
    RibbonApacheHttpRequest request = mock(RibbonApacheHttpRequest.class);
    doReturn(null).when(lb).chooseServer(eq(serviceName));
    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));
    try {
        client.execute(request, null);
        fail("Expected IOException for no servers available");
    } catch (ClientException ex) {
        assertThat(ex.getMessage(), containsString("Load balancer does not have available server for client"));
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientException(com.netflix.client.ClientException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 22 with ILoadBalancer

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

the class RibbonLoadBalancingHttpClientTests method testRetryOnStatusCode.

@Test
public void testRetryOnStatusCode() 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);
    Locale locale = new Locale("en");
    doReturn(locale).when(response).getLocale();
    StatusLine statusLine = mock(StatusLine.class);
    doReturn(200).when(statusLine).getStatusCode();
    doReturn(statusLine).when(response).getStatusLine();
    final CloseableHttpResponse fourOFourResponse = mock(CloseableHttpResponse.class);
    doReturn(locale).when(fourOFourResponse).getLocale();
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContentLength(5);
    entity.setContent(new ByteArrayInputStream("error".getBytes()));
    doReturn(entity).when(fourOFourResponse).getEntity();
    StatusLine fourOFourStatusLine = mock(StatusLine.class);
    doReturn(404).when(fourOFourStatusLine).getStatusCode();
    doReturn(fourOFourStatusLine).when(fourOFourResponse).getStatusLine();
    doReturn(fourOFourResponse).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, "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));
    client.execute(request, null);
    verify(fourOFourResponse, times(1)).close();
    verify(delegate, times(2)).execute(any(HttpUriRequest.class));
    verify(lb, times(1)).chooseServer(eq(serviceName));
    assertEquals(1, myBackOffPolicy.getCount());
}
Also used : Locale(java.util.Locale) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) URI(java.net.URI) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 23 with ILoadBalancer

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

the class SpringRetryEnabledOkHttpClientTests method noServersFoundTest.

@Test
public void noServersFoundTest() throws Exception {
    String serviceName = "noservers";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.POST;
    URI uri = new URI("http://" + host + ":" + port);
    OkHttpClient delegate = mock(OkHttpClient.class);
    ILoadBalancer lb = mock(ILoadBalancer.class);
    RetryableOkHttpLoadBalancingClient client = setupClientForServerValidation(serviceName, host, port, delegate, lb);
    OkHttpRibbonRequest request = mock(OkHttpRibbonRequest.class);
    doReturn(null).when(lb).chooseServer(eq(serviceName));
    doReturn(method).when(request).getMethod();
    doReturn(uri).when(request).getURI();
    doReturn(request).when(request).withNewUri(any(URI.class));
    Request okRequest = new Request.Builder().url("ws:testerror.sc").build();
    doReturn(okRequest).when(request).toRequest();
    try {
        client.execute(request, null);
        fail("Expected ClientException for no servers available");
    } catch (ClientException ex) {
        assertThat(ex.getMessage(), containsString("Load balancer does not have available server for client"));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) Request(okhttp3.Request) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientException(com.netflix.client.ClientException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 24 with ILoadBalancer

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

the class SpringRetryEnabledOkHttpClientTests method invalidServerTest.

@Test
public void invalidServerTest() throws Exception {
    String serviceName = "noservers";
    String host = serviceName;
    int port = 80;
    HttpMethod method = HttpMethod.POST;
    URI uri = new URI("http://" + host + ":" + port);
    OkHttpClient delegate = mock(OkHttpClient.class);
    ILoadBalancer lb = mock(ILoadBalancer.class);
    RetryableOkHttpLoadBalancingClient client = setupClientForServerValidation(serviceName, host, port, delegate, lb);
    OkHttpRibbonRequest request = mock(OkHttpRibbonRequest.class);
    doReturn(new Server(null, 8000)).when(lb).chooseServer(eq(serviceName));
    doReturn(method).when(request).getMethod();
    doReturn(uri).when(request).getURI();
    doReturn(request).when(request).withNewUri(any(URI.class));
    Request okRequest = new Request.Builder().url("ws:testerror.sc").build();
    doReturn(okRequest).when(request).toRequest();
    try {
        client.execute(request, null);
        fail("Expected ClientException for no Invalid Host");
    } catch (ClientException ex) {
        assertThat(ex.getMessage(), containsString("Invalid Server for: "));
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Server(com.netflix.loadbalancer.Server) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) Request(okhttp3.Request) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ClientException(com.netflix.client.ClientException) URI(java.net.URI) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 25 with ILoadBalancer

use of com.netflix.loadbalancer.ILoadBalancer in project feign-reactive by kptfh.

the class LoadBalancingReactiveHttpClientTest method setupServersList.

@BeforeClass
public static void setupServersList() throws ClientException {
    DefaultClientConfigImpl clientConfig = new DefaultClientConfigImpl();
    clientConfig.loadDefaultValues();
    clientConfig.setProperty(CommonClientConfigKey.NFLoadBalancerClassName, BaseLoadBalancer.class.getName());
    ILoadBalancer lb = ClientFactory.registerNamedLoadBalancerFromclientConfig(serviceName, clientConfig);
    lb.addServers(asList(new Server("localhost", server1.port()), new Server("localhost", server2.port())));
}
Also used : Server(com.netflix.loadbalancer.Server) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) DefaultClientConfigImpl(com.netflix.client.config.DefaultClientConfigImpl)

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