< prev index next >

test/jdk/java/nio/channels/Selector/SelectWithConsumer.java

Print this page




 394      */
 395     public void testInterruptDuringSelect() throws Exception {
 396         // select(Consumer)
 397         try (Selector sel = Selector.open()) {
 398             scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
 399             int n = sel.select(k -> assertTrue(false));
 400             assertTrue(n == 0);
 401             assertTrue(Thread.currentThread().isInterrupted());
 402             assertTrue(sel.isOpen());
 403         } finally {
 404             Thread.currentThread().interrupted();  // clear interrupt status
 405         }
 406 
 407         // select(Consumer, timeout)
 408         try (Selector sel = Selector.open()) {
 409             scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
 410             long start = System.currentTimeMillis();
 411             int n = sel.select(k -> assertTrue(false), 60*1000);
 412             long duration = System.currentTimeMillis() - start;
 413             assertTrue(n == 0);
 414             assertTrue(duration > 500 && duration < 5000,
 415                     "select took " + duration + " ms");
 416             assertTrue(Thread.currentThread().isInterrupted());
 417             assertTrue(sel.isOpen());
 418         } finally {
 419             Thread.currentThread().interrupted();  // clear interrupt status
 420         }
 421     }
 422 
 423     /**
 424      * Test invoking select on a closed selector
 425      */
 426     @Test(expectedExceptions = ClosedSelectorException.class)
 427     public void testClosedSelector1() throws Exception {
 428         Selector sel = Selector.open();
 429         sel.close();
 430         sel.select(k -> assertTrue(false));
 431     }
 432     @Test(expectedExceptions = ClosedSelectorException.class)
 433     public void testClosedSelector2() throws Exception {
 434         Selector sel = Selector.open();
 435         sel.close();




 394      */
 395     public void testInterruptDuringSelect() throws Exception {
 396         // select(Consumer)
 397         try (Selector sel = Selector.open()) {
 398             scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
 399             int n = sel.select(k -> assertTrue(false));
 400             assertTrue(n == 0);
 401             assertTrue(Thread.currentThread().isInterrupted());
 402             assertTrue(sel.isOpen());
 403         } finally {
 404             Thread.currentThread().interrupted();  // clear interrupt status
 405         }
 406 
 407         // select(Consumer, timeout)
 408         try (Selector sel = Selector.open()) {
 409             scheduleInterrupt(Thread.currentThread(), 1, SECONDS);
 410             long start = System.currentTimeMillis();
 411             int n = sel.select(k -> assertTrue(false), 60*1000);
 412             long duration = System.currentTimeMillis() - start;
 413             assertTrue(n == 0);


 414             assertTrue(Thread.currentThread().isInterrupted());
 415             assertTrue(sel.isOpen());
 416         } finally {
 417             Thread.currentThread().interrupted();  // clear interrupt status
 418         }
 419     }
 420 
 421     /**
 422      * Test invoking select on a closed selector
 423      */
 424     @Test(expectedExceptions = ClosedSelectorException.class)
 425     public void testClosedSelector1() throws Exception {
 426         Selector sel = Selector.open();
 427         sel.close();
 428         sel.select(k -> assertTrue(false));
 429     }
 430     @Test(expectedExceptions = ClosedSelectorException.class)
 431     public void testClosedSelector2() throws Exception {
 432         Selector sel = Selector.open();
 433         sel.close();


< prev index next >