Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/Wakeup.java
38828 views
/*1* Copyright (c) 2001, 2008, 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 ..27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;33import java.util.Random;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 {46volatile boolean started = false;47volatile int entries = 0;48volatile int wakeups = 0;49volatile boolean wantInterrupt = false;50volatile boolean gotInterrupt = false;51volatile Exception exception = null;52volatile boolean closed = false;53Object gate = new Object();5455Selector sel;5657Sleeper(Selector sel) {58super("Sleeper", System.err);59this.sel = sel;60}6162public void go() throws Exception {63started = true;64for (;;) {65synchronized (gate) { }66entries++;67try {68sel.select();69} catch (ClosedSelectorException x) {70closed = true;71}72boolean intr = Thread.currentThread().isInterrupted();73wakeups++;74System.err.println("Wakeup " + wakeups75+ (closed ? " (closed)" : "")76+ (intr ? " (intr)" : ""));77if (wakeups > 1000)78throw new Exception("Too many wakeups");79if (closed)80return;81if (wantInterrupt) {82while (!Thread.interrupted())83Thread.yield();84gotInterrupt = true;85wantInterrupt = false;86}87}88}8990}9192private static int checkedWakeups = 0;9394private static void check(Sleeper sleeper, boolean intr)95throws Exception96{97checkedWakeups++;98if (sleeper.wakeups > checkedWakeups) {99sleeper.finish(100);100throw new Exception("Sleeper has run ahead");101}102int n = 0;103while (sleeper.wakeups < checkedWakeups) {104sleep(50);105if ((n += 50) > 1000) {106sleeper.finish(100);107throw new Exception("Sleeper appears to be dead ("108+ checkedWakeups + ")");109}110}111if (sleeper.wakeups > checkedWakeups) {112sleeper.finish(100);113throw new Exception("Too many wakeups: Expected "114+ checkedWakeups115+ ", got " + sleeper.wakeups);116}117if (intr) {118n = 0;119// Interrupts can sometimes be delayed, so wait120while (!sleeper.gotInterrupt) {121sleep(50);122if ((n += 50) > 1000) {123sleeper.finish(100);124throw new Exception("Interrupt never delivered");125}126}127sleeper.gotInterrupt = false;128}129System.err.println("Check " + checkedWakeups130+ (intr ? " (intr " + n + ")" : ""));131}132133public static void main(String[] args) throws Exception {134135Selector sel = Selector.open();136137// Wakeup before select138sel.wakeup();139140Sleeper sleeper = new Sleeper(sel);141142sleeper.start();143while (!sleeper.started)144sleep(50);145146check(sleeper, false); // 1147148for (int i = 2; i < 5; i++) {149// Wakeup during select150sel.wakeup();151check(sleeper, false); // 2 .. 4152}153154// Double wakeup155synchronized (sleeper.gate) {156sel.wakeup();157check(sleeper, false); // 5158sel.wakeup();159sel.wakeup();160}161check(sleeper, false); // 6162163// Interrupt164synchronized (sleeper.gate) {165sleeper.wantInterrupt = true;166sleeper.interrupt();167check(sleeper, true); // 7168}169170// Interrupt before select171while (sleeper.entries < 8)172Thread.yield();173synchronized (sleeper.gate) {174sel.wakeup();175check(sleeper, false); // 8176sleeper.wantInterrupt = true;177sleeper.interrupt();178sleep(50);179}180check(sleeper, true); // 9181182// Close during select183while (sleeper.entries < 10)184Thread.yield();185synchronized (sleeper.gate) {186sel.close();187check(sleeper, false); // 10188}189190if (sleeper.finish(200) == 0)191throw new Exception("Test failed");192if (!sleeper.closed)193throw new Exception("Selector not closed");194}195196}197198199