57207
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
57226
|
24 |
/*
|
57207
|
25 |
* @test
|
|
26 |
* @library /test/lib
|
|
27 |
* @build jdk.test.lib.Utils
|
|
28 |
* @run testng Timeouts
|
|
29 |
* @run testng/othervm -Djdk.net.usePlainSocketImpl Timeouts
|
|
30 |
* @summary Test Socket timeouts
|
|
31 |
*/
|
|
32 |
|
|
33 |
import java.io.Closeable;
|
|
34 |
import java.io.IOException;
|
57210
|
35 |
import java.io.InputStream;
|
|
36 |
import java.io.OutputStream;
|
57207
|
37 |
import java.net.ConnectException;
|
|
38 |
import java.net.ServerSocket;
|
|
39 |
import java.net.Socket;
|
|
40 |
import java.net.SocketAddress;
|
|
41 |
import java.net.SocketException;
|
|
42 |
import java.net.SocketTimeoutException;
|
|
43 |
import java.util.concurrent.Executors;
|
|
44 |
import java.util.concurrent.ScheduledExecutorService;
|
|
45 |
import java.util.concurrent.TimeUnit;
|
|
46 |
|
|
47 |
import org.testng.annotations.Test;
|
|
48 |
import static org.testng.Assert.*;
|
|
49 |
import jdk.test.lib.Utils;
|
|
50 |
|
|
51 |
@Test
|
|
52 |
public class Timeouts {
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Test timed connect where connection is established
|
|
56 |
*/
|
|
57 |
public void testTimedConnect1() throws IOException {
|
|
58 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
59 |
try (Socket s = new Socket()) {
|
57210
|
60 |
s.connect(ss.getLocalSocketAddress(), 2000);
|
57207
|
61 |
}
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
/**
|
|
66 |
* Test timed connect where connection is refused
|
|
67 |
*/
|
|
68 |
public void testTimedConnect2() throws IOException {
|
|
69 |
try (Socket s = new Socket()) {
|
|
70 |
SocketAddress remote = Utils.refusingEndpoint();
|
|
71 |
try {
|
|
72 |
s.connect(remote, 2000);
|
|
73 |
} catch (ConnectException expected) { }
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
/**
|
57226
|
78 |
* Test connect with a timeout of Integer.MAX_VALUE
|
|
79 |
*/
|
|
80 |
public void testTimedConnect3() throws IOException {
|
|
81 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
82 |
try (Socket s = new Socket()) {
|
|
83 |
s.connect(ss.getLocalSocketAddress(), Integer.MAX_VALUE);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Test connect with a negative timeout. This case is not currently specified
|
|
90 |
* but the long standing behavior is to throw IllegalArgumentException.
|
|
91 |
*/
|
|
92 |
public void testTimedConnect4() throws IOException {
|
|
93 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
94 |
try (Socket s = new Socket()) {
|
|
95 |
try {
|
|
96 |
s.connect(ss.getLocalSocketAddress(), -1);
|
|
97 |
assertTrue(false);
|
|
98 |
} catch (IllegalArgumentException expected) { }
|
|
99 |
}
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
57210
|
104 |
* Test timed read where the read succeeds immediately
|
57207
|
105 |
*/
|
|
106 |
public void testTimedRead1() throws IOException {
|
|
107 |
withConnection((s1, s2) -> {
|
|
108 |
s1.getOutputStream().write(99);
|
57210
|
109 |
s2.setSoTimeout(30*1000);
|
|
110 |
int b = s2.getInputStream().read();
|
|
111 |
assertTrue(b == 99);
|
|
112 |
});
|
|
113 |
}
|
|
114 |
|
|
115 |
/**
|
|
116 |
* Test timed read where the read succeeds after a delay
|
|
117 |
*/
|
|
118 |
public void testTimedRead2() throws IOException {
|
|
119 |
withConnection((s1, s2) -> {
|
|
120 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
121 |
s2.setSoTimeout(30*1000);
|
57207
|
122 |
int b = s2.getInputStream().read();
|
|
123 |
assertTrue(b == 99);
|
|
124 |
});
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* Test timed read where the read times out
|
|
129 |
*/
|
57210
|
130 |
public void testTimedRead3() throws IOException {
|
57207
|
131 |
withConnection((s1, s2) -> {
|
57210
|
132 |
s2.setSoTimeout(2000);
|
57207
|
133 |
try {
|
|
134 |
s2.getInputStream().read();
|
|
135 |
assertTrue(false);
|
|
136 |
} catch (SocketTimeoutException expected) { }
|
|
137 |
});
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Test timed read that succeeds after a previous read has timed out
|
|
142 |
*/
|
57210
|
143 |
public void testTimedRead4() throws IOException {
|
57207
|
144 |
withConnection((s1, s2) -> {
|
57210
|
145 |
s2.setSoTimeout(2000);
|
57207
|
146 |
try {
|
|
147 |
s2.getInputStream().read();
|
|
148 |
assertTrue(false);
|
|
149 |
} catch (SocketTimeoutException e) { }
|
|
150 |
s1.getOutputStream().write(99);
|
|
151 |
int b = s2.getInputStream().read();
|
|
152 |
assertTrue(b == 99);
|
|
153 |
});
|
|
154 |
}
|
|
155 |
|
|
156 |
/**
|
57210
|
157 |
* Test timed read that succeeds after a previous read has timed out and
|
|
158 |
* after a short delay
|
57207
|
159 |
*/
|
57210
|
160 |
public void testTimedRead5() throws IOException {
|
57207
|
161 |
withConnection((s1, s2) -> {
|
57210
|
162 |
s2.setSoTimeout(2000);
|
|
163 |
try {
|
|
164 |
s2.getInputStream().read();
|
|
165 |
assertTrue(false);
|
|
166 |
} catch (SocketTimeoutException e) { }
|
|
167 |
s2.setSoTimeout(30*3000);
|
|
168 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
169 |
int b = s2.getInputStream().read();
|
|
170 |
assertTrue(b == 99);
|
|
171 |
});
|
|
172 |
}
|
|
173 |
|
|
174 |
/**
|
|
175 |
* Test untimed read that succeeds after a previous read has timed out
|
|
176 |
*/
|
|
177 |
public void testTimedRead6() throws IOException {
|
|
178 |
withConnection((s1, s2) -> {
|
|
179 |
s2.setSoTimeout(2000);
|
57207
|
180 |
try {
|
|
181 |
s2.getInputStream().read();
|
|
182 |
assertTrue(false);
|
|
183 |
} catch (SocketTimeoutException e) { }
|
|
184 |
s1.getOutputStream().write(99);
|
|
185 |
s2.setSoTimeout(0);
|
|
186 |
int b = s2.getInputStream().read();
|
|
187 |
assertTrue(b == 99);
|
|
188 |
});
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
57210
|
192 |
* Test untimed read that succeeds after a previous read has timed out and
|
|
193 |
* after a short delay
|
|
194 |
*/
|
|
195 |
public void testTimedRead7() throws IOException {
|
|
196 |
withConnection((s1, s2) -> {
|
|
197 |
s2.setSoTimeout(2000);
|
|
198 |
try {
|
|
199 |
s2.getInputStream().read();
|
|
200 |
assertTrue(false);
|
|
201 |
} catch (SocketTimeoutException e) { }
|
|
202 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
203 |
s2.setSoTimeout(0);
|
|
204 |
int b = s2.getInputStream().read();
|
|
205 |
assertTrue(b == 99);
|
|
206 |
});
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
57207
|
210 |
* Test async close of timed read
|
|
211 |
*/
|
57210
|
212 |
public void testTimedRead8() throws IOException {
|
57207
|
213 |
withConnection((s1, s2) -> {
|
57210
|
214 |
s2.setSoTimeout(30*1000);
|
57207
|
215 |
scheduleClose(s2, 2000);
|
|
216 |
try {
|
|
217 |
s2.getInputStream().read();
|
|
218 |
assertTrue(false);
|
|
219 |
} catch (SocketException expected) { }
|
|
220 |
});
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
57226
|
224 |
* Test read with a timeout of Integer.MAX_VALUE
|
|
225 |
*/
|
|
226 |
public void testTimedRead9() throws IOException {
|
|
227 |
withConnection((s1, s2) -> {
|
|
228 |
scheduleWrite(s1.getOutputStream(), 99, 2000);
|
|
229 |
s2.setSoTimeout(Integer.MAX_VALUE);
|
|
230 |
int b = s2.getInputStream().read();
|
|
231 |
assertTrue(b == 99);
|
|
232 |
});
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
57207
|
236 |
* Test writing after a timed read. The timed read changes the underlying
|
|
237 |
* socket to non-blocking.
|
|
238 |
*/
|
|
239 |
public void testTimedWrite1() throws IOException {
|
|
240 |
withConnection((s1, s2) -> {
|
|
241 |
s1.getOutputStream().write(99);
|
|
242 |
s2.setSoTimeout(3000);
|
|
243 |
int b = s2.getInputStream().read();
|
|
244 |
assertTrue(b == 99);
|
|
245 |
|
|
246 |
// schedule thread to read s1 to EOF
|
57210
|
247 |
scheduleReadToEOF(s1.getInputStream(), 3000);
|
57207
|
248 |
|
|
249 |
// write a lot so that write blocks
|
|
250 |
byte[] data = new byte[128*1024];
|
|
251 |
for (int i = 0; i < 100; i++) {
|
|
252 |
s2.getOutputStream().write(data);
|
|
253 |
}
|
|
254 |
});
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Test async close of writer (after a timed read). The timed read changes
|
|
259 |
* the underlying socket to non-blocking.
|
|
260 |
*/
|
|
261 |
public void testTimedWrite2() throws IOException {
|
|
262 |
withConnection((s1, s2) -> {
|
|
263 |
s1.getOutputStream().write(99);
|
|
264 |
s2.setSoTimeout(3000);
|
|
265 |
int b = s2.getInputStream().read();
|
|
266 |
assertTrue(b == 99);
|
|
267 |
|
|
268 |
// schedule s2 to be be closed
|
|
269 |
scheduleClose(s2, 3000);
|
|
270 |
|
|
271 |
// write a lot so that write blocks
|
|
272 |
byte[] data = new byte[128*1024];
|
|
273 |
try {
|
|
274 |
while (true) {
|
|
275 |
s2.getOutputStream().write(data);
|
|
276 |
}
|
|
277 |
} catch (SocketException expected) { }
|
|
278 |
});
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
57210
|
282 |
* Test timed accept where a connection is established immediately
|
57207
|
283 |
*/
|
|
284 |
public void testTimedAccept1() throws IOException {
|
|
285 |
Socket s1 = null;
|
|
286 |
Socket s2 = null;
|
|
287 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
288 |
s1 = new Socket();
|
|
289 |
s1.connect(ss.getLocalSocketAddress());
|
57210
|
290 |
ss.setSoTimeout(30*1000);
|
57207
|
291 |
s2 = ss.accept();
|
|
292 |
} finally {
|
|
293 |
if (s1 != null) s1.close();
|
|
294 |
if (s2 != null) s2.close();
|
|
295 |
}
|
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
57210
|
299 |
* Test timed accept where a connection is established after a short delay
|
57207
|
300 |
*/
|
|
301 |
public void testTimedAccept2() throws IOException {
|
|
302 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57210
|
303 |
ss.setSoTimeout(30*1000);
|
|
304 |
scheduleConnect(ss.getLocalSocketAddress(), 2000);
|
|
305 |
Socket s = ss.accept();
|
|
306 |
s.close();
|
|
307 |
}
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* Test timed accept where the accept times out
|
|
312 |
*/
|
|
313 |
public void testTimedAccept3() throws IOException {
|
|
314 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57207
|
315 |
ss.setSoTimeout(2000);
|
|
316 |
try {
|
57210
|
317 |
Socket s = ss.accept();
|
|
318 |
s.close();
|
57207
|
319 |
assertTrue(false);
|
|
320 |
} catch (SocketTimeoutException expected) { }
|
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
/**
|
57210
|
325 |
* Test timed accept where a connection is established immediately after a
|
|
326 |
* previous accept timed out.
|
57207
|
327 |
*/
|
57210
|
328 |
public void testTimedAccept4() throws IOException {
|
57207
|
329 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57210
|
330 |
ss.setSoTimeout(2000);
|
57207
|
331 |
try {
|
57210
|
332 |
Socket s = ss.accept();
|
|
333 |
s.close();
|
57207
|
334 |
assertTrue(false);
|
|
335 |
} catch (SocketTimeoutException expected) { }
|
|
336 |
try (Socket s1 = new Socket()) {
|
|
337 |
s1.connect(ss.getLocalSocketAddress());
|
|
338 |
Socket s2 = ss.accept();
|
|
339 |
s2.close();
|
|
340 |
}
|
|
341 |
}
|
|
342 |
}
|
|
343 |
|
|
344 |
/**
|
57210
|
345 |
* Test untimed accept where a connection is established after a previous
|
|
346 |
* accept timed out
|
|
347 |
*/
|
|
348 |
public void testTimedAccept5() throws IOException {
|
|
349 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
350 |
ss.setSoTimeout(2000);
|
|
351 |
try {
|
|
352 |
Socket s = ss.accept();
|
|
353 |
s.close();
|
|
354 |
assertTrue(false);
|
|
355 |
} catch (SocketTimeoutException expected) { }
|
|
356 |
ss.setSoTimeout(0);
|
|
357 |
try (Socket s1 = new Socket()) {
|
|
358 |
s1.connect(ss.getLocalSocketAddress());
|
|
359 |
Socket s2 = ss.accept();
|
|
360 |
s2.close();
|
|
361 |
}
|
|
362 |
}
|
|
363 |
}
|
|
364 |
|
|
365 |
/**
|
|
366 |
* Test untimed accept where a connection is established after a previous
|
|
367 |
* accept timed out and after a short delay
|
|
368 |
*/
|
|
369 |
public void testTimedAccept6() throws IOException {
|
|
370 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
371 |
ss.setSoTimeout(2000);
|
|
372 |
try {
|
|
373 |
Socket s = ss.accept();
|
|
374 |
s.close();
|
|
375 |
assertTrue(false);
|
|
376 |
} catch (SocketTimeoutException expected) { }
|
|
377 |
ss.setSoTimeout(0);
|
|
378 |
scheduleConnect(ss.getLocalSocketAddress(), 2000);
|
|
379 |
Socket s = ss.accept();
|
|
380 |
s.close();
|
|
381 |
}
|
|
382 |
}
|
|
383 |
|
|
384 |
/**
|
57207
|
385 |
* Test async close of a timed accept
|
|
386 |
*/
|
57210
|
387 |
public void testTimedAccept7() throws IOException {
|
57207
|
388 |
try (ServerSocket ss = new ServerSocket(0)) {
|
57210
|
389 |
ss.setSoTimeout(30*1000);
|
57207
|
390 |
scheduleClose(ss, 2000);
|
|
391 |
try {
|
|
392 |
ss.accept().close();
|
|
393 |
assertTrue(false);
|
|
394 |
} catch (SocketException expected) { }
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
57226
|
398 |
/**
|
|
399 |
* Test Socket setSoTimeout with a negative timeout. This case is not currently
|
|
400 |
* specified but the long standing behavior is to throw IllegalArgumentException.
|
|
401 |
*/
|
57207
|
402 |
@Test(expectedExceptions = { IllegalArgumentException.class })
|
|
403 |
public void testBadTimeout1() throws IOException {
|
|
404 |
try (Socket s = new Socket()) {
|
|
405 |
s.setSoTimeout(-1);
|
|
406 |
}
|
|
407 |
}
|
|
408 |
|
57226
|
409 |
/**
|
|
410 |
* Test ServerSocket setSoTimeout with a negative timeout. This case is not
|
|
411 |
* currently specified but the long standing behavior is to throw
|
|
412 |
* IllegalArgumentException.
|
|
413 |
*/
|
57207
|
414 |
@Test(expectedExceptions = { IllegalArgumentException.class })
|
|
415 |
public void testBadTimeout2() throws IOException {
|
|
416 |
try (ServerSocket ss = new ServerSocket()) {
|
|
417 |
ss.setSoTimeout(-1);
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
interface ThrowingBiConsumer<T, U> {
|
|
422 |
void accept(T t, U u) throws IOException;
|
|
423 |
}
|
|
424 |
|
|
425 |
/**
|
|
426 |
* Invokes the consumer with a connected pair of sockets
|
|
427 |
*/
|
|
428 |
static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
|
|
429 |
throws IOException
|
|
430 |
{
|
|
431 |
Socket s1 = null;
|
|
432 |
Socket s2 = null;
|
|
433 |
try (ServerSocket ss = new ServerSocket(0)) {
|
|
434 |
s1 = new Socket();
|
|
435 |
s1.connect(ss.getLocalSocketAddress());
|
|
436 |
s2 = ss.accept();
|
|
437 |
consumer.accept(s1, s2);
|
|
438 |
} finally {
|
|
439 |
if (s1 != null) s1.close();
|
|
440 |
if (s2 != null) s2.close();
|
|
441 |
}
|
|
442 |
}
|
|
443 |
|
|
444 |
/**
|
57210
|
445 |
* Schedule c to be closed after a delay
|
57207
|
446 |
*/
|
|
447 |
static void scheduleClose(Closeable c, long delay) {
|
57226
|
448 |
schedule(() -> {
|
|
449 |
try {
|
|
450 |
c.close();
|
|
451 |
} catch (IOException ioe) { }
|
|
452 |
}, delay);
|
57207
|
453 |
}
|
|
454 |
|
|
455 |
/**
|
57210
|
456 |
* Schedule a thread to connect to the given end point after a delay
|
57207
|
457 |
*/
|
57210
|
458 |
static void scheduleConnect(SocketAddress remote, long delay) {
|
57226
|
459 |
schedule(() -> {
|
|
460 |
try (Socket s = new Socket()) {
|
|
461 |
s.connect(remote);
|
|
462 |
} catch (IOException ioe) { }
|
|
463 |
}, delay);
|
57207
|
464 |
}
|
57210
|
465 |
|
|
466 |
/**
|
|
467 |
* Schedule a thread to read to EOF after a delay
|
|
468 |
*/
|
|
469 |
static void scheduleReadToEOF(InputStream in, long delay) {
|
57226
|
470 |
schedule(() -> {
|
|
471 |
byte[] bytes = new byte[8192];
|
|
472 |
try {
|
|
473 |
while (in.read(bytes) != -1) { }
|
|
474 |
} catch (IOException ioe) { }
|
|
475 |
}, delay);
|
57210
|
476 |
}
|
|
477 |
|
|
478 |
/**
|
|
479 |
* Schedule a thread to write after a delay
|
|
480 |
*/
|
|
481 |
static void scheduleWrite(OutputStream out, byte[] data, long delay) {
|
57226
|
482 |
schedule(() -> {
|
|
483 |
try {
|
|
484 |
out.write(data);
|
|
485 |
} catch (IOException ioe) { }
|
|
486 |
}, delay);
|
|
487 |
}
|
|
488 |
static void scheduleWrite(OutputStream out, int b, long delay) {
|
|
489 |
scheduleWrite(out, new byte[] { (byte)b }, delay);
|
|
490 |
}
|
|
491 |
|
|
492 |
static void schedule(Runnable task, long delay) {
|
57210
|
493 |
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
494 |
try {
|
|
495 |
executor.schedule(task, delay, TimeUnit.MILLISECONDS);
|
|
496 |
} finally {
|
|
497 |
executor.shutdown();
|
|
498 |
}
|
|
499 |
}
|
57207
|
500 |
}
|