Skip to content

Commit

Permalink
Fix socket timeout test failures.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
enh-google committed Oct 23, 2011
1 parent 6435031 commit 04e25f3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 98 deletions.
41 changes: 16 additions & 25 deletions luni/src/test/java/libcore/java/net/OldDatagramSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -769,18 +769,24 @@ public void test_getSendBufferSize() throws Exception {
}
}

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) {
}
}

Expand Down Expand Up @@ -1133,21 +1139,6 @@ public void test_setReceiveBufferSizeI() throws Exception {
}
}

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){
Expand Down
27 changes: 13 additions & 14 deletions luni/src/test/java/libcore/java/net/OldServerSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,24 @@ public void test_accept() throws IOException {
}
}

public void test_getSoTimeout() throws IOException {
ServerSocket newSocket = new ServerSocket();
newSocket.close();
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);
}
s.close();
try {
newSocket.setSoTimeout(100);
s.getSoTimeout();
fail("SocketException was not thrown.");
} catch(SocketException e) {
//expected
} catch (SocketException expected) {
}
}

public void test_setSoTimeoutI() throws IOException {
ServerSocket newSocket = new ServerSocket();
newSocket.close();
try {
newSocket.setSoTimeout(100);
s.setSoTimeout(1000);
fail("SocketException was not thrown.");
} catch(SocketException se) {
//expected
} catch (SocketException expected) {
}
}

Expand Down
84 changes: 25 additions & 59 deletions luni/src/test/java/libcore/java/net/OldSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,30 +405,24 @@ public void test_getSendBufferSize() {
}
}

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) {
}
}

Expand Down Expand Up @@ -586,31 +580,6 @@ public void test_setSoLingerZI() {
}
}

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 {
Expand Down Expand Up @@ -1233,9 +1202,9 @@ public SocketCloser(int timeout, Socket theSocket) {
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 {
Expand Down Expand Up @@ -1532,17 +1501,14 @@ public SocketConnector(int timeout, Socket theSocket,

// 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();

Expand All @@ -1554,9 +1520,9 @@ public SocketConnector(int timeout, Socket theSocket,
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 {
Expand Down

0 comments on commit 04e25f3

Please sign in to comment.