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 long startMillis = millisTime(); |
135 try { |
135 try { |
136 s2.getInputStream().read(); |
136 s2.getInputStream().read(); |
137 assertTrue(false); |
137 assertTrue(false); |
138 } catch (SocketTimeoutException expected) { |
138 } catch (SocketTimeoutException expected) { |
139 int timeout = s2.getSoTimeout(); |
139 int timeout = s2.getSoTimeout(); |
140 checkDuration(start, timeout-100, timeout+2000); |
140 checkDuration(startMillis, timeout-100, timeout+2000); |
141 } |
141 } |
142 }); |
142 }); |
143 } |
143 } |
144 |
144 |
145 /** |
145 /** |
314 * Test timed accept where the accept times out |
314 * Test timed accept where the accept times out |
315 */ |
315 */ |
316 public void testTimedAccept3() throws IOException { |
316 public void testTimedAccept3() throws IOException { |
317 try (ServerSocket ss = new ServerSocket(0)) { |
317 try (ServerSocket ss = new ServerSocket(0)) { |
318 ss.setSoTimeout(2000); |
318 ss.setSoTimeout(2000); |
319 long start = System.currentTimeMillis(); |
319 long startMillis = millisTime(); |
320 try { |
320 try { |
321 Socket s = ss.accept(); |
321 Socket s = ss.accept(); |
322 s.close(); |
322 s.close(); |
323 assertTrue(false); |
323 assertTrue(false); |
324 } catch (SocketTimeoutException expected) { |
324 } catch (SocketTimeoutException expected) { |
325 int timeout = ss.getSoTimeout(); |
325 int timeout = ss.getSoTimeout(); |
326 checkDuration(start, timeout-100, timeout+2000); |
326 checkDuration(startMillis, timeout-100, timeout+2000); |
327 } |
327 } |
328 } |
328 } |
329 } |
329 } |
330 |
330 |
331 /** |
331 /** |
394 public void testTimedAccept7() throws IOException { |
394 public void testTimedAccept7() throws IOException { |
395 try (ServerSocket ss = new ServerSocket(0)) { |
395 try (ServerSocket ss = new ServerSocket(0)) { |
396 ss.setSoTimeout(30*1000); |
396 ss.setSoTimeout(30*1000); |
397 long delay = 2000; |
397 long delay = 2000; |
398 scheduleClose(ss, delay); |
398 scheduleClose(ss, delay); |
399 long start = System.currentTimeMillis(); |
399 long startMillis = millisTime(); |
400 try { |
400 try { |
401 ss.accept().close(); |
401 ss.accept().close(); |
402 assertTrue(false); |
402 assertTrue(false); |
403 } catch (SocketException expected) { |
403 } catch (SocketException expected) { |
404 checkDuration(start, delay-100, delay+2000); |
404 checkDuration(startMillis, delay-100, delay+2000); |
405 } |
405 } |
406 } |
406 } |
407 } |
407 } |
408 |
408 |
409 /** |
409 /** |
411 */ |
411 */ |
412 public void testTimedAccept8() throws IOException { |
412 public void testTimedAccept8() throws IOException { |
413 try (ServerSocket ss = new ServerSocket(0)) { |
413 try (ServerSocket ss = new ServerSocket(0)) { |
414 ss.setSoTimeout(2000); |
414 ss.setSoTimeout(2000); |
415 Thread.currentThread().interrupt(); |
415 Thread.currentThread().interrupt(); |
416 long start = System.currentTimeMillis(); |
416 long startMillis = millisTime(); |
417 try { |
417 try { |
418 Socket s = ss.accept(); |
418 Socket s = ss.accept(); |
419 s.close(); |
419 s.close(); |
420 assertTrue(false); |
420 assertTrue(false); |
421 } catch (SocketTimeoutException expected) { |
421 } catch (SocketTimeoutException expected) { |
422 // accept should have blocked for 2 seconds |
422 // accept should have blocked for 2 seconds |
423 int timeout = ss.getSoTimeout(); |
423 int timeout = ss.getSoTimeout(); |
424 checkDuration(start, timeout-100, timeout+2000); |
424 checkDuration(startMillis, timeout-100, timeout+2000); |
425 assertTrue(Thread.currentThread().isInterrupted()); |
425 assertTrue(Thread.currentThread().isInterrupted()); |
426 } finally { |
426 } finally { |
427 Thread.interrupted(); // clear interrupt status |
427 Thread.interrupted(); // clear interrupt status |
428 } |
428 } |
429 } |
429 } |
435 public void testTimedAccept9() throws IOException { |
435 public void testTimedAccept9() throws IOException { |
436 try (ServerSocket ss = new ServerSocket(0)) { |
436 try (ServerSocket ss = new ServerSocket(0)) { |
437 ss.setSoTimeout(4000); |
437 ss.setSoTimeout(4000); |
438 // interrupt thread after 1 second |
438 // interrupt thread after 1 second |
439 Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000); |
439 Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000); |
440 long start = System.currentTimeMillis(); |
440 long startMillis = millisTime(); |
441 try { |
441 try { |
442 Socket s = ss.accept(); // should block for 4 seconds |
442 Socket s = ss.accept(); // should block for 4 seconds |
443 s.close(); |
443 s.close(); |
444 assertTrue(false); |
444 assertTrue(false); |
445 } catch (SocketTimeoutException expected) { |
445 } catch (SocketTimeoutException expected) { |
446 // accept should have blocked for 4 seconds |
446 // accept should have blocked for 4 seconds |
447 int timeout = ss.getSoTimeout(); |
447 int timeout = ss.getSoTimeout(); |
448 checkDuration(start, timeout-100, timeout+2000); |
448 checkDuration(startMillis, timeout-100, timeout+2000); |
449 assertTrue(Thread.currentThread().isInterrupted()); |
449 assertTrue(Thread.currentThread().isInterrupted()); |
450 } finally { |
450 } finally { |
451 interrupter.cancel(true); |
451 interrupter.cancel(true); |
452 Thread.interrupted(); // clear interrupt status |
452 Thread.interrupted(); // clear interrupt status |
453 } |
453 } |
460 public void testTimedAccept10() throws Exception { |
460 public void testTimedAccept10() throws Exception { |
461 ExecutorService pool = Executors.newFixedThreadPool(2); |
461 ExecutorService pool = Executors.newFixedThreadPool(2); |
462 try (ServerSocket ss = new ServerSocket(0)) { |
462 try (ServerSocket ss = new ServerSocket(0)) { |
463 ss.setSoTimeout(4000); |
463 ss.setSoTimeout(4000); |
464 |
464 |
465 long start = System.currentTimeMillis(); |
465 long startMillis = millisTime(); |
466 |
466 |
467 Future<Socket> result1 = pool.submit(ss::accept); |
467 Future<Socket> result1 = pool.submit(ss::accept); |
468 Future<Socket> result2 = pool.submit(ss::accept); |
468 Future<Socket> result2 = pool.submit(ss::accept); |
469 |
469 |
470 // both tasks should complete with SocketTimeoutException |
470 // both tasks should complete with SocketTimeoutException |
473 e = expectThrows(ExecutionException.class, result2::get); |
473 e = expectThrows(ExecutionException.class, result2::get); |
474 assertTrue(e.getCause() instanceof SocketTimeoutException); |
474 assertTrue(e.getCause() instanceof SocketTimeoutException); |
475 |
475 |
476 // should get here in 4 seconds, not 8 seconds |
476 // should get here in 4 seconds, not 8 seconds |
477 int timeout = ss.getSoTimeout(); |
477 int timeout = ss.getSoTimeout(); |
478 checkDuration(start, timeout-100, timeout+2000); |
478 checkDuration(startMillis, timeout-100, timeout+2000); |
479 } finally { |
479 } finally { |
480 pool.shutdown(); |
480 pool.shutdown(); |
481 } |
481 } |
482 } |
482 } |
483 |
483 |
487 public void testTimedAccept11() throws Exception { |
487 public void testTimedAccept11() throws Exception { |
488 ExecutorService pool = Executors.newFixedThreadPool(2); |
488 ExecutorService pool = Executors.newFixedThreadPool(2); |
489 try (ServerSocket ss = new ServerSocket(0)) { |
489 try (ServerSocket ss = new ServerSocket(0)) { |
490 ss.setSoTimeout(4000); |
490 ss.setSoTimeout(4000); |
491 |
491 |
492 long start = System.currentTimeMillis(); |
492 long startMillis = millisTime(); |
493 |
493 |
494 Future<Socket> result1 = pool.submit(ss::accept); |
494 Future<Socket> result1 = pool.submit(ss::accept); |
495 Future<Socket> result2 = pool.submit(ss::accept); |
495 Future<Socket> result2 = pool.submit(ss::accept); |
496 |
496 |
497 // establish connection after 2 seconds |
497 // establish connection after 2 seconds |
515 } |
515 } |
516 assertTrue((s1 != null) ^ (s2 != null)); |
516 assertTrue((s1 != null) ^ (s2 != null)); |
517 |
517 |
518 // should get here in 4 seconds, not 8 seconds |
518 // should get here in 4 seconds, not 8 seconds |
519 int timeout = ss.getSoTimeout(); |
519 int timeout = ss.getSoTimeout(); |
520 checkDuration(start, timeout-100, timeout+2000); |
520 checkDuration(startMillis, timeout-100, timeout+2000); |
521 } finally { |
521 } finally { |
522 pool.shutdown(); |
522 pool.shutdown(); |
523 } |
523 } |
524 } |
524 } |
525 |
525 |
629 executor.shutdown(); |
629 executor.shutdown(); |
630 } |
630 } |
631 } |
631 } |
632 |
632 |
633 /** |
633 /** |
|
634 * Returns the current time in milliseconds. |
|
635 */ |
|
636 private static long millisTime() { |
|
637 long now = System.nanoTime(); |
|
638 return TimeUnit.MILLISECONDS.convert(now, TimeUnit.NANOSECONDS); |
|
639 } |
|
640 |
|
641 /** |
634 * Check the duration of a task |
642 * Check the duration of a task |
635 * @param start start time, in milliseconds |
643 * @param start start time, in milliseconds |
636 * @param min minimum expected duration, in milliseconds |
644 * @param min minimum expected duration, in milliseconds |
637 * @param max maximum expected duration, in milliseconds |
645 * @param max maximum expected duration, in milliseconds |
638 * @return the duration (now - start), in milliseconds |
646 * @return the duration (now - start), in milliseconds |
639 */ |
647 */ |
640 private static long checkDuration(long start, long min, long max) { |
648 private static long checkDuration(long start, long min, long max) { |
641 long duration = System.currentTimeMillis() - start; |
649 long duration = millisTime() - start; |
642 assertTrue(duration >= min && duration <= max); |
650 assertTrue(duration >= min, |
|
651 "Duration " + duration + "ms, expected >= " + min + "ms"); |
|
652 assertTrue(duration <= max, |
|
653 "Duration " + duration + "ms, expected <= " + max + "ms"); |
643 return duration; |
654 return duration; |
644 } |
655 } |
645 } |
656 } |