use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.
the class RibbonInterceptorTests method testIntercept.
@Test
public void testIntercept() throws Exception {
RibbonServer server = new RibbonServer("myservice", new Server("myhost", 8080));
LoadBalancerInterceptor interceptor = new LoadBalancerInterceptor(new MyClient(server));
given(this.request.getURI()).willReturn(new URL("http://myservice").toURI());
given(this.execution.execute(isA(HttpRequest.class), isA(byte[].class))).willReturn(this.response);
ArgumentCaptor<HttpRequestWrapper> argument = ArgumentCaptor.forClass(HttpRequestWrapper.class);
ClientHttpResponse response = interceptor.intercept(this.request, new byte[0], this.execution);
assertNotNull("response was null", response);
verify(this.execution).execute(argument.capture(), isA(byte[].class));
HttpRequestWrapper wrapper = argument.getValue();
assertEquals("wrong constructed uri", new URL("http://myhost:8080").toURI(), wrapper.getURI());
}
use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyRetryCount.
@Test
public void testGetRetryPolicyRetryCount() throws Exception {
int sameServer = 3;
int nextServer = 3;
RibbonServer server = getRibbonServer();
IClientConfig config = mock(IClientConfig.class);
doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
doReturn(false).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), eq(false));
doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
HttpRequest request = mock(HttpRequest.class);
doReturn(HttpMethod.GET).when(request).getMethod();
LoadBalancedRetryContext context = spy(new LoadBalancedRetryContext(null, request));
// inner loop is for same server retries
for (int i = 0; i < nextServer + 1; i++) {
// the same sever counter and increment the next server counter
for (int j = 0; j < sameServer + 1; j++) {
if (j < 3) {
assertThat(policy.canRetrySameServer(context), is(true));
} else {
assertThat(policy.canRetrySameServer(context), is(false));
}
policy.registerThrowable(context, new IOException());
}
if (i < 3) {
assertThat(policy.canRetryNextServer(context), is(true));
} else {
assertThat(policy.canRetryNextServer(context), is(false));
}
}
assertThat(context.isExhaustedOnly(), is(true));
assertThat(policy.retryableStatusCode(400), is(false));
verify(context, times(4)).setServiceInstance(any(ServiceInstance.class));
}
use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancedRetryFactoryTests method testGetRetryPolicyNoRetry.
@Test
public void testGetRetryPolicyNoRetry() throws Exception {
int sameServer = 0;
int nextServer = 0;
boolean retryOnAllOps = false;
RibbonServer server = getRibbonServer();
IClientConfig config = mock(IClientConfig.class);
doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
doReturn(sameServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
doReturn(nextServer).when(config).getPropertyAsInteger(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
doReturn(retryOnAllOps).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
doReturn(retryOnAllOps).when(config).getPropertyAsBoolean(eq(CommonClientConfigKey.OkToRetryOnAllOperations), anyBoolean());
doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
doReturn(server.getServiceId()).when(config).getClientName();
doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
HttpRequest request = mock(HttpRequest.class);
doReturn(HttpMethod.GET).when(request).getMethod();
LoadBalancedRetryContext context = new LoadBalancedRetryContext(null, request);
assertThat(policy.canRetryNextServer(context), is(true));
assertThat(policy.canRetrySameServer(context), is(false));
assertThat(policy.retryableStatusCode(400), is(false));
}
use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancedRetryFactoryTests method testCiruitRelatedExceptionsUpdateServerStats.
@Test
public void testCiruitRelatedExceptionsUpdateServerStats() throws Exception {
int sameServer = 3;
int nextServer = 3;
RibbonServer server = getRibbonServer();
IClientConfig config = mock(IClientConfig.class);
doReturn(sameServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetries), anyInt());
doReturn(nextServer).when(config).get(eq(CommonClientConfigKey.MaxAutoRetriesNextServer), anyInt());
doReturn(false).when(config).get(eq(CommonClientConfigKey.OkToRetryOnAllOperations), eq(false));
doReturn(config).when(clientFactory).getClientConfig(eq(server.getServiceId()));
doReturn("").when(config).getPropertyAsString(eq(RibbonLoadBalancedRetryPolicy.RETRYABLE_STATUS_CODES), eq(""));
clientFactory.getLoadBalancerContext(server.getServiceId()).setRetryHandler(new DefaultLoadBalancerRetryHandler(config));
RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
RibbonLoadBalancedRetryFactory factory = new RibbonLoadBalancedRetryFactory(clientFactory);
LoadBalancedRetryPolicy policy = factory.createRetryPolicy(server.getServiceId(), client);
HttpRequest request = mock(HttpRequest.class);
LoadBalancedRetryContext context = spy(new LoadBalancedRetryContext(null, request));
doReturn(server).when(context).getServiceInstance();
policy.registerThrowable(context, new IOException());
verify(serverStats, times(0)).incrementSuccessiveConnectionFailureCount();
// Circuit Related should increment failure count
policy.registerThrowable(context, new SocketException());
verify(serverStats, times(1)).incrementSuccessiveConnectionFailureCount();
}
use of org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer in project spring-cloud-netflix by spring-cloud.
the class RibbonLoadBalancerClientTests method testExecute.
@Test
public void testExecute() throws IOException {
final RibbonServer server = getRibbonServer();
RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
final String returnVal = "myval";
Object actualReturn = client.execute(server.getServiceId(), (LoadBalancerRequest<Object>) instance -> {
assertServiceInstance(server, instance);
return returnVal;
});
verifyServerStats();
assertEquals("retVal was wrong", returnVal, actualReturn);
}
Aggregations