Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/AsyncCloseChannel.java
38828 views
/*1* Copyright (c) 2006, 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 6285901 650108925* @summary Check no data is written to wrong socket channel during async closing.26* @author Xueming Shen27*/2829import java.io.*;30import java.nio.*;31import java.nio.channels.*;32import java.net.*;3334public class AsyncCloseChannel {35static volatile boolean failed = false;36static volatile boolean keepGoing = true;37static int maxAcceptCount = 100;38static volatile int acceptCount = 0;39static String host = "127.0.0.1";40static int sensorPort;41static int targetPort;4243public static void main(String args[]) throws Exception {44if (System.getProperty("os.name").startsWith("Windows")) {45System.err.println("WARNING: Still does not work on Windows!");46return;47}48Thread ss = new SensorServer(); ss.start();49Thread ts = new TargetServer(); ts.start();5051sensorPort = ((ServerThread)ss).server.getLocalPort();52targetPort = ((ServerThread)ts).server.getLocalPort();5354Thread sc = new SensorClient(); sc.start();55Thread tc = new TargetClient(); tc.start();5657while(acceptCount < maxAcceptCount && !failed) {58Thread.sleep(10);59}60keepGoing = false;61try {62ss.interrupt();63ts.interrupt();64sc.interrupt();65tc.interrupt();66} catch (Exception e) {}67if (failed)68throw new RuntimeException("AsyncCloseChannel2 failed after <"69+ acceptCount + "> times of accept!");70}7172static class SensorServer extends ServerThread {73public void runEx() throws Exception {74while(keepGoing) {75try {76final Socket s = server.accept();77new Thread() {78public void run() {79try {80int c = s.getInputStream().read();81if(c != -1) {82// No data is ever written to the peer's socket!83System.err.println("Oops: read a character: "84+ (char) c);85failed = true;86}87} catch (IOException ex) {88ex.printStackTrace();89} finally {90closeIt(s);91}92}93}.start();94} catch (IOException ex) {95System.err.println("Exception on sensor server " + ex.getMessage());96}97}98}99}100101static class TargetServer extends ServerThread {102public void runEx() throws Exception {103while (keepGoing) {104try {105final Socket s = server.accept();106acceptCount++;107new Thread() {108public void run() {109boolean empty = true;110try {111for(;;) {112int c = s.getInputStream().read();113if(c == -1) {114if(!empty)115break;116}117empty = false;118}119} catch (IOException ex) {120ex.printStackTrace();121} finally {122closeIt(s);123}124}125}.start();126} catch (IOException ex) {127System.err.println("Exception on target server " + ex.getMessage());128}129}130}131}132133static class SensorClient extends Thread {134private static boolean wake;135private static SensorClient theClient;136public void run() {137while (keepGoing) {138Socket s = null;139try {140s = new Socket();141synchronized(this) {142while(!wake && keepGoing) {143try {144wait();145} catch (InterruptedException ex) { }146}147wake = false;148}149s.connect(new InetSocketAddress(host, sensorPort));150try {151Thread.sleep(10);152} catch (InterruptedException ex) { }153} catch (IOException ex) {154System.err.println("Exception on sensor client " + ex.getMessage());155} finally {156if(s != null) {157try {158s.close();159} catch(IOException ex) { ex.printStackTrace();}160}161}162}163}164165public SensorClient() {166theClient = this;167}168169public static void wakeMe() {170synchronized(theClient) {171wake = true;172theClient.notify();173}174}175}176177static class TargetClient extends Thread {178volatile boolean ready = false;179public void run() {180while(keepGoing) {181try {182final SocketChannel s = SocketChannel.open(183new InetSocketAddress(host, targetPort));184s.finishConnect();185s.socket().setSoLinger(false, 0);186ready = false;187Thread t = new Thread() {188public void run() {189ByteBuffer b = ByteBuffer.allocate(1);190try {191for(;;) {192b.clear();193b.put((byte) 'A');194b.flip();195s.write(b);196ready = true;197}198} catch (IOException ex) {199if(!(ex instanceof ClosedChannelException))200System.err.println("Exception in target client child "201+ ex.toString());202}203}204};205t.start();206while(!ready && keepGoing) {207try {208Thread.sleep(10);209} catch (InterruptedException ex) {}210}211s.close();212SensorClient.wakeMe();213t.join();214} catch (IOException ex) {215System.err.println("Exception in target client parent "216+ ex.getMessage());217} catch (InterruptedException ex) {}218}219}220}221222static abstract class ServerThread extends Thread {223ServerSocket server;224public ServerThread() {225super();226try {227server = new ServerSocket(0);228} catch (IOException ex) {229ex.printStackTrace();230}231}232233public void interrupt() {234super.interrupt();235if (server != null) {236try {237server.close();238} catch (IOException ex) {239ex.printStackTrace();240}241}242}243public void run() {244try {245runEx();246} catch (Exception ex) {247ex.printStackTrace();248}249}250251abstract void runEx() throws Exception;252}253254public static void closeIt(Socket s) {255try {256if(s != null)257s.close();258} catch (IOException ex) { }259}260}261262263