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


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/7280550a
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/7280550a
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/7280550a

Branch: refs/heads/HDFS-7240
Commit: 7280550a8f668df8aa32e4630db4ead49e9b8b6d
Parents: 89c9347
Author: Arpit Agarwal <a...@apache.org>
Authored: Mon Apr 4 10:50:11 2016 -0700
Committer: Arpit Agarwal <a...@apache.org>
Committed: Mon Apr 4 10:50:11 2016 -0700

----------------------------------------------------------------------
 .../java/org/apache/hadoop/net/NetUtils.java    | 15 +++++--
 .../org/apache/hadoop/net/TestNetUtils.java     | 47 +++++++++++---------
 2 files changed, 38 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/7280550a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
index 2c3661a..4050107 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java
@@ -782,12 +782,21 @@ 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: "
-                                               + exception
-                                               + "; Host Details : "
-                                               + 
getHostDetailsAsString(destHost, destPort, localHost))
+             + exception
+             + "; Host Details : "
+             + getHostDetailsAsString(destHost, destPort, localHost))
           .initCause(exception);
 
     }

http://git-wip-us.apache.org/repos/asf/hadoop/blob/7280550a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
index c93ede8..e59ac77 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/net/TestNetUtils.java
@@ -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,15 +188,11 @@ 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");
-    }
+    NetUtils.verifyHostnames(names);
   }
-  
+
   /** 
    * Test for {@link NetUtils#isLocalAddress(java.net.InetAddress)}
    */
@@ -267,7 +263,18 @@ public class TestNetUtils {
     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 @@ public class TestNetUtils {
     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 @@ public class TestNetUtils {
     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 @@ 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 "
-                                         + expectedClass
-                                         + " got " + wrapped.getClass() + ": " 
+ wrapped).initCause(wrapped);
+         + expectedClass
+         + " got " + wrapped.getClass() + ": " + wrapped).initCause(wrapped);
     }
     return wrapped;
   }

Reply via email to