diff --git a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java index 250ab800..de4e8668 100644 --- a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java +++ b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java @@ -16,23 +16,25 @@ package org.springframework.cloud.zookeeper.discovery; -import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; import org.apache.curator.x.discovery.ServiceInstance; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies; -import lombok.SneakyThrows; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; /** * @author Spencer Gibb * @author Marcin Grzejszczak, 4financeIT */ +@Slf4j public class ZookeeperDiscoveryClient implements DiscoveryClient { private ZookeeperServiceDiscovery serviceDiscovery; @@ -93,7 +95,11 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient { @Override public List getServices() { - ArrayList services = null; + List services = null; + if (this.serviceDiscovery.getServiceDiscovery() == null) { + log.warn("Service Discovery is not yet ready - returning empty list of services"); + return Collections.emptyList(); + } try { services = new ArrayList<>(this.serviceDiscovery.getServiceDiscovery().queryForNames()); } diff --git a/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClientSpec.groovy b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClientSpec.groovy new file mode 100644 index 00000000..91925063 --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/test/groovy/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClientSpec.groovy @@ -0,0 +1,20 @@ +package org.springframework.cloud.zookeeper.discovery + +import spock.lang.Specification + +/** + * @author Marcin Grzejszczak + */ +class ZookeeperDiscoveryClientSpec extends Specification { + + def "should return an empty list of services if service discovery is null"() { + given: + ZookeeperServiceDiscovery serviceDiscovery = Stub() + ZookeeperDiscoveryClient zookeeperDiscoveryClient = new ZookeeperDiscoveryClient(serviceDiscovery, null) + when: + List services = zookeeperDiscoveryClient.getServices() + then: + assert services.empty + } +} +