Skip to content

Commit 47bcc87

Browse files
committed
Merge pull request square#86 from nfuller/FixTestOnAndroid2.1
Make tests pass on Android and other platforms
2 parents 03341ed + e332533 commit 47bcc87

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)