Path: blob/master/test/jdk/java/nio/channels/Selector/RegisterDuringSelect.java
51568 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 820131525* @build SelectorUtils26* @run main RegisterDuringSelect27* @summary Test that channels can be registered, interest ops can changed,28* and keys cancelled while a selection operation is in progress.29*/3031import java.io.IOException;32import java.nio.channels.Pipe;33import java.nio.channels.SelectionKey;34import java.nio.channels.Selector;3536public class RegisterDuringSelect {37interface TestOperation {38void accept(Thread t, Selector sel, Pipe.SourceChannel sc) throws Exception;39}40static class Test {41final Selector sel;42final Pipe p;43final Pipe.SourceChannel sc;4445Test() throws Exception {46sel = Selector.open();47p = Pipe.open();48sc = p.source();49sc.configureBlocking(false);50}51void test(TestOperation op) throws Exception {52try {53Thread t = new Thread(() -> {54try {55sel.select();56} catch (IOException ex) {57throw new RuntimeException(ex);58}59});60t.start();61op.accept(t, sel, sc);62} finally {63sel.close();64p.source().close();65p.sink().close();66}67}68}69/**70* Invoke register, interestOps, and cancel concurrently with a thread71* doing a selection operation72*/73public static void main(String args[]) throws Exception {74new Test().test((t, sel, sc) -> {75System.out.println("register ...");76// spin until make sure select is invoked77SelectorUtils.spinUntilLocked(t, sel);78SelectionKey key = sc.register(sel, SelectionKey.OP_READ);79try {80if (!sel.keys().contains(key))81throw new RuntimeException("key not in key set");82} finally {83sel.wakeup();84t.join();85}86});87new Test().test((t, sel, sc) -> {88System.out.println("interestOps ...");89SelectionKey key = sc.register(sel, SelectionKey.OP_READ);90// spin until make sure select is invoked91SelectorUtils.spinUntilLocked(t, sel);92key.interestOps(0);93try {94if (key.interestOps() != 0)95throw new RuntimeException("interested ops not cleared");96} finally {97sel.wakeup();98t.join();99}100});101new Test().test((t, sel, sc) -> {102System.out.println("cancel ...");103SelectionKey key = sc.register(sel, SelectionKey.OP_READ);104// spin until make sure select is invoked105SelectorUtils.spinUntilLocked(t, sel);106key.cancel();107sel.wakeup();108t.join();109if (sel.keys().contains(key))110throw new RuntimeException("key not removed from key set");111});112}113}114115116