Fix socket timeout test failures.

The kernel uses awkward device-specific units that make tests with timeouts
that aren't integer multiples of a second awkward.

There's no common superclass for the various sockets, so we can't factor
out the test even though -- apart from the creation of the specific socket --
it's textually identical in each case. Nice library design, boys!

I've also fixed a couple of CloseGuard warnings in passing.

Bug: 5159133
Change-Id: Iaf4c9891b3cf2f5b3ebcee5cb2ba862157ab034e
diff --git a/luni/src/test/java/libcore/java/net/OldDatagramSocketTest.java b/luni/src/test/java/libcore/java/net/OldDatagramSocketTest.java
index 0a27fc7..666a503 100644
--- a/luni/src/test/java/libcore/java/net/OldDatagramSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/OldDatagramSocketTest.java
@@ -769,18 +769,24 @@
         }
     }
 
-    public void test_getSoTimeout() throws Exception {
-        // Test for method int java.net.DatagramSocket.getSoTimeout()
-        int portNumber = Support_PortManager.getNextPortForUDP();
-        ds = new java.net.DatagramSocket(portNumber);
-        ds.setSoTimeout(100);
-        assertEquals("Returned incorrect timeout", 100, ds.getSoTimeout());
-        ds.close();
+    public void test_getSoTimeout_setSoTimeout() throws Exception {
+        // TODO: a useful test would check that setSoTimeout actually causes timeouts!
+        DatagramSocket s = new DatagramSocket();
+        s.setSoTimeout(1500);
+        int ms = s.getSoTimeout();
+        if (ms < 1500-10 || ms > 1500+10) {
+            fail("suspicious timeout: " + ms);
+        }
+        s.close();
         try {
-            ds.getSoTimeout();
+            s.getSoTimeout();
             fail("SocketException was not thrown.");
-        } catch(SocketException se) {
-            //expected
+        } catch (SocketException expected) {
+        }
+        try {
+            s.setSoTimeout(1000);
+            fail("SocketException was not thrown.");
+        } catch (SocketException expected) {
         }
     }
 
@@ -1133,21 +1139,6 @@
         }
     }
 
-    public void test_setSoTimeoutI() throws Exception {
-        // Test for method void java.net.DatagramSocket.setSoTimeout(int)
-        int portNumber = Support_PortManager.getNextPortForUDP();
-        ds = new java.net.DatagramSocket(portNumber);
-        ds.setSoTimeout(5000);
-        assertTrue("Set incorrect timeout", ds.getSoTimeout() >= 5000);
-        ds.close();
-        try {
-            ds.setSoTimeout(100);
-            fail("SocketException was not thrown.");
-        } catch(SocketException se) {
-            //expected
-        }
-    }
-
     public void test_ConstructorLjava_net_DatagramSocketImpl() {
         class testDatagramSocket extends DatagramSocket {
             public testDatagramSocket(DatagramSocketImpl impl){
diff --git a/luni/src/test/java/libcore/java/net/OldServerSocketTest.java b/luni/src/test/java/libcore/java/net/OldServerSocketTest.java
index af47a5a..cf35489 100644
--- a/luni/src/test/java/libcore/java/net/OldServerSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/OldServerSocketTest.java
@@ -242,25 +242,24 @@
         }
     }
 
-    public void test_getSoTimeout() throws IOException {
-        ServerSocket newSocket = new ServerSocket();
-        newSocket.close();
-        try {
-            newSocket.setSoTimeout(100);
-            fail("SocketException was not thrown.");
-        } catch(SocketException e) {
-            //expected
+    public void test_getSoTimeout_setSoTimeout() throws Exception {
+        // TODO: a useful test would check that setSoTimeout actually causes timeouts!
+        ServerSocket s = new ServerSocket();
+        s.setSoTimeout(1500);
+        int ms = s.getSoTimeout();
+        if (ms < 1500-10 || ms > 1500+10) {
+            fail("suspicious timeout: " + ms);
         }
-    }
-
-    public void test_setSoTimeoutI() throws IOException {
-        ServerSocket newSocket = new ServerSocket();
-        newSocket.close();
+        s.close();
         try {
-            newSocket.setSoTimeout(100);
+            s.getSoTimeout();
             fail("SocketException was not thrown.");
-        } catch(SocketException se) {
-            //expected
+        } catch (SocketException expected) {
+        }
+        try {
+            s.setSoTimeout(1000);
+            fail("SocketException was not thrown.");
+        } catch (SocketException expected) {
         }
     }
 
diff --git a/luni/src/test/java/libcore/java/net/OldSocketTest.java b/luni/src/test/java/libcore/java/net/OldSocketTest.java
index 42f7b78..fda9557 100644
--- a/luni/src/test/java/libcore/java/net/OldSocketTest.java
+++ b/luni/src/test/java/libcore/java/net/OldSocketTest.java
@@ -405,30 +405,24 @@
         }
     }
 
-    public void test_getSoTimeout() {
-        // Test for method int java.net.Socket.getSoTimeout()
-        int sport = startServer("SServer getSoTimeout");
-        try {
-            s = new Socket(InetAddress.getLocalHost(), sport);
-            s.setSoTimeout(100);
-            assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout());
-            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
-        } catch (Exception e) {
-            handleException(e, SO_TIMEOUT);
+    public void test_getSoTimeout_setSoTimeout() throws Exception {
+        // TODO: a useful test would check that setSoTimeout actually causes timeouts!
+        Socket s = new Socket();
+        s.setSoTimeout(1500);
+        int ms = s.getSoTimeout();
+        if (ms < 1500-10 || ms > 1500+10) {
+            fail("suspicious timeout: " + ms);
         }
-
+        s.close();
         try {
-            int portNumber = Support_PortManager.getNextPort();
-            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
-            s.close();
-            try {
-                s.getSoTimeout();
-                fail("SocketException was not thrown.");
-            } catch(SocketException ioe) {
-                //expected
-            }
-        } catch(Exception e) {
-            fail("Unexpected exception was thrown: " + e.toString());
+            s.getSoTimeout();
+            fail("SocketException was not thrown.");
+        } catch (SocketException expected) {
+        }
+        try {
+            s.setSoTimeout(1000);
+            fail("SocketException was not thrown.");
+        } catch (SocketException expected) {
         }
     }
 
@@ -586,31 +580,6 @@
         }
     }
 
-    public void test_setSoTimeoutI() {
-        // Test for method void java.net.Socket.setSoTimeout(int)
-        try {
-            int sport = startServer("SServer seSoTimeoutI");
-            int portNumber = Support_PortManager.getNextPort();
-            s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
-            s.setSoTimeout(100);
-            assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout());
-            ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
-        } catch (Exception e) {
-            handleException(e, SO_TIMEOUT);
-        }
-
-        try {
-            Socket theSocket = new Socket();
-            theSocket.close();
-            theSocket.setSoTimeout(1);
-            fail("SocketException was not thrown.");
-        } catch(SocketException ioe) {
-            //expected
-        } catch(IOException ioe) {
-            fail("IOException was thrown.");
-        }
-    }
-
     public void test_setTcpNoDelayZ() {
         // Test for method void java.net.Socket.setTcpNoDelay(boolean)
         try {
@@ -1233,9 +1202,9 @@
             socket.connect( new InetSocketAddress(InetAddress.getLocalHost(),
                     Support_PortManager.getNextPort()));
             fail("IllegalBlockingModeException was not thrown.");
-        } catch(IllegalBlockingModeException ibme) {
-            //expected
+        } catch (IllegalBlockingModeException expected) {
         }
+        socket.close();
     }
 
     public void test_connectLjava_net_SocketAddressI() throws Exception {
@@ -1532,17 +1501,14 @@
 
         // now try to set options while we are connecting
         theSocket = new Socket();
-        SocketConnector connector = new SocketConnector(5000, theSocket,
-                nonReachableAddress);
+        SocketConnector connector = new SocketConnector(5000, theSocket, nonReachableAddress);
         connector.start();
-        theSocket.setSoTimeout(100);
+        theSocket.setSoTimeout(1000);
         Thread.sleep(10);
-        assertEquals("Socket option not set during connect: 10 ", 100,
-                theSocket.getSoTimeout());
+        assertEquals("Socket option not set during connect: 10 ", 1000, theSocket.getSoTimeout());
         Thread.sleep(50);
-        theSocket.setSoTimeout(200);
-        assertEquals("Socket option not set during connect: 50 ", 200,
-                theSocket.getSoTimeout());
+        theSocket.setSoTimeout(2000);
+        assertEquals("Socket option not set during connect: 50 ", 2000, theSocket.getSoTimeout());
         Thread.sleep(5000);
         theSocket.close();
 
@@ -1554,9 +1520,9 @@
             socket.connect( new InetSocketAddress(InetAddress.getLocalHost(),
                     Support_PortManager.getNextPort()), port);
             fail("IllegalBlockingModeException was not thrown.");
-        } catch(IllegalBlockingModeException ibme) {
-            //expected
+        } catch (IllegalBlockingModeException expected) {
         }
+        channel.close();
     }
 
     public void test_isInputShutdown() throws IOException {