1 /*
   2  * Copyright (c) 2018, 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 
  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  * @library /test/lib
  29  * @build jdk.test.lib.Utils
  30  * @run main RegisterDuringSelect
  31  */
  32 
  33 import java.io.IOException;
  34 import java.nio.channels.ClosedSelectorException;
  35 import java.nio.channels.Pipe;
  36 import java.nio.channels.SelectionKey;
  37 import java.nio.channels.Selector;
  38 import java.util.concurrent.Callable;
  39 import java.util.concurrent.ExecutorService;
  40 import java.util.concurrent.Executors;
  41 import java.util.concurrent.Future;
  42 import java.util.concurrent.Phaser;
  43 
  44 import jdk.test.lib.Utils;
  45 
  46 public class RegisterDuringSelect {
  47 
  48     static Callable<Void> selectLoop(Selector sel, Phaser barrier) {
  49         return new Callable<Void>() {
  50             @Override
  51             public Void call() throws IOException {
  52                 for (;;) {
  53                     try {
  54                         sel.select();
  55                     } catch (ClosedSelectorException ignore) {
  56                         return null;
  57                     }
  58                     if (sel.isOpen()) {
  59                         barrier.arriveAndAwaitAdvance();
  60                         System.out.println("selectLoop advanced ...");
  61                     } else {
  62                         // closed
  63                         return null;
  64                     }
  65                 }
  66             }
  67         };
  68     }
  69     /**
  70      * Invoke register, interestOps, and cancel concurrently with a thread
  71      * doing a selection operation
  72      */
  73     public static void main(String args[]) throws Exception {
  74         Future<Void> result;
  75 
  76         ExecutorService pool = Executors.newFixedThreadPool(1);
  77         try (Selector sel = Selector.open()) {
  78             Phaser barrier = new Phaser(2);
  79 
  80             // submit task to do the select loop
  81             result = pool.submit(selectLoop(sel, barrier));
  82 
  83             Pipe p = Pipe.open();
  84             try {
  85                 Pipe.SourceChannel sc = p.source();
  86                 sc.configureBlocking(false);
  87 
  88                 System.out.println("register ...");
  89                 SelectionKey key = sc.register(sel, SelectionKey.OP_READ);
  90                 if (!sel.keys().contains(key))
  91                     throw new RuntimeException("key not in key set");
  92                 Thread.sleep(Utils.adjustTimeout(50));
  93                 sel.wakeup();
  94                 barrier.arriveAndAwaitAdvance();
  95 
  96                 System.out.println("interestOps ...");
  97                 key.interestOps(0);
  98                 Thread.sleep(Utils.adjustTimeout(50));
  99                 sel.wakeup();
 100                 barrier.arriveAndAwaitAdvance();
 101 
 102                 System.out.println("cancel ...");
 103                 key.cancel();
 104                 sel.wakeup();
 105                 barrier.arriveAndAwaitAdvance();
 106                 if (sel.keys().contains(key))
 107                     throw new RuntimeException("key not removed from key set");
 108 
 109             } finally {
 110                 p.source().close();
 111                 p.sink().close();
 112             }
 113 
 114         } finally {
 115             pool.shutdown();
 116         }
 117 
 118         // ensure selectLoop completes without exception
 119         result.get();
 120 
 121     }
 122 }
 123