Search in sources :

Example 11 with RibbonServer

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());
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) Server(com.netflix.loadbalancer.Server) HttpRequestWrapper(org.springframework.http.client.support.HttpRequestWrapper) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) LoadBalancerInterceptor(org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor) URL(java.net.URL) Test(org.junit.Test)

Example 12 with RibbonServer

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));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) ServiceInstance(org.springframework.cloud.client.ServiceInstance) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) IOException(java.io.IOException) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 13 with RibbonServer

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));
}
Also used : HttpRequest(org.springframework.http.HttpRequest) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 14 with RibbonServer

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();
}
Also used : HttpRequest(org.springframework.http.HttpRequest) SocketException(java.net.SocketException) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) LoadBalancedRetryPolicy(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy) IClientConfig(com.netflix.client.config.IClientConfig) DefaultLoadBalancerRetryHandler(com.netflix.client.DefaultLoadBalancerRetryHandler) IOException(java.io.IOException) LoadBalancedRetryContext(org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext) Test(org.junit.Test)

Example 15 with RibbonServer

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);
}
Also used : LoadBalancerRequest(org.springframework.cloud.client.loadbalancer.LoadBalancerRequest) ServerStats(com.netflix.loadbalancer.ServerStats) URL(java.net.URL) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) Matchers.anyString(org.mockito.Matchers.anyString) MockitoAnnotations(org.mockito.MockitoAnnotations) BDDMockito.given(org.mockito.BDDMockito.given) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Matchers.anyObject(org.mockito.Matchers.anyObject) DefaultUriBuilderFactory(org.springframework.web.util.DefaultUriBuilderFactory) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) LoadBalancerStats(com.netflix.loadbalancer.LoadBalancerStats) Before(org.junit.Before) Server(com.netflix.loadbalancer.Server) Assert.assertNotNull(org.junit.Assert.assertNotNull) IClientConfig(com.netflix.client.config.IClientConfig) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Matchers.anyDouble(org.mockito.Matchers.anyDouble) Mockito.verify(org.mockito.Mockito.verify) CommonClientConfigKey(com.netflix.client.config.CommonClientConfigKey) Assert.assertNull(org.junit.Assert.assertNull) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) RibbonServer(org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer) Matchers.anyObject(org.mockito.Matchers.anyObject) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

RibbonServer (org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer)16 Test (org.junit.Test)13 IClientConfig (com.netflix.client.config.IClientConfig)10 ServiceInstance (org.springframework.cloud.client.ServiceInstance)8 HttpRequest (org.springframework.http.HttpRequest)7 LoadBalancedRetryPolicy (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryPolicy)6 DefaultLoadBalancerRetryHandler (com.netflix.client.DefaultLoadBalancerRetryHandler)5 IOException (java.io.IOException)5 URI (java.net.URI)5 LoadBalancedRetryContext (org.springframework.cloud.client.loadbalancer.LoadBalancedRetryContext)5 Server (com.netflix.loadbalancer.Server)4 URL (java.net.URL)4 ServerStats (com.netflix.loadbalancer.ServerStats)2 DefaultUriBuilderFactory (org.springframework.web.util.DefaultUriBuilderFactory)2 CommonClientConfigKey (com.netflix.client.config.CommonClientConfigKey)1 BaseLoadBalancer (com.netflix.loadbalancer.BaseLoadBalancer)1 LoadBalancerStats (com.netflix.loadbalancer.LoadBalancerStats)1 SocketException (java.net.SocketException)1 Collections (java.util.Collections)1 Map (java.util.Map)1