Path: blob/master/test/jdk/java/nio/channels/SelectionKey/RacyRegister.java
51568 views
/*1* Copyright (c) 2012, 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 713288925* @summary Test that register does not return a valid SelectionKey when26* invoked at around the time that the channel is closed27*/2829import java.nio.channels.*;30import java.util.concurrent.*;31import java.util.Random;32import java.io.IOException;3334public class RacyRegister {3536public static void main(String[] args) throws Exception {37ExecutorService pool = Executors.newFixedThreadPool(1);38try (Selector sel = Selector.open()) {39int count = 100;40while (count-- > 0) {41final SocketChannel sc = SocketChannel.open();42sc.configureBlocking(false);4344// close channel asynchronously45Future<Void> result = pool.submit(new Callable<Void>() {46public Void call() throws IOException {47sc.close();48return null;49}50});5152// attempt to register channel with Selector53SelectionKey key = null;54try {55key = sc.register(sel, SelectionKey.OP_READ);56} catch (ClosedChannelException ignore) {57}5859// ensure close is done60result.get();6162// if we have a key then it should be invalid63if (key != null && key.isValid())64throw new RuntimeException("Key is valid");65}66} finally {67pool.shutdown();68}69}70}717273