Path: blob/master/test/jdk/java/nio/channels/Selector/SelectAndCancel.java
41153 views
/*1* Copyright (c) 2004, 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 472934225* @library /test/lib26* @build jdk.test.lib.Utils27* @run main SelectAndCancel28* @summary Check for CancelledKeyException when key cancelled during select29*/3031import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.nio.channels.SelectionKey;34import java.nio.channels.Selector;35import java.nio.channels.ServerSocketChannel;36import java.util.concurrent.CountDownLatch;3738public class SelectAndCancel {39static volatile SelectionKey sk;40static volatile Throwable ex;4142/*43* CancelledKeyException is the failure symptom of 472934244* NOTE: The failure is timing dependent and is not always45* seen immediately when the bug is present.46*/47public static void main(String[] args) throws Throwable {48final Selector selector = Selector.open();49final ServerSocketChannel ssc = ServerSocketChannel.open().bind(50new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));51final InetSocketAddress isa = (InetSocketAddress)ssc.getLocalAddress();52final CountDownLatch signal = new CountDownLatch(1);5354// Create and start a selector in a separate thread.55Thread t = new Thread(new Runnable() {56public void run() {57try {58ssc.configureBlocking(false);59sk = ssc.register(selector, SelectionKey.OP_ACCEPT);60signal.countDown();61selector.select();62} catch (Throwable e) {63ex = e;64}65}66});67t.start();6869signal.await();70// Wait for above thread to get to select() before we call cancel.71Thread.sleep((long)(300 * jdk.test.lib.Utils.TIMEOUT_FACTOR));7273// CancelledKeyException should not be thrown.74ssc.close();75sk.cancel();76selector.close();77t.join();78if (ex != null) {79throw ex;80}81}82}838485