Path: blob/master/test/jdk/java/nio/channels/Selector/Wakeup.java
41153 views
/*1* Copyright (c) 2001, 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 640599525* @summary Unit test for selector wakeup and interruption26* @library .. /test/lib27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;33import java.util.concurrent.CyclicBarrier;3435public class Wakeup {3637static void sleep(int millis) {38try {39Thread.sleep(millis);40} catch (InterruptedException x) {41x.printStackTrace();42}43}4445static class Sleeper extends TestThread {46private static final long TIMEOUT = jdk.test.lib.Utils.adjustTimeout(20_000);4748// barrier is used to synchronize sleeper thread and checking49// thread which is the main thread: when go() get to the end,50// then start checking the sleeper's status.51private static CyclicBarrier barrier = new CyclicBarrier(2);52private static int wakeups = 0;53private static int waits = 0;5455volatile boolean interruptBeforeSelect = false;56volatile boolean started = false;57volatile boolean wantInterrupt = false;58volatile boolean closed = false;59Object gate = new Object();6061Selector sel;6263Sleeper(Selector sel, boolean wantInterrupt, boolean interruptBeforeSelect) {64super("Sleeper", System.err);65this.sel = sel;66this.wantInterrupt = wantInterrupt;67this.interruptBeforeSelect = interruptBeforeSelect;68}6970public void go() throws Exception {71started = true;72if (interruptBeforeSelect) {73synchronized (gate) { }74}75wakeups++;76System.err.println("Wakeup, selecting, " + wakeups);77try {78sel.select();79} catch (ClosedSelectorException x) {80closed = true;81}82boolean intr = Thread.currentThread().isInterrupted();83System.err.println("Wakeup " + wakeups84+ (closed ? " (closed)" : "")85+ (intr ? " (intr)" : ""));86if (closed)87return;88if (wantInterrupt) {89while (!Thread.interrupted())90Thread.yield();91}92System.err.println("Wakeup, waiting, " + wakeups);93barrier.await();94System.err.println("Wakeup, wait successfully, " + wakeups);95}9697void check(boolean close) throws Exception {98waits++;99System.err.println("waiting sleeper, " + waits);100if (!close) {101barrier.await();102System.err.println("wait barrier successfully, " + waits);103}104if (finish(TIMEOUT) == 0)105throw new Exception("Test failed");106if (this.closed != close)107throw new Exception("Selector was closed");108}109110void check() throws Exception {111check(false);112}113114static Sleeper createSleeper(Selector sel, boolean wantInterrupt,115boolean interruptBeforeSelect) throws Exception {116if (!wantInterrupt && interruptBeforeSelect) {117throw new RuntimeException("Wrong parameters!");118}119120Sleeper sleeper = new Sleeper(sel, wantInterrupt, interruptBeforeSelect);121122if (interruptBeforeSelect) {123synchronized(sleeper.gate) {124sleeper.start();125while (!sleeper.started)126sleep(50);127sleeper.interrupt();128}129} else {130sleeper.start();131while (!sleeper.started)132sleep(50);133if (wantInterrupt) {134sleep(50);135sleeper.interrupt();136}137}138return sleeper;139}140}141142static Sleeper newSleeper(Selector sel) throws Exception {143return Sleeper.createSleeper(sel, false, false);144}145146static Sleeper newSleeperWantInterrupt(Selector sel) throws Exception {147return Sleeper.createSleeper(sel, true, false);148}149150static Sleeper newSleeperWantInterruptBeforeSelect(Selector sel) throws Exception {151return Sleeper.createSleeper(sel, true, true);152}153154public static void main(String[] args) throws Exception {155156Selector sel = Selector.open();157158// Wakeup before select159sel.wakeup();160161Sleeper sleeper = newSleeper(sel); // 1162sleeper.check();163164for (int i = 2; i < 5; i++) {165// Wakeup during select166sleeper = newSleeper(sel);167sel.wakeup();168sleeper.check(); // 2 .. 4169}170171// Double wakeup172sel.wakeup();173sel.wakeup();174sleeper = newSleeper(sel);175sleeper.check(); // 5176177// Interrupt178sleeper = newSleeperWantInterrupt(sel);179sleeper.check(); // 6180181// Interrupt before select182sleeper = newSleeperWantInterruptBeforeSelect(sel);183sleeper.check(); // 7184185// Close during select186sleeper = newSleeper(sel);187sel.close();188sleeper.check(); // 8189190sleeper = newSleeper(sel);191sleeper.check(true);192}193194}195196197