Skip to content

Commit e332533

Browse files
committed
Make tests pass on Android and other platforms
Previously failing with: okio.SocketTimeoutTest#writeWithTimeout junit.framework.AssertionFailedError at junit.framework.Assert.fail(Assert.java:56) at okio.SocketTimeoutTest.writeWithTimeout(SocketTimeoutTest.java:75) The problem: the 1MB of data can be written to the client socket even though the server socket is not reading. This is because sockets on Android are buffered by default by more that the amount of data in the test. This prevents the write timeout occurring. Socket defaults measured on a Nexus 4 running AOSP: send: 524288 bytes, receive: 1048576 bytes. IIRC, it varies by device. Only some of the buffers need to be set to fix this, but setting all of them seems reasonable to make it explicit. The buffers are set to 1/4 of the data being transferred to ensure that the data will flood the send and receive buffers with some to spare. Too small and the "withoutTimeout" tests will timeout due to inefficiency. The tests were also failing on a Linux desktop, probably for similar reasons.
1 parent 03341ed commit e332533

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

okio/src/test/java/okio/SocketTimeoutTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import static org.junit.Assert.fail;
3030

3131
public class SocketTimeoutTest {
32+
33+
// The size of the socket buffers to use. Less than half the data transferred during tests to
34+
// ensure send and receive buffers are flooded and any necessary blocking behavior takes place.
35+
private static final int SOCKET_BUFFER_SIZE = 256 * 1024;
3236
private static final int ONE_MB = 1024 * 1024;
3337

3438
@Test public void readWithoutTimeout() throws Exception {
@@ -88,12 +92,14 @@ public class SocketTimeoutTest {
8892
static Socket socket(final int readableByteCount, final int writableByteCount) throws IOException {
8993
final ServerSocket serverSocket = new ServerSocket(0);
9094
serverSocket.setReuseAddress(true);
95+
serverSocket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
9196

9297
Thread peer = new Thread("peer") {
9398
@Override public void run() {
9499
Socket socket = null;
95100
try {
96101
socket = serverSocket.accept();
102+
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
97103
writeFully(socket.getOutputStream(), readableByteCount);
98104
readFully(socket.getInputStream(), writableByteCount);
99105
Thread.sleep(5000); // Sleep 5 seconds so the peer can close the connection.
@@ -108,7 +114,10 @@ static Socket socket(final int readableByteCount, final int writableByteCount) t
108114
};
109115
peer.start();
110116

111-
return new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
117+
Socket socket = new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort());
118+
socket.setReceiveBufferSize(SOCKET_BUFFER_SIZE);
119+
socket.setSendBufferSize(SOCKET_BUFFER_SIZE);
120+
return socket;
112121
}
113122

114123
private static void writeFully(OutputStream out, int byteCount) throws IOException {

0 commit comments

Comments
 (0)