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