< prev index next >

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

Print this page




  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 
  24 /* @test
  25  * @bug 8201315
  26  * @summary Test that channels can be registered, interest ops can changed,
  27  *          and keys cancelled while a selection operation is in progress.
  28  */
  29 
  30 import java.io.IOException;
  31 import java.nio.channels.ClosedSelectorException;
  32 import java.nio.channels.Pipe;
  33 import java.nio.channels.SelectionKey;
  34 import java.nio.channels.Selector;
  35 import java.util.concurrent.Callable;
  36 import java.util.concurrent.ExecutorService;
  37 import java.util.concurrent.Executors;
  38 import java.util.concurrent.Future;
  39 import java.util.concurrent.Phaser;
  40 
  41 public class RegisterDuringSelect {
  42 
  43     static Callable<Void> selectLoop(Selector sel, Phaser barrier) {
  44         return new Callable<Void>() {












  45             @Override
  46             public Void call() throws IOException {
  47                 for (;;) {
  48                     try {
  49                         sel.select();
  50                     } catch (ClosedSelectorException ignore) {
  51                         return null;
  52                     }
  53                     if (sel.isOpen()) {
  54                         barrier.arriveAndAwaitAdvance();
  55                         System.out.println("selectLoop advanced ...");
  56                     } else {
  57                         // closed
  58                         return null;
  59                     }

  60                 }










  61             }
  62         };
  63     }



  64     /**
  65      * Invoke register, interestOps, and cancel concurrently with a thread
  66      * doing a selection operation
  67      */
  68     public static void main(String args[]) throws Exception {
  69         Future<Void> result;
  70 
  71         ExecutorService pool = Executors.newFixedThreadPool(1);
  72         try (Selector sel = Selector.open()) {
  73             Phaser barrier = new Phaser(2);
  74 
  75             // submit task to do the select loop
  76             result = pool.submit(selectLoop(sel, barrier));
  77 
  78             Pipe p = Pipe.open();
  79             try {
  80                 Pipe.SourceChannel sc = p.source();
  81                 sc.configureBlocking(false);
  82 
  83                 System.out.println("register ...");
  84                 SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
  85                 if (!sel.keys().contains(key))
  86                     throw new RuntimeException("key not in key set");
  87                 sel.wakeup();
  88                 barrier.arriveAndAwaitAdvance();
  89 


  90                 System.out.println("interestOps ...");
  91                 key.interestOps(0);
  92                 sel.wakeup();
  93                 barrier.arriveAndAwaitAdvance();
  94 





  95                 System.out.println("cancel ...");
  96                 key.cancel();
  97                 sel.wakeup();
  98                 barrier.arriveAndAwaitAdvance();
  99                 if (sel.keys().contains(key))
 100                     throw new RuntimeException("key not removed from key set");
 101 
 102             } finally {
 103                 p.source().close();
 104                 p.sink().close();
 105             }
 106 
 107         } finally {
 108             pool.shutdown();
 109         }
 110 
 111         // ensure selectLoop completes without exception
 112         result.get();
 113 
 114     }
 115 }
 116 


  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 
  24 /* @test
  25  * @bug 8201315
  26  * @summary Test that channels can be registered, interest ops can changed,
  27  *          and keys cancelled while a selection operation is in progress.
  28  */
  29 
  30 import java.io.IOException;

  31 import java.nio.channels.Pipe;
  32 import java.nio.channels.SelectionKey;
  33 import java.nio.channels.Selector;





  34 
  35 public class RegisterDuringSelect {
  36     static abstract class Select {
  37         Selector sel;
  38         Pipe p;
  39         Pipe.SourceChannel sc;
  40         SelectionKey key;
  41         Thread t;
  42 
  43         Select() throws Exception {
  44             sel = Selector.open();
  45             p = Pipe.open();
  46             sc = p.source();
  47             sc.configureBlocking(false);
  48         }
  49         Runnable selectF(Selector sel) {
  50             return new Runnable() {
  51                 @Override
  52                 public void run() {

  53                     try {
  54                         sel.select();
  55                     } catch (IOException ex) {
  56                         throw new RuntimeException(ex);
  57                     }






  58                 }
  59             };
  60         }
  61         void test() throws Exception {
  62             try {
  63                 t = new Thread(selectF(sel));
  64                 t.start();
  65                 key = sc.register(sel, SelectionKey.OP_READ);
  66                 realTest();
  67             } finally {
  68                 sel.close();
  69                 p.source().close();
  70                 p.sink().close();
  71             }

  72         }
  73         abstract void realTest() throws Exception;
  74     }
  75 
  76     /**
  77      * Invoke register, interestOps, and cancel concurrently with a thread
  78      * doing a selection operation
  79      */
  80     public static void main(String args[]) throws Exception {
  81         new Select() {
  82             @Override
  83             void realTest() {











  84                 System.out.println("register ...");
  85                 sel.wakeup();
  86                 if (!sel.keys().contains(key))
  87                     throw new RuntimeException("key not in key set");
  88             }
  89         }.test();
  90         new Select() {
  91             @Override
  92             void realTest() {
  93                 System.out.println("interestOps ...");
  94                 key.interestOps(0);
  95                 sel.wakeup();
  96                 if (key.interestOps() != 0)
  97                     throw new RuntimeException("interested ops not cleared");
  98             }
  99         }.test();
 100         new Select() {
 101             @Override
 102             void realTest() throws Exception {
 103                 System.out.println("cancel ...");
 104                 key.cancel();
 105                 sel.wakeup();
 106                 t.join();
 107                 if (sel.keys().contains(key))
 108                     throw new RuntimeException("key not removed from key set");




 109             }
 110         }.test();







 111     }
 112 }

< prev index next >