From a4169d451e2db697b9bbc44c5543bb1e9ee45984 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 19 Apr 2017 20:01:04 -0600 Subject: [PATCH] Only registerManagement if registration is non-null. fixes https://github.com/spring-cloud/spring-cloud-zookeeper/issues/122 --- .../AbstractAutoServiceRegistration.java | 10 +++++-- .../RetryLoadBalancerInterceptorTest.java | 1 - .../AbstractAutoServiceRegistrationTests.java | 30 +++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java index 960ad191..255db186 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java @@ -41,7 +41,10 @@ public abstract class AbstractAutoServiceRegistration ex */ @Override protected void registerManagement() { - this.serviceRegistry.register(getManagementRegistration()); + R registration = getManagementRegistration(); + if (registration != null) { + this.serviceRegistry.register(registration); + } } /** @@ -57,7 +60,10 @@ public abstract class AbstractAutoServiceRegistration ex */ @Override protected void deregisterManagement() { - this.serviceRegistry.deregister(getManagementRegistration()); + R registration = getManagementRegistration(); + if (registration != null) { + this.serviceRegistry.deregister(registration); + } } @Override 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 af508496..351fa65a 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 @@ -16,7 +16,6 @@ import org.springframework.mock.http.client.MockClientHttpResponse; import org.springframework.retry.policy.NeverRetryPolicy; import org.springframework.retry.support.RetryTemplate; -import static org.bouncycastle.crypto.tls.ConnectionEnd.client; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.any; diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java index e669c576..917d0907 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java @@ -5,8 +5,9 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.autoconfigure.LocalManagementPort; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -31,10 +32,10 @@ public class AbstractAutoServiceRegistrationTests { @Autowired private TestAutoServiceRegistration autoRegistration; - @Value("${local.server.port}") + @LocalServerPort private int port; - @Value("${local.management.port}") + @LocalManagementPort private int managementPort; @Test @@ -65,18 +66,35 @@ public class AbstractAutoServiceRegistrationTests { } } + public static class TestMgmtRegistration extends TestRegistration { + @Override + public String getServiceId() { + return "testMgmtRegistration2"; + } + } + public static class TestServiceRegistry implements ServiceRegistry { private boolean registered = false; private boolean deregistered = false; @Override public void register(TestRegistration registration) { - this.registered = true; + if (registration == null) { + throw new NullPointerException(); + } + if (!(registration instanceof TestMgmtRegistration)) { + this.registered = true; + } } @Override public void deregister(TestRegistration registration) { - this.deregistered = true; + if (registration == null) { + throw new NullPointerException(); + } + if (!(registration instanceof TestMgmtRegistration)) { + this.deregistered = true; + } } @Override @@ -131,7 +149,7 @@ public class AbstractAutoServiceRegistrationTests { @Override protected TestRegistration getRegistration() { - return null; + return new TestRegistration(); } @Override