23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @library /test/lib |
26 * @library /test/lib |
27 * @build jdk.test.lib.Utils |
27 * @build jdk.test.lib.Utils |
28 * @run testng Timeouts |
28 * @run testng/timeout=180 Timeouts |
29 * @summary Test Socket timeouts |
29 * @summary Test Socket timeouts |
30 */ |
30 */ |
31 |
31 |
32 import java.io.Closeable; |
32 import java.io.Closeable; |
33 import java.io.IOException; |
33 import java.io.IOException; |
129 * Test timed read where the read times out |
129 * Test timed read where the read times out |
130 */ |
130 */ |
131 public void testTimedRead3() throws IOException { |
131 public void testTimedRead3() throws IOException { |
132 withConnection((s1, s2) -> { |
132 withConnection((s1, s2) -> { |
133 s2.setSoTimeout(2000); |
133 s2.setSoTimeout(2000); |
|
134 long start = System.currentTimeMillis(); |
134 try { |
135 try { |
135 s2.getInputStream().read(); |
136 s2.getInputStream().read(); |
136 assertTrue(false); |
137 assertTrue(false); |
137 } catch (SocketTimeoutException expected) { } |
138 } catch (SocketTimeoutException expected) { |
|
139 int timeout = s2.getSoTimeout(); |
|
140 checkDuration(start, timeout-100, timeout+2000); |
|
141 } |
138 }); |
142 }); |
139 } |
143 } |
140 |
144 |
141 /** |
145 /** |
142 * Test timed read that succeeds after a previous read has timed out |
146 * Test timed read that succeeds after a previous read has timed out |
310 * Test timed accept where the accept times out |
314 * Test timed accept where the accept times out |
311 */ |
315 */ |
312 public void testTimedAccept3() throws IOException { |
316 public void testTimedAccept3() throws IOException { |
313 try (ServerSocket ss = new ServerSocket(0)) { |
317 try (ServerSocket ss = new ServerSocket(0)) { |
314 ss.setSoTimeout(2000); |
318 ss.setSoTimeout(2000); |
|
319 long start = System.currentTimeMillis(); |
315 try { |
320 try { |
316 Socket s = ss.accept(); |
321 Socket s = ss.accept(); |
317 s.close(); |
322 s.close(); |
318 assertTrue(false); |
323 assertTrue(false); |
319 } catch (SocketTimeoutException expected) { } |
324 } catch (SocketTimeoutException expected) { |
|
325 int timeout = ss.getSoTimeout(); |
|
326 checkDuration(start, timeout-100, timeout+2000); |
|
327 } |
320 } |
328 } |
321 } |
329 } |
322 |
330 |
323 /** |
331 /** |
324 * Test timed accept where a connection is established immediately after a |
332 * Test timed accept where a connection is established immediately after a |
384 * Test async close of a timed accept |
392 * Test async close of a timed accept |
385 */ |
393 */ |
386 public void testTimedAccept7() throws IOException { |
394 public void testTimedAccept7() throws IOException { |
387 try (ServerSocket ss = new ServerSocket(0)) { |
395 try (ServerSocket ss = new ServerSocket(0)) { |
388 ss.setSoTimeout(30*1000); |
396 ss.setSoTimeout(30*1000); |
389 scheduleClose(ss, 2000); |
397 long delay = 2000; |
|
398 scheduleClose(ss, delay); |
|
399 long start = System.currentTimeMillis(); |
390 try { |
400 try { |
391 ss.accept().close(); |
401 ss.accept().close(); |
392 assertTrue(false); |
402 assertTrue(false); |
393 } catch (SocketException expected) { } |
403 } catch (SocketException expected) { |
|
404 checkDuration(start, delay-100, delay+2000); |
|
405 } |
394 } |
406 } |
395 } |
407 } |
396 |
408 |
397 /** |
409 /** |
398 * Test timed accept with the thread interrupt status set. |
410 * Test timed accept with the thread interrupt status set. |
405 try { |
417 try { |
406 Socket s = ss.accept(); |
418 Socket s = ss.accept(); |
407 s.close(); |
419 s.close(); |
408 assertTrue(false); |
420 assertTrue(false); |
409 } catch (SocketTimeoutException expected) { |
421 } catch (SocketTimeoutException expected) { |
410 // accept should have blocked for 2000ms |
422 // accept should have blocked for 2 seconds |
411 assertTrue((System.currentTimeMillis() - start) > 1800); |
423 int timeout = ss.getSoTimeout(); |
|
424 checkDuration(start, timeout-100, timeout+2000); |
412 assertTrue(Thread.currentThread().isInterrupted()); |
425 assertTrue(Thread.currentThread().isInterrupted()); |
413 } finally { |
426 } finally { |
414 Thread.interrupted(); // clear interrupt status |
427 Thread.interrupted(); // clear interrupt status |
415 } |
428 } |
416 } |
429 } |
420 * Test interrupt of thread blocked in timed accept. |
433 * Test interrupt of thread blocked in timed accept. |
421 */ |
434 */ |
422 public void testTimedAccept9() throws IOException { |
435 public void testTimedAccept9() throws IOException { |
423 try (ServerSocket ss = new ServerSocket(0)) { |
436 try (ServerSocket ss = new ServerSocket(0)) { |
424 ss.setSoTimeout(4000); |
437 ss.setSoTimeout(4000); |
425 // interrupt thread after 1000ms |
438 // interrupt thread after 1 second |
426 Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000); |
439 Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000); |
427 long start = System.currentTimeMillis(); |
440 long start = System.currentTimeMillis(); |
428 try { |
441 try { |
429 Socket s = ss.accept(); // should block for 4000 |
442 Socket s = ss.accept(); // should block for 4 seconds |
430 s.close(); |
443 s.close(); |
431 assertTrue(false); |
444 assertTrue(false); |
432 } catch (SocketTimeoutException expected) { |
445 } catch (SocketTimeoutException expected) { |
433 // accept should have blocked for 4000ms |
446 // accept should have blocked for 4 seconds |
434 assertTrue((System.currentTimeMillis() - start) > 3800); |
447 int timeout = ss.getSoTimeout(); |
|
448 checkDuration(start, timeout-100, timeout+2000); |
435 assertTrue(Thread.currentThread().isInterrupted()); |
449 assertTrue(Thread.currentThread().isInterrupted()); |
436 } finally { |
450 } finally { |
437 interrupter.cancel(true); |
451 interrupter.cancel(true); |
438 Thread.interrupted(); // clear interrupt status |
452 Thread.interrupted(); // clear interrupt status |
439 } |
453 } |
457 Throwable e = expectThrows(ExecutionException.class, result1::get); |
471 Throwable e = expectThrows(ExecutionException.class, result1::get); |
458 assertTrue(e.getCause() instanceof SocketTimeoutException); |
472 assertTrue(e.getCause() instanceof SocketTimeoutException); |
459 e = expectThrows(ExecutionException.class, result2::get); |
473 e = expectThrows(ExecutionException.class, result2::get); |
460 assertTrue(e.getCause() instanceof SocketTimeoutException); |
474 assertTrue(e.getCause() instanceof SocketTimeoutException); |
461 |
475 |
462 // both tasks should completed in a little over 4000ms |
476 // should get here in 4 seconds, not 8 seconds |
463 assertTrue((System.currentTimeMillis() - start) < 5000); |
477 int timeout = ss.getSoTimeout(); |
|
478 checkDuration(start, timeout-100, timeout+2000); |
464 } finally { |
479 } finally { |
465 pool.shutdown(); |
480 pool.shutdown(); |
466 } |
481 } |
467 } |
482 } |
468 |
483 |
477 long start = System.currentTimeMillis(); |
492 long start = System.currentTimeMillis(); |
478 |
493 |
479 Future<Socket> result1 = pool.submit(ss::accept); |
494 Future<Socket> result1 = pool.submit(ss::accept); |
480 Future<Socket> result2 = pool.submit(ss::accept); |
495 Future<Socket> result2 = pool.submit(ss::accept); |
481 |
496 |
482 scheduleConnect(ss.getLocalSocketAddress(), 1500); |
497 // establish connection after 3 seconds |
|
498 scheduleConnect(ss.getLocalSocketAddress(), 3000); |
483 |
499 |
484 // one task should have accepted the connection, the other should |
500 // one task should have accepted the connection, the other should |
485 // have completed with SocketTimeoutException |
501 // have completed with SocketTimeoutException |
486 Socket s1 = null; |
502 Socket s1 = null; |
487 try { |
503 try { |
497 } catch (ExecutionException e) { |
513 } catch (ExecutionException e) { |
498 assertTrue(e.getCause() instanceof SocketTimeoutException); |
514 assertTrue(e.getCause() instanceof SocketTimeoutException); |
499 } |
515 } |
500 assertTrue((s1 != null) ^ (s2 != null)); |
516 assertTrue((s1 != null) ^ (s2 != null)); |
501 |
517 |
502 // both tasks should completed in a little over 4000ms |
518 // should get here in 4 seconds, not 7 seconds |
503 long duration = System.currentTimeMillis() - start; |
519 int timeout = ss.getSoTimeout(); |
504 assertTrue(duration > 3800 && duration < 5000); |
520 checkDuration(start, timeout-100, timeout+2000); |
505 } finally { |
521 } finally { |
506 pool.shutdown(); |
522 pool.shutdown(); |
507 } |
523 } |
508 } |
524 } |
509 |
525 |
611 return executor.schedule(task, delay, TimeUnit.MILLISECONDS); |
627 return executor.schedule(task, delay, TimeUnit.MILLISECONDS); |
612 } finally { |
628 } finally { |
613 executor.shutdown(); |
629 executor.shutdown(); |
614 } |
630 } |
615 } |
631 } |
|
632 |
|
633 /** |
|
634 * Check the duration of a task |
|
635 * @param start start time, in milliseconds |
|
636 * @param min minimum expected duration, in milliseconds |
|
637 * @param max maximum expected duration, in milliseconds |
|
638 * @return the duration (now - start), in milliseconds |
|
639 */ |
|
640 private static long checkDuration(long start, long min, long max) { |
|
641 long duration = System.currentTimeMillis() - start; |
|
642 assertTrue(duration >= min && duration <= max); |
|
643 return duration; |
|
644 } |
616 } |
645 } |