Path: blob/master/test/jdk/java/net/Socket/SocketAcceptInterruptTest.java
41152 views
/*1* Copyright (c) 2020, 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* @test25* @bug 823785826* @summary PlainSocketImpl.socketAccept() handles EINTR incorrectly27* @requires (os.family != "windows")28* @compile NativeThread.java29* @run main/othervm/native -Djdk.net.usePlainSocketImpl=true SocketAcceptInterruptTest 030* @run main/othervm/native -Djdk.net.usePlainSocketImpl=true SocketAcceptInterruptTest 500031* @run main/othervm/native SocketAcceptInterruptTest 032* @run main/othervm/native SocketAcceptInterruptTest 500033*/34import java.io.IOException;35import java.io.InputStream;36import java.io.OutputStream;37import java.net.InetAddress;38import java.net.ServerSocket;39import java.net.*;40import java.util.concurrent.Callable;41import java.util.concurrent.ExecutorService;42import java.util.concurrent.Executors;43import java.util.concurrent.Future;4445public class SocketAcceptInterruptTest {4647public static void main(String[] args) throws Exception {48System.loadLibrary("NativeThread");49InetAddress loopback = InetAddress.getLoopbackAddress();50ExecutorService executor = Executors.newFixedThreadPool(1);5152try ( ServerSocket ss = new ServerSocket(0, 50, loopback);) {53Server server = new Server(ss, Integer.parseInt(args[0]));54Future<Result> future = executor.submit(server);55long threadId = server.getID();5657sendSignal(threadId, ss);58sleep(100);59// In failing case server socket will be closed, so we do need to check first60if (!ss.isClosed()) {61// After sending SIGPIPE, create client socket and connect to server62try ( Socket s = new Socket(loopback, ss.getLocalPort()); InputStream in = s.getInputStream();) {63in.read(); // reading one byte is enought for test.64}65}66Result result = future.get();67if (result.status == Result.FAIL) {68throw result.exception;69}70} finally {71executor.shutdown();72}73System.out.println("OK!");74}7576private static void sendSignal(long threadId, ServerSocket ss) {77System.out.println("Sending SIGPIPE to ServerSocket thread.");78int count = 0;79while (!ss.isClosed() && count++ < 20) {80sleep(10);81if (NativeThread.signal(threadId, NativeThread.SIGPIPE) != 0) {82throw new RuntimeException("Failed to interrupt the server thread.");83}84}85}8687private static void sleep(long time) {88try {89Thread.sleep(time);90} catch (InterruptedException e) {91// ignore the exception.92}93}9495static class Server implements Callable<Result> {9697private volatile long threadId;98private final ServerSocket serverSocket;99private final int timeout;100101public Server(ServerSocket ss, int timeout) {102serverSocket = ss;103this.timeout = timeout;104}105106@Override107public Result call() {108try {109threadId = NativeThread.getID();110serverSocket.setSoTimeout(timeout);111try ( Socket socket = serverSocket.accept();112OutputStream outputStream = socket.getOutputStream();) {113outputStream.write("Hello!".getBytes());114return new Result(Result.SUCCESS, null);115}116} catch (IOException e) {117close();118return new Result(Result.FAIL, e);119}120}121122long getID() {123while (threadId == 0) {124sleep(5);125}126return threadId;127}128129private void close() {130if (!serverSocket.isClosed()) {131try {132serverSocket.close();133} catch (IOException ex) {134// ignore the exception135}136}137}138}139140static class Result {141142static final int SUCCESS = 0;143static final int FAIL = 1;144final int status;145final Exception exception;146147public Result(int status, Exception ex) {148this.status = status;149exception = ex;150}151}152}153154155