return null for choose() rather than throw an illegal argument exception.

fixes gh-236
This commit is contained in:
Spencer Gibb
2015-03-02 09:44:11 -07:00
parent ab714bd970
commit 7b6f14805d
2 changed files with 22 additions and 9 deletions

View File

@@ -53,7 +53,11 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
@Override
public ServiceInstance choose(String serviceId) {
return new RibbonServer(serviceId, getServer(serviceId));
Server server = getServer(serviceId);
if (server == null) {
return null;
}
return new RibbonServer(serviceId, server);
}
@Override
@@ -61,7 +65,7 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
RibbonLoadBalancerContext context = this.clientFactory
.getLoadBalancerContext(serviceId);
Server server = getServer(serviceId, loadBalancer);
Server server = getServer(loadBalancer);
RibbonServer ribbonServer = new RibbonServer(serviceId, server);
ServerStats serverStats = context.getServerStats(server);
@@ -88,16 +92,14 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
}
protected Server getServer(String serviceId) {
return getServer(serviceId, getLoadBalancer(serviceId));
return getServer(getLoadBalancer(serviceId));
}
protected Server getServer(String serviceId, ILoadBalancer loadBalancer) {
Server server = loadBalancer.chooseServer("default");
if (server == null) {
throw new IllegalStateException(
"Unable to locate ILoadBalancer for service: " + serviceId);
protected Server getServer(ILoadBalancer loadBalancer) {
if (loadBalancer == null) {
return null;
}
return server;
return loadBalancer.chooseServer("default"); //TODO: better handling of key
}
protected ILoadBalancer getLoadBalancer(String serviceId) {

View File

@@ -35,6 +35,7 @@ import com.netflix.loadbalancer.ServerStats;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNull;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.anyString;
@@ -83,6 +84,16 @@ public class RibbonLoadBalancerClientTests {
assertServiceInstance(server, serviceInstance);
}
@Test
public void testChooseMissing() {
given(this.clientFactory.getLoadBalancer(this.loadBalancer.getName()))
.willReturn(null);
given(this.loadBalancer.getName()).willReturn("missingservice");
RibbonLoadBalancerClient client = new RibbonLoadBalancerClient(this.clientFactory);
ServiceInstance instance = client.choose("missingservice");
assertNull("instance wasn't null", instance);
}
@Test
public void testExecute() {
final RibbonServer server = getRibbonServer();