Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/SelectAndCancel.java
38828 views
/*1* Copyright (c) 2004, 2010, 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* @summary Check for CancelledKeyException when key cancelled during select26*/2728import java.nio.channels.*;29import java.io.IOException;30import java.net.*;3132public class SelectAndCancel {33static SelectionKey sk;3435/*36* CancelledKeyException is the failure symptom of 472934237* NOTE: The failure is timing dependent and is not always38* seen immediately when the bug is present.39*/40public static void main(String[] args) throws Exception {41final Selector selector = Selector.open();42final ServerSocketChannel ssc =43ServerSocketChannel.open().bind(new InetSocketAddress(0));44final InetSocketAddress isa =45new InetSocketAddress(InetAddress.getLocalHost(), ssc.socket().getLocalPort());4647// Create and start a selector in a separate thread.48new Thread(new Runnable() {49public void run() {50try {51ssc.configureBlocking(false);52sk = ssc.register(selector, SelectionKey.OP_ACCEPT);53selector.select();54} catch (IOException e) {55System.err.println("error in selecting thread");56e.printStackTrace();57}58}59}).start();6061// Wait for above thread to get to select() before we call close.62Thread.sleep(3000);6364// Try to close. This should wakeup select.65new Thread(new Runnable() {66public void run() {67try {68SocketChannel sc = SocketChannel.open();69sc.connect(isa);70ssc.close();71sk.cancel();72sc.close();73} catch (IOException e) {74System.err.println("error in closing thread");75System.err.println(e);76}77}78}).start();7980// Wait for select() to be awakened, which should be done by close.81Thread.sleep(3000);8283selector.wakeup();84selector.close();85}86}878889