29
29
import static org .junit .Assert .fail ;
30
30
31
31
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 ;
32
36
private static final int ONE_MB = 1024 * 1024 ;
33
37
34
38
@ Test public void readWithoutTimeout () throws Exception {
@@ -88,12 +92,14 @@ public class SocketTimeoutTest {
88
92
static Socket socket (final int readableByteCount , final int writableByteCount ) throws IOException {
89
93
final ServerSocket serverSocket = new ServerSocket (0 );
90
94
serverSocket .setReuseAddress (true );
95
+ serverSocket .setReceiveBufferSize (SOCKET_BUFFER_SIZE );
91
96
92
97
Thread peer = new Thread ("peer" ) {
93
98
@ Override public void run () {
94
99
Socket socket = null ;
95
100
try {
96
101
socket = serverSocket .accept ();
102
+ socket .setSendBufferSize (SOCKET_BUFFER_SIZE );
97
103
writeFully (socket .getOutputStream (), readableByteCount );
98
104
readFully (socket .getInputStream (), writableByteCount );
99
105
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
108
114
};
109
115
peer .start ();
110
116
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 ;
112
121
}
113
122
114
123
private static void writeFully (OutputStream out , int byteCount ) throws IOException {
0 commit comments