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"));
}
}
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());
}
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"));
}
}
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: "));
}
}
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())));
}
Aggregations