Merge branch '1.2.x'

This commit is contained in:
Spencer Gibb
2018-03-19 12:21:21 -04:00
2 changed files with 22 additions and 3 deletions

View File

@@ -29,7 +29,6 @@ import org.apache.zookeeper.KeeperException;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies;
import org.springframework.util.ReflectionUtils;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
@@ -76,8 +75,14 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient {
instances.add(createServiceInstance(serviceIdToQuery, instance));
}
return instances;
} catch (KeeperException.NoNodeException e) {
if (log.isDebugEnabled()) {
log.debug("Error getting instances from zookeeper. Possibly, no service has registered.", e);
}
// this means that nothing has registered as a service yes
return Collections.emptyList();
} catch (Exception exception) {
ReflectionUtils.rethrowRuntimeException(exception);
rethrowRuntimeException(exception);
}
return new ArrayList<>();
}

View File

@@ -6,6 +6,8 @@ import org.apache.curator.x.discovery.ServiceDiscovery;
import org.apache.zookeeper.KeeperException.NoNodeException;
import org.junit.Test;
import org.springframework.cloud.client.ServiceInstance;
import static org.assertj.core.api.BDDAssertions.then;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -26,7 +28,7 @@ public class ZookeeperDiscoveryClientTests {
}
@Test
public void shouldReturnEmptyWhenNoNodeException() throws Exception {
public void getServicesShouldReturnEmptyWhenNoNodeException() throws Exception {
// given:
ServiceDiscovery<ZookeeperInstance> serviceDiscovery = mock(ServiceDiscovery.class);
when(serviceDiscovery.queryForNames()).thenThrow(new NoNodeException());
@@ -36,4 +38,16 @@ public class ZookeeperDiscoveryClientTests {
// then:
then(services).isEmpty();
}
@Test
public void getInstancesshouldReturnEmptyWhenNoNodeException() throws Exception {
// given:
ServiceDiscovery<ZookeeperInstance> serviceDiscovery = mock(ServiceDiscovery.class);
when(serviceDiscovery.queryForInstances("myservice")).thenThrow(new NoNodeException());
ZookeeperDiscoveryClient discoveryClient = new ZookeeperDiscoveryClient(serviceDiscovery, null);
// when:
List<ServiceInstance> instances = discoveryClient.getInstances("myservice");
// then:
then(instances).isEmpty();
}
}