Path: blob/master/test/jdk/java/net/Socket/asyncClose/ServerSocket_accept.java
41153 views
/*1* Copyright (c) 2001, 2019, 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* Tests that a thread blocked in ServerSocket.accept25* throws a SocketException if the socket is asynchronously closed.26*/27import java.io.IOException;28import java.net.InetAddress;29import java.net.ServerSocket;30import java.net.Socket;31import java.net.SocketException;32import java.util.concurrent.CountDownLatch;33import java.util.concurrent.atomic.AtomicBoolean;3435public class ServerSocket_accept extends AsyncCloseTest implements Runnable {36private final ServerSocket ss;37private final int timeout;38private final CountDownLatch latch;39private final AtomicBoolean readyToClose = new AtomicBoolean(false);4041public ServerSocket_accept() throws IOException {42this(0);43}4445public ServerSocket_accept(int timeout) throws IOException {46this.timeout = timeout;47latch = new CountDownLatch(1);48ss = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());49}5051public String description() {52String s = "ServerSocket.accept()";53if (timeout > 0) {54s += " (with timeout)";55}56return s;57}5859public void run() {60try {61latch.countDown();62Socket s;63// if readyToClose is still false it means some other64// process on the system attempted to connect: just65// ignore it, and go back to accept again.66do {67s = ss.accept();68} while (!readyToClose.get());69failed("ServerSocket.accept() returned unexpectedly!!" + " - " + s);70} catch (SocketException se) {71closed();72} catch (Exception e) {73failed(e.getMessage());74}75}7677public AsyncCloseTest go(){78try {79Thread thr = new Thread(this);80thr.start();81latch.await();82Thread.sleep(5000); //sleep, so ServerSocket.accept() can block83readyToClose.set(true);84ss.close();85thr.join();8687if (isClosed()) {88return passed();89} else {90return failed("ServerSocket.accept() wasn't preempted");91}92} catch (Exception x) {93failed(x.getMessage());94throw new RuntimeException(x);95}96}97}9899100