Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/RacyDeregister.java
38828 views
/*1* Copyright (c) 2013, 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/*24* Portions Copyright (c) 2012 IBM Corporation25*/2627import java.net.InetAddress;28import java.net.InetSocketAddress;29import java.nio.channels.SelectionKey;30import java.nio.channels.Selector;31import java.nio.channels.ServerSocketChannel;32import java.nio.channels.SocketChannel;3334/*35* @test36* @bug 642920437* @summary SelectionKey.interestOps does not update interest set on Windows.38* @author Frank Ding39*/40public class RacyDeregister {4142static boolean notified;43static final Object selectorLock = new Object();44static final Object notifyLock = new Object();45/**46* null: not terminated47* true: passed48* false: failed49*/50static volatile Boolean succTermination = null;5152public static void main(String[] args) throws Exception {53InetAddress addr = InetAddress.getByName(null);54ServerSocketChannel sc = ServerSocketChannel.open();55sc.socket().bind(new InetSocketAddress(addr, 0));5657SocketChannel.open(new InetSocketAddress(addr,58sc.socket().getLocalPort()));5960SocketChannel accepted = sc.accept();61accepted.configureBlocking(false);6263SocketChannel.open(new InetSocketAddress(addr,64sc.socket().getLocalPort()));65SocketChannel accepted2 = sc.accept();66accepted2.configureBlocking(false);6768final Selector sel = Selector.open();69SelectionKey key2 = accepted2.register(sel, SelectionKey.OP_READ);70final SelectionKey[] key = new SelectionKey[]{71accepted.register(sel, SelectionKey.OP_READ)};727374// thread that will be changing key[0].interestOps to OP_READ | OP_WRITE75new Thread() {7677public void run() {78try {79for (int k = 0; k < 15; k++) {80for (int i = 0; i < 10000; i++) {81synchronized (notifyLock) {82synchronized (selectorLock) {83sel.wakeup();84key[0].interestOps(SelectionKey.OP_READ85| SelectionKey.OP_WRITE);86}87notified = false;88long beginTime = System.currentTimeMillis();89while (true) {90notifyLock.wait(5000);91if (notified) {92break;93}94long endTime = System.currentTimeMillis();95if (endTime - beginTime > 5000) {96succTermination = false;97// wake up main thread doing select()98sel.wakeup();99return;100}101}102}103}104}105succTermination = true;106// wake up main thread doing select()107sel.wakeup();108} catch (Exception e) {109System.out.println(e);110succTermination = true;111// wake up main thread doing select()112sel.wakeup();113}114}115}.start();116117// main thread will be doing registering/deregistering with the sel118while (true) {119sel.select();120if (Boolean.TRUE.equals(succTermination)) {121System.out.println("Test passed");122sel.close();123sc.close();124break;125} else if (Boolean.FALSE.equals(succTermination)) {126System.out.println("Failed to pass the test");127sel.close();128sc.close();129throw new RuntimeException("Failed to pass the test");130}131synchronized (selectorLock) {132}133if (sel.selectedKeys().contains(key[0]) && key[0].isWritable()) {134synchronized (notifyLock) {135notified = true;136notifyLock.notify();137key[0].cancel();138sel.selectNow();139key2 = accepted2.register(sel, SelectionKey.OP_READ);140key[0] = accepted.register(sel, SelectionKey.OP_READ);141}142}143key2.cancel();144sel.selectedKeys().clear();145}146}147}148149150