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 89c9347511
commit 7280550a8f
2 changed files with 38 additions and 24 deletions

View File

@ -782,12 +782,21 @@ public static IOException wrapException(final String destHost,
+ ": " + 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: "
+ exception
+ "; Host Details : "
+ getHostDetailsAsString(destHost, destPort, localHost))
+ exception
+ "; Host Details : "
+ getHostDetailsAsString(destHost, destPort, localHost))
.initCause(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 void testAvoidLoopbackTcpSockets() throws Exception {
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,15 +188,11 @@ public void testVerifyHostnamesException() throws UnknownHostException {
}
@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");
}
NetUtils.verifyHostnames(names);
}
/**
* Test for {@link NetUtils#isLocalAddress(java.net.InetAddress)}
*/
@ -267,7 +263,18 @@ public void testWrapEOFException() throws Throwable {
assertRemoteDetailsIncluded(wrapped);
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");
@ -322,8 +329,8 @@ private void assertInException(Exception e, String text) throws Throwable {
String message = extractExceptionMessage(e);
if (!(message.contains(text))) {
throw new AssertionFailedError("Wrong text in message "
+ "\"" + message + "\""
+ " expected \"" + text + "\"")
+ "\"" + message + "\""
+ " expected \"" + text + "\"")
.initCause(e);
}
}
@ -343,8 +350,8 @@ private void assertNotInException(Exception e, String text)
String message = extractExceptionMessage(e);
if (message.contains(text)) {
throw new AssertionFailedError("Wrong text in message "
+ "\"" + message + "\""
+ " did not expect \"" + text + "\"")
+ "\"" + message + "\""
+ " did not expect \"" + text + "\"")
.initCause(e);
}
}
@ -353,15 +360,13 @@ private IOException verifyExceptionClass(IOException e,
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 "
+ expectedClass
+ " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped);
+ expectedClass
+ " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped);
}
return wrapped;
}