HADOOP-11212. NetUtils.wrapException to handle SocketException explicitly. (Contributed by Steve Loughran)

This commit is contained in:
Arpit Agarwal 2016-04-04 10:50:11 -07:00
parent caaadf9f3e
commit 30e12edb67
2 changed files with 38 additions and 24 deletions

View File

@ -782,6 +782,15 @@ public class NetUtils {
+ ": " + exception
+ ";"
+ see("EOFException"));
} else if (exception instanceof SocketException) {
// Many of the predecessor exceptions are subclasses of SocketException,
// so must be handled before this
return wrapWithMessage(exception,
"Call From "
+ localHost + " to " + destHost + ":" + destPort
+ " failed on socket exception: " + exception
+ ";"
+ see("SocketException"));
}
else {
return (IOException) new IOException("Failed on local exception: "

View File

@ -72,7 +72,7 @@ public class TestNetUtils {
* This is a regression test for HADOOP-6722.
*/
@Test
public void testAvoidLoopbackTcpSockets() throws Exception {
public void testAvoidLoopbackTcpSockets() throws Throwable {
Configuration conf = new Configuration();
Socket socket = NetUtils.getDefaultSocketFactory(conf)
@ -88,11 +88,11 @@ public class TestNetUtils {
fail("Should not have connected");
} catch (ConnectException ce) {
System.err.println("Got exception: " + ce);
assertTrue(ce.getMessage().contains("resulted in a loopback"));
assertInException(ce, "resulted in a loopback");
} catch (SocketException se) {
// Some TCP stacks will actually throw their own Invalid argument exception
// here. This is also OK.
assertTrue(se.getMessage().contains("Invalid argument"));
assertInException(se, "Invalid argument");
}
}
@ -188,13 +188,9 @@ public class TestNetUtils {
}
@Test
public void testVerifyHostnamesNoException() {
public void testVerifyHostnamesNoException() throws UnknownHostException {
String[] names = {"valid.host.com", "1.com"};
try {
NetUtils.verifyHostnames(names);
} catch (UnknownHostException e) {
fail("NetUtils.verifyHostnames threw unexpected UnknownHostException");
}
}
/**
@ -268,6 +264,17 @@ public class TestNetUtils {
assertInException(wrapped, "/EOFException");
}
@Test
public void testWrapSocketException() throws Throwable {
IOException wrapped = verifyExceptionClass(new SocketException("failed"),
SocketException.class);
assertInException(wrapped, "failed");
assertWikified(wrapped);
assertInException(wrapped, "localhost");
assertRemoteDetailsIncluded(wrapped);
assertInException(wrapped, "/SocketException");
}
@Test
public void testGetConnectAddress() throws IOException {
NetUtils.addStaticResolution("host", "127.0.0.1");
@ -353,10 +360,8 @@ public class TestNetUtils {
Class expectedClass)
throws Throwable {
assertNotNull("Null Exception", e);
IOException wrapped =
NetUtils.wrapException("desthost", DEST_PORT,
"localhost", LOCAL_PORT,
e);
IOException wrapped = NetUtils.wrapException("desthost", DEST_PORT,
"localhost", LOCAL_PORT, e);
LOG.info(wrapped.toString(), wrapped);
if(!(wrapped.getClass().equals(expectedClass))) {
throw new AssertionFailedError("Wrong exception class; expected "