From fb76beb3f27ef8e39ea1fb4864f51c3363fc5f4b Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 20 Dec 2017 17:55:25 -0500 Subject: [PATCH 1/5] Test for NPE when calling getClass() in RefreshScope fixes gh-297 --- .../RefreshScopeNullBeanIntegrationTests.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeNullBeanIntegrationTests.java diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeNullBeanIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeNullBeanIntegrationTests.java new file mode 100644 index 00000000..b02c5a30 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeNullBeanIntegrationTests.java @@ -0,0 +1,85 @@ +/* + * Copyright 2006-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.context.scope.refresh; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {RefreshScopeNullBeanIntegrationTests.TestConfiguration.class}) +public class RefreshScopeNullBeanIntegrationTests { + + @Autowired + private MyCustomComponent myCustomComponent; + + @Autowired + private org.springframework.cloud.context.scope.refresh.RefreshScope scope; + + @Test + @DirtiesContext + public void testRefreshBean() { + assertThat(this.myCustomComponent).isNotNull(); + // ...and then refresh, so the bean is re-initialized: + // this.scope.refreshAll(); + } + + protected static class OptionalService { } + + public static class MyCustomComponent { + + private final OptionalService optionalService; + + public MyCustomComponent(OptionalService optionalService) { + this.optionalService = optionalService; + } + } + + @Configuration + protected static class OptionalConfiguration { + + @Bean + @RefreshScope + public OptionalService service() { + return null; + } + } + + @Configuration + @Import({ RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, OptionalConfiguration.class }) + protected static class TestConfiguration { + + @Autowired(required = false) + private OptionalService optionalService; + + @Bean + public MyCustomComponent myCustomComponent() { + return new MyCustomComponent(optionalService); + } + } + +} From f54e4c8cc6eac18a6c8f74a29239f90f87971430 Mon Sep 17 00:00:00 2001 From: saga Date: Fri, 22 Dec 2017 23:32:39 +0800 Subject: [PATCH 2/5] Supplement spring-retry listener features (#298) * Supplement spring-retry listener features --- .../main/asciidoc/spring-cloud-commons.adoc | 36 ++++ .../LoadBalancedRetryListenerFactory.java | 37 +++++ .../LoadBalancerAutoConfiguration.java | 12 +- .../RetryLoadBalancerInterceptor.java | 26 +++ .../RetryLoadBalancerInterceptorTest.java | 157 ++++++++++++++++-- 5 files changed, 255 insertions(+), 13 deletions(-) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancedRetryListenerFactory.java diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 6653a8e6..eb01aaf7 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -428,6 +428,42 @@ public class MyConfiguration { NOTE: `client` in the above examples should be replaced with your Ribbon client's name. +If you want to add one or more `RetryListener` to your retry you will need to +create a bean of type `LoadBalancedRetryListenerFactory` and return the `RetryListener` array +you would like to use for a given service. + +[source,java,indent=0] +---- +@Configuration +public class MyConfiguration { + @Bean + LoadBalancedRetryListenerFactory retryListenerFactory() { + return new LoadBalancedRetryListenerFactory() { + @Override + public RetryListener[] createRetryListeners(String service) { + return new RetryListener[]{new RetryListener() { + @Override + public boolean open(RetryContext context, RetryCallback callback) { + //TODO Do you business... + return true; + } + + @Override + public void close(RetryContext context, RetryCallback callback, Throwable throwable) { + //TODO Do you business... + } + + @Override + public void onError(RetryContext context, RetryCallback callback, Throwable throwable) { + //TODO Do you business... + } + }}; + } + }; + } +} +---- + === Multiple RestTemplate objects If you want a `RestTemplate` that is not load balanced, create a `RestTemplate` diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancedRetryListenerFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancedRetryListenerFactory.java new file mode 100644 index 00000000..f16a99c7 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancedRetryListenerFactory.java @@ -0,0 +1,37 @@ +/* + * Copyright 2013-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.client.loadbalancer; + +import org.springframework.retry.RetryListener; + +/** + * Factory class to return the retry listeners. + * @author Gang Li + */ +public interface LoadBalancedRetryListenerFactory { + + RetryListener[] createRetryListeners(String service); + + class DefaultRetryListenerFactory implements LoadBalancedRetryListenerFactory { + + @Override + public RetryListener[] createRetryListeners(String service) { + return new RetryListener[0]; + } + } + +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerAutoConfiguration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerAutoConfiguration.java index 67530c4b..dc6ef26b 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerAutoConfiguration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/LoadBalancerAutoConfiguration.java @@ -39,6 +39,7 @@ import org.springframework.web.client.RestTemplate; * @author Spencer Gibb * @author Dave Syer * @author Will Tran + * @author Gang Li */ @Configuration @ConditionalOnClass(RestTemplate.class) @@ -123,6 +124,12 @@ public class LoadBalancerAutoConfiguration { public LoadBalancedBackOffPolicyFactory loadBalancedBackOffPolicyFactory() { return new LoadBalancedBackOffPolicyFactory.NoBackOffPolicyFactory(); } + + @Bean + @ConditionalOnMissingBean + public LoadBalancedRetryListenerFactory loadBalancedRetryListenerFactory() { + return new LoadBalancedRetryListenerFactory.DefaultRetryListenerFactory(); + } } @Configuration @@ -134,9 +141,10 @@ public class LoadBalancerAutoConfiguration { LoadBalancerClient loadBalancerClient, LoadBalancerRetryProperties properties, LoadBalancedRetryPolicyFactory lbRetryPolicyFactory, LoadBalancerRequestFactory requestFactory, - LoadBalancedBackOffPolicyFactory backOffPolicyFactory) { + LoadBalancedBackOffPolicyFactory backOffPolicyFactory, + LoadBalancedRetryListenerFactory retryListenerFactory) { return new RetryLoadBalancerInterceptor(loadBalancerClient, properties, - lbRetryPolicyFactory, requestFactory, backOffPolicyFactory); + lbRetryPolicyFactory, requestFactory, backOffPolicyFactory, retryListenerFactory); } @Bean diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java index d59f1103..ee46b919 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java @@ -33,6 +33,7 @@ import org.springframework.retry.RecoveryCallback; import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; import org.springframework.retry.RetryException; +import org.springframework.retry.RetryListener; import org.springframework.retry.backoff.BackOffPolicy; import org.springframework.retry.backoff.NoBackOffPolicy; import org.springframework.retry.policy.NeverRetryPolicy; @@ -42,6 +43,7 @@ import org.springframework.util.Assert; /** * @author Ryan Baxter * @author Will Tran + * @author Gang Li */ public class RetryLoadBalancerInterceptor implements ClientHttpRequestInterceptor { @@ -51,6 +53,7 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto private LoadBalancerRetryProperties lbProperties; private LoadBalancerRequestFactory requestFactory; private LoadBalancedBackOffPolicyFactory backOffPolicyFactory; + private LoadBalancedRetryListenerFactory retryListenerFactory; @Deprecated @@ -64,8 +67,11 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto this.lbProperties = lbProperties; this.requestFactory = requestFactory; this.backOffPolicyFactory = new LoadBalancedBackOffPolicyFactory.NoBackOffPolicyFactory(); + this.retryListenerFactory = new LoadBalancedRetryListenerFactory.DefaultRetryListenerFactory(); } + @Deprecated + //TODO remove in 2.0.x public RetryLoadBalancerInterceptor(LoadBalancerClient loadBalancer, LoadBalancerRetryProperties lbProperties, LoadBalancedRetryPolicyFactory lbRetryPolicyFactory, @@ -76,8 +82,24 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto this.lbProperties = lbProperties; this.requestFactory = requestFactory; this.backOffPolicyFactory = backOffPolicyFactory; + this.retryListenerFactory = new LoadBalancedRetryListenerFactory.DefaultRetryListenerFactory();; } + public RetryLoadBalancerInterceptor(LoadBalancerClient loadBalancer, + LoadBalancerRetryProperties lbProperties, + LoadBalancedRetryPolicyFactory lbRetryPolicyFactory, + LoadBalancerRequestFactory requestFactory, + LoadBalancedBackOffPolicyFactory backOffPolicyFactory, + LoadBalancedRetryListenerFactory retryListenerFactory) { + this.loadBalancer = loadBalancer; + this.lbRetryPolicyFactory = lbRetryPolicyFactory; + this.lbProperties = lbProperties; + this.requestFactory = requestFactory; + this.backOffPolicyFactory = backOffPolicyFactory; + this.retryListenerFactory = retryListenerFactory; + + } + @Override public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException { @@ -90,6 +112,10 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto BackOffPolicy backOffPolicy = backOffPolicyFactory.createBackOffPolicy(serviceName); template.setBackOffPolicy(backOffPolicy == null ? new NoBackOffPolicy() : backOffPolicy); template.setThrowLastExceptionOnExhausted(true); + RetryListener[] retryListeners = this.retryListenerFactory.createRetryListeners(serviceName); + if (retryListeners != null && retryListeners.length != 0) { + template.setListeners(retryListeners); + } template.setRetryPolicy( !lbProperties.isEnabled() || retryPolicy == null ? new NeverRetryPolicy() : new InterceptorRetryPolicy(request, retryPolicy, loadBalancer, diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java index 8e913df3..2f6a70d6 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java @@ -15,27 +15,29 @@ import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpResponse; import org.springframework.mock.http.client.MockClientHttpResponse; + +import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; import org.springframework.retry.RetryException; +import org.springframework.retry.RetryListener; +import org.springframework.retry.TerminatedRetryException; + import org.springframework.retry.backoff.BackOffContext; import org.springframework.retry.backoff.BackOffInterruptedException; import org.springframework.retry.backoff.BackOffPolicy; -import org.springframework.retry.backoff.ExponentialBackOffPolicy; -import org.springframework.retry.policy.NeverRetryPolicy; -import org.springframework.retry.support.RetryTemplate; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** * @author Ryan Baxter + * @author Gang Li */ @RunWith(MockitoJUnitRunner.class) public class RetryLoadBalancerInterceptorTest { @@ -44,6 +46,7 @@ public class RetryLoadBalancerInterceptorTest { private LoadBalancerRetryProperties lbProperties; private LoadBalancerRequestFactory lbRequestFactory; private LoadBalancedBackOffPolicyFactory backOffPolicyFactory = new LoadBalancedBackOffPolicyFactory.NoBackOffPolicyFactory(); + private LoadBalancedRetryListenerFactory retryListenerFactory = new LoadBalancedRetryListenerFactory.DefaultRetryListenerFactory(); @Before public void setUp() throws Exception { @@ -71,7 +74,7 @@ public class RetryLoadBalancerInterceptorTest { when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()); lbProperties.setEnabled(false); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, - lbRequestFactory, backOffPolicyFactory); + lbRequestFactory, backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); interceptor.intercept(request, body, execution); @@ -92,7 +95,7 @@ public class RetryLoadBalancerInterceptorTest { when(client.execute(eq("foo_underscore"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse); lbProperties.setEnabled(true); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, lbRequestFactory, - backOffPolicyFactory); + backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); ClientHttpResponse rsp = interceptor.intercept(request, body, execution); @@ -110,7 +113,7 @@ public class RetryLoadBalancerInterceptorTest { when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse); lbProperties.setEnabled(true); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, - lbRequestFactory, backOffPolicyFactory); + lbRequestFactory, backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); interceptor.intercept(request, body, execution); @@ -131,7 +134,7 @@ public class RetryLoadBalancerInterceptorTest { when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenReturn(clientHttpResponse); lbProperties.setEnabled(true); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, - lbRequestFactory, backOffPolicyFactory); + lbRequestFactory, backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); ClientHttpResponse rsp = interceptor.intercept(request, body, execution); @@ -159,7 +162,7 @@ public class RetryLoadBalancerInterceptorTest { thenReturn(clientHttpResponseNotFound).thenReturn(clientHttpResponseOk); lbProperties.setEnabled(true); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, - lbRequestFactory, backOffPolicyFactory); + lbRequestFactory, backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); ClientHttpResponse rsp = interceptor.intercept(request, body, execution); @@ -216,7 +219,7 @@ public class RetryLoadBalancerInterceptorTest { when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()).thenReturn(clientHttpResponse); lbProperties.setEnabled(true); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, lbRequestFactory, - backOffPolicyFactory); + backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); ClientHttpResponse rsp = interceptor.intercept(request, body, execution); @@ -241,13 +244,93 @@ public class RetryLoadBalancerInterceptorTest { when(client.execute(eq("foo"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()).thenReturn(clientHttpResponse); lbProperties.setEnabled(true); RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, - lbRequestFactory, backOffPolicyFactory); + lbRequestFactory, backOffPolicyFactory, retryListenerFactory); byte[] body = new byte[]{}; ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); ClientHttpResponse rsp = interceptor.intercept(request, body, execution); verify(lbRequestFactory).createRequest(request, body, execution); } + @Test + public void retryListenerTest() throws Throwable { + HttpRequest request = mock(HttpRequest.class); + when(request.getURI()).thenReturn(new URI("http://listener")); + ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK); + LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class); + when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class))).thenReturn(true); + LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class); + when(lbRetryPolicyFactory.create(eq("listener"), any(ServiceInstanceChooser.class))).thenReturn(policy); + LoadBalancedBackOffPolicyFactory backOffPolicyFactory = mock(LoadBalancedBackOffPolicyFactory.class); + MyBackOffPolicy backOffPolicy = new MyBackOffPolicy(); + when(backOffPolicyFactory.createBackOffPolicy(eq("listener"))).thenReturn(backOffPolicy); + ServiceInstance serviceInstance = mock(ServiceInstance.class); + when(client.choose(eq("listener"))).thenReturn(serviceInstance); + when(client.execute(eq("listener"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()).thenReturn(clientHttpResponse); + lbProperties.setEnabled(true); + MyRetryListeners retryListeners = new MyRetryListeners(); + RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, lbRequestFactory, + backOffPolicyFactory, retryListeners); + byte[] body = new byte[]{}; + ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); + ClientHttpResponse rsp = interceptor.intercept(request, body, execution); + verify(client, times(2)).execute(eq("listener"), eq(serviceInstance), any(LoadBalancerRequest.class)); + assertThat(rsp, is(clientHttpResponse)); + verify(lbRequestFactory, times(2)).createRequest(request, body, execution); + assertThat(backOffPolicy.getBackoffAttempts(), is(1)); + assertThat(retryListeners.getOnError(), is(1)); + } + + @Test(expected = TerminatedRetryException.class) + public void retryListenerTestNoRetry() throws Throwable { + HttpRequest request = mock(HttpRequest.class); + when(request.getURI()).thenReturn(new URI("http://noRetry")); + ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK); + LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class); + when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class))).thenReturn(true); + LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class); + when(lbRetryPolicyFactory.create(eq("noRetry"), any(ServiceInstanceChooser.class))).thenReturn(policy); + LoadBalancedBackOffPolicyFactory backOffPolicyFactory = mock(LoadBalancedBackOffPolicyFactory.class); + MyBackOffPolicy backOffPolicy = new MyBackOffPolicy(); + when(backOffPolicyFactory.createBackOffPolicy(eq("noRetry"))).thenReturn(backOffPolicy); + ServiceInstance serviceInstance = mock(ServiceInstance.class); + when(client.choose(eq("noRetry"))).thenReturn(serviceInstance); + when(client.execute(eq("noRetry"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()).thenReturn(clientHttpResponse); + lbProperties.setEnabled(true); + MyRetryListenersNotRetry retryListeners = new MyRetryListenersNotRetry(); + RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, lbRequestFactory, + backOffPolicyFactory, retryListeners); + byte[] body = new byte[]{}; + ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); + ClientHttpResponse rsp = interceptor.intercept(request, body, execution); + } + + @Test + public void retryWithDefaultConstructorTest() throws Throwable { + HttpRequest request = mock(HttpRequest.class); + when(request.getURI()).thenReturn(new URI("http://default")); + ClientHttpResponse clientHttpResponse = new MockClientHttpResponse(new byte[]{}, HttpStatus.OK); + LoadBalancedRetryPolicy policy = mock(LoadBalancedRetryPolicy.class); + when(policy.canRetryNextServer(any(LoadBalancedRetryContext.class))).thenReturn(true); + LoadBalancedRetryPolicyFactory lbRetryPolicyFactory = mock(LoadBalancedRetryPolicyFactory.class); + when(lbRetryPolicyFactory.create(eq("default"), any(ServiceInstanceChooser.class))).thenReturn(policy); + LoadBalancedBackOffPolicyFactory backOffPolicyFactory = mock(LoadBalancedBackOffPolicyFactory.class); + MyBackOffPolicy backOffPolicy = new MyBackOffPolicy(); + when(backOffPolicyFactory.createBackOffPolicy(eq("default"))).thenReturn(backOffPolicy); + ServiceInstance serviceInstance = mock(ServiceInstance.class); + when(client.choose(eq("default"))).thenReturn(serviceInstance); + when(client.execute(eq("default"), eq(serviceInstance), any(LoadBalancerRequest.class))).thenThrow(new IOException()).thenReturn(clientHttpResponse); + lbProperties.setEnabled(true); + RetryLoadBalancerInterceptor interceptor = new RetryLoadBalancerInterceptor(client, lbProperties, lbRetryPolicyFactory, lbRequestFactory, + backOffPolicyFactory); + byte[] body = new byte[]{}; + ClientHttpRequestExecution execution = mock(ClientHttpRequestExecution.class); + ClientHttpResponse rsp = interceptor.intercept(request, body, execution); + verify(client, times(2)).execute(eq("default"), eq(serviceInstance), any(LoadBalancerRequest.class)); + assertThat(rsp, is(clientHttpResponse)); + verify(lbRequestFactory, times(2)).createRequest(request, body, execution); + assertThat(backOffPolicy.getBackoffAttempts(), is(1)); + } + class MyBackOffPolicy implements BackOffPolicy { private int backoffAttempts = 0; @@ -271,4 +354,56 @@ public class RetryLoadBalancerInterceptorTest { return backoffAttempts; } } + + class MyRetryListeners implements LoadBalancedRetryListenerFactory { + + private int onError = 0; + + @Override + public RetryListener[] createRetryListeners(String service) { + return new RetryListener[] {new RetryListener() { + @Override + public boolean open(RetryContext context, RetryCallback callback) { + return true; + } + + @Override + public void close(RetryContext context, RetryCallback callback, Throwable throwable) { + + } + + @Override + public void onError(RetryContext context, RetryCallback callback, Throwable throwable) { + onError++; + } + }}; + } + + public int getOnError() { + return onError; + } + } + + class MyRetryListenersNotRetry implements LoadBalancedRetryListenerFactory { + + @Override + public RetryListener[] createRetryListeners(String service) { + return new RetryListener[] {new RetryListener() { + @Override + public boolean open(RetryContext context, RetryCallback callback) { + return false; + } + + @Override + public void close(RetryContext context, RetryCallback callback, Throwable throwable) { + + } + + @Override + public void onError(RetryContext context, RetryCallback callback, Throwable throwable) { + } + }}; + } + + } } \ No newline at end of file From b60d21a076efac6645803e6cfdb5aaee5d62563d Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 11 Jan 2018 21:21:26 +0000 Subject: [PATCH 3/5] Update SNAPSHOT to 1.3.1.RELEASE --- docs/pom.xml | 2 +- pom.xml | 4 ++-- spring-cloud-commons-dependencies/pom.xml | 4 ++-- spring-cloud-commons/pom.xml | 2 +- spring-cloud-context/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index c739e728..9c0dbdd6 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.1.RELEASE pom Spring Cloud Commons Docs diff --git a/pom.xml b/pom.xml index 5bdf0759..de4b8c83 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.1.RELEASE pom Spring Cloud Commons Parent Spring Cloud Commons Parent @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.6.RELEASE + 1.3.7.RELEASE diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index 7a971c42..dff2b35d 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -5,11 +5,11 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.6.RELEASE + 1.3.7.RELEASE spring-cloud-commons-dependencies - 1.3.1.BUILD-SNAPSHOT + 1.3.1.RELEASE pom spring-cloud-commons-dependencies Spring Cloud Commons Dependencies diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index bc6909d0..bf7b13ee 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.1.RELEASE .. spring-cloud-commons diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index de4636a1..2cc1e2e6 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.1.RELEASE .. spring-cloud-context diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index bf5431d8..359b8158 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.1.RELEASE spring-cloud-starter spring-cloud-starter From dd34b7f189a6a15a0d09778a097c6d3000086cb7 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 11 Jan 2018 21:23:03 +0000 Subject: [PATCH 4/5] Going back to snapshots --- docs/pom.xml | 2 +- pom.xml | 4 ++-- spring-cloud-commons-dependencies/pom.xml | 4 ++-- spring-cloud-commons/pom.xml | 2 +- spring-cloud-context/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index 9c0dbdd6..c739e728 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.RELEASE + 1.3.1.BUILD-SNAPSHOT pom Spring Cloud Commons Docs diff --git a/pom.xml b/pom.xml index de4b8c83..5bdf0759 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.cloud spring-cloud-commons-parent - 1.3.1.RELEASE + 1.3.1.BUILD-SNAPSHOT pom Spring Cloud Commons Parent Spring Cloud Commons Parent @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-build - 1.3.7.RELEASE + 1.3.6.RELEASE diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index dff2b35d..7a971c42 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -5,11 +5,11 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.7.RELEASE + 1.3.6.RELEASE spring-cloud-commons-dependencies - 1.3.1.RELEASE + 1.3.1.BUILD-SNAPSHOT pom spring-cloud-commons-dependencies Spring Cloud Commons Dependencies diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index bf7b13ee..bc6909d0 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.RELEASE + 1.3.1.BUILD-SNAPSHOT .. spring-cloud-commons diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index 2cc1e2e6..de4636a1 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.RELEASE + 1.3.1.BUILD-SNAPSHOT .. spring-cloud-context diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index 359b8158..bf5431d8 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.RELEASE + 1.3.1.BUILD-SNAPSHOT spring-cloud-starter spring-cloud-starter From 0b01ca13bb639e9de4511edc0a3d164922b0f5f7 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 11 Jan 2018 21:23:03 +0000 Subject: [PATCH 5/5] Bumping versions to 1.3.2.BUILD-SNAPSHOT after release --- docs/pom.xml | 2 +- pom.xml | 2 +- spring-cloud-commons-dependencies/pom.xml | 2 +- spring-cloud-commons/pom.xml | 2 +- spring-cloud-context/pom.xml | 2 +- spring-cloud-starter/pom.xml | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index c739e728..c47cb511 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.2.BUILD-SNAPSHOT pom Spring Cloud Commons Docs diff --git a/pom.xml b/pom.xml index 5bdf0759..5a6c36b7 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.2.BUILD-SNAPSHOT pom Spring Cloud Commons Parent Spring Cloud Commons Parent diff --git a/spring-cloud-commons-dependencies/pom.xml b/spring-cloud-commons-dependencies/pom.xml index 7a971c42..0c4a765d 100644 --- a/spring-cloud-commons-dependencies/pom.xml +++ b/spring-cloud-commons-dependencies/pom.xml @@ -9,7 +9,7 @@ spring-cloud-commons-dependencies - 1.3.1.BUILD-SNAPSHOT + 1.3.2.BUILD-SNAPSHOT pom spring-cloud-commons-dependencies Spring Cloud Commons Dependencies diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index bc6909d0..e4c87ae5 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.2.BUILD-SNAPSHOT .. spring-cloud-commons diff --git a/spring-cloud-context/pom.xml b/spring-cloud-context/pom.xml index de4636a1..80007b24 100644 --- a/spring-cloud-context/pom.xml +++ b/spring-cloud-context/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.2.BUILD-SNAPSHOT .. spring-cloud-context diff --git a/spring-cloud-starter/pom.xml b/spring-cloud-starter/pom.xml index bf5431d8..49f3801a 100644 --- a/spring-cloud-starter/pom.xml +++ b/spring-cloud-starter/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-commons-parent - 1.3.1.BUILD-SNAPSHOT + 1.3.2.BUILD-SNAPSHOT spring-cloud-starter spring-cloud-starter