Path: blob/master/test/jdk/java/net/Socket/asyncClose/Socket_getOutputStream_write.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 Socket.getOutputStream().write()25* throws a SocketException if the socket is asynchronously closed.26*/27import java.net.*;28import java.io.*;29import java.util.concurrent.CountDownLatch;3031public class Socket_getOutputStream_write extends AsyncCloseTest implements Runnable {32private final Socket s;33private final CountDownLatch latch;3435public Socket_getOutputStream_write() {36latch = new CountDownLatch(1);37s = new Socket();38}3940public String description() {41return "Socket.getOutputStream().write()";42}4344public void run() {45try {46OutputStream out = s.getOutputStream();47byte b[] = new byte[8192];48latch.countDown();49for (;;) {50out.write(b);51}52} catch (SocketException se) {53if (latch.getCount() != 1) {54closed();55}56} catch (Exception e) {57failed(e.getMessage());58} finally {59if (latch.getCount() == 1) {60latch.countDown();61}62}63}6465public AsyncCloseTest go() {66try {67InetAddress lh = InetAddress.getLocalHost();68ServerSocket ss = new ServerSocket(0, 0, lh);69s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );70Socket s2 = ss.accept();71Thread thr = new Thread(this);72thr.start();73latch.await();74Thread.sleep(1000);75s.close();76thr.join();7778if (isClosed()) {79return passed();80} else {81return failed("Socket.getOutputStream().write() wasn't preempted");82}83} catch (Exception x) {84failed(x.getMessage());85throw new RuntimeException(x);86}87}88}899091