Throw IOException in RibbonLoadBalancerClient.execute

Fixes proper RestTemplate behavior. fixes gh-986
parent 2b409524
......@@ -16,6 +16,7 @@
package org.springframework.cloud.netflix.ribbon;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
......@@ -71,7 +72,7 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
}
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) {
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
Server server = getServer(loadBalancer);
if (server == null) {
......@@ -89,6 +90,11 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
statsRecorder.recordStats(returnVal);
return returnVal;
}
// catch IOException and rethrow so RestTemplate behaves correctly
catch (IOException ex) {
statsRecorder.recordStats(ex);
throw ex;
}
catch (Exception ex) {
statsRecorder.recordStats(ex);
ReflectionUtils.rethrowRuntimeException(ex);
......
......@@ -16,6 +16,7 @@
package org.springframework.cloud.netflix.ribbon;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.Collections;
......@@ -38,9 +39,12 @@ import com.netflix.loadbalancer.ServerStats;
import lombok.SneakyThrows;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyDouble;
......@@ -139,7 +143,7 @@ public class RibbonLoadBalancerClientTests {
}
@Test
public void testExecute() {
public void testExecute() throws IOException {
final RibbonServer server = getRibbonServer();
RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(server);
final String returnVal = "myval";
......@@ -176,6 +180,27 @@ public class RibbonLoadBalancerClientTests {
verifyServerStats();
}
@Test
public void testExecuteIOException() {
final RibbonServer ribbonServer = getRibbonServer();
RibbonLoadBalancerClient client = getRibbonLoadBalancerClient(ribbonServer);
try {
client.execute(ribbonServer.getServiceId(),
new LoadBalancerRequest<Object>() {
@Override
public Object apply(ServiceInstance instance) throws Exception {
assertServiceInstance(ribbonServer, instance);
throw new IOException();
}
});
fail("Should have thrown exception");
}
catch (Exception ex) {
assertThat("wrong exception type", ex, is(instanceOf(IOException.class)));
}
verifyServerStats();
}
protected RibbonServer getRibbonServer() {
return new RibbonServer("testService", new Server("myhost", 9080), false,
Collections.singletonMap("mykey", "myvalue"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment