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 */ 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 } 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 | 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 } 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 |