Path: blob/master/test/jdk/java/net/Socket/AsyncShutdown.java
41149 views
/*1* Copyright (c) 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* @test25* @requires (os.family == "linux" | os.family == "mac")26* @run testng AsyncShutdown27* @summary Test shutdownInput/shutdownOutput with threads blocked in read/write28*/2930import java.io.IOException;31import java.net.InetAddress;32import java.net.InetSocketAddress;33import java.net.ServerSocket;34import java.net.Socket;35import java.net.SocketTimeoutException;36import java.util.concurrent.Executors;37import java.util.concurrent.ScheduledExecutorService;38import java.util.concurrent.TimeUnit;3940import org.testng.annotations.Test;41import static org.testng.Assert.*;4243@Test44public class AsyncShutdown {4546public void testShutdownInput1() throws IOException {47withConnection((s1, s2) -> {48scheduleShutdownInput(s1, 2000);49int n = s1.getInputStream().read();50assertTrue(n == -1);51});52}5354public void testShutdownInput2() throws IOException {55withConnection((s1, s2) -> {56scheduleShutdownInput(s1, 2000);57s1.setSoTimeout(30*1000);58int n = s1.getInputStream().read();59assertTrue(n == -1);60});61}6263public void testShutdownOutput1() throws IOException {64withConnection((s1, s2) -> {65scheduleShutdownOutput(s1, 2000);66byte[] data = new byte[128*1024];67try {68while (true) {69s1.getOutputStream().write(data);70}71} catch (IOException expected) { }72});73}7475public void testShutdownOutput2() throws IOException {76withConnection((s1, s2) -> {77s1.setSoTimeout(100);78try {79s1.getInputStream().read();80assertTrue(false);81} catch (SocketTimeoutException e) { }8283scheduleShutdownOutput(s1, 2000);84byte[] data = new byte[128*1024];85try {86while (true) {87s1.getOutputStream().write(data);88}89} catch (IOException expected) { }90});91}9293static void scheduleShutdownInput(Socket s, long delay) {94schedule(() -> {95try {96s.shutdownInput();97} catch (IOException ioe) { }98}, delay);99}100101static void scheduleShutdownOutput(Socket s, long delay) {102schedule(() -> {103try {104s.shutdownOutput();105} catch (IOException ioe) { }106}, delay);107}108109static void schedule(Runnable task, long delay) {110ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();111try {112executor.schedule(task, delay, TimeUnit.MILLISECONDS);113} finally {114executor.shutdown();115}116}117118interface ThrowingBiConsumer<T, U> {119void accept(T t, U u) throws IOException;120}121122static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)123throws IOException124{125Socket s1 = null;126Socket s2 = null;127try (ServerSocket ss = createBoundServer()) {128s1 = new Socket();129s1.connect(ss.getLocalSocketAddress());130s2 = ss.accept();131consumer.accept(s1, s2);132} finally {133if (s1 != null) s1.close();134if (s2 != null) s2.close();135}136}137138static ServerSocket createBoundServer() throws IOException {139ServerSocket ss = new ServerSocket();140InetAddress loopback = InetAddress.getLoopbackAddress();141InetSocketAddress address = new InetSocketAddress(loopback, 0);142ss.bind(address);143return ss;144}145146}147148149