Path: blob/master/test/jdk/java/nio/channels/AsynchronousSocketChannel/DieBeforeComplete.java
41153 views
/*1* Copyright (c) 2008, 2009, 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 684268725* @summary Unit test for AsynchronousSocketChannel/AsynchronousServerSocketChannel26*/27import java.nio.ByteBuffer;28import java.nio.channels.*;29import java.net.*;30import java.util.concurrent.*;31import java.util.concurrent.atomic.AtomicReference;3233/**34* Initiates I/O operation on a thread that terminates before the I/O completes.35*/3637public class DieBeforeComplete {3839public static void main(String[] args) throws Exception {40final AsynchronousServerSocketChannel listener =41AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(0));4243InetAddress lh = InetAddress.getLocalHost();44int port = ((InetSocketAddress) (listener.getLocalAddress())).getPort();45final SocketAddress sa = new InetSocketAddress(lh, port);4647// -- accept --4849// initiate accept in a thread that dies before connection is established50Future<AsynchronousSocketChannel> r1 =51initiateAndDie(new Task<AsynchronousSocketChannel>() {52public Future<AsynchronousSocketChannel> run() {53return listener.accept();54}});5556// establish and accept connection57SocketChannel peer = SocketChannel.open(sa);58final AsynchronousSocketChannel channel = r1.get();5960// --- read --6162// initiate read in a thread that dies befores bytes are available63final ByteBuffer dst = ByteBuffer.allocate(100);64Future<Integer> r2 = initiateAndDie(new Task<Integer>() {65public Future<Integer> run() {66return channel.read(dst);67}});6869// send bytes70peer.write(ByteBuffer.wrap("hello".getBytes()));71int nread = r2.get();72if (nread <= 0)73throw new RuntimeException("Should have read at least one byte");7475// -- write --7677// initiate writes in threads that dies78boolean completedImmediately;79Future<Integer> r3;80do {81final ByteBuffer src = ByteBuffer.wrap(new byte[10000]);82r3 = initiateAndDie(new Task<Integer>() {83public Future<Integer> run() {84return channel.write(src);85}});86try {87int nsent = r3.get(5, TimeUnit.SECONDS);88if (nsent <= 0)89throw new RuntimeException("Should have wrote at least one byte");90completedImmediately = true;91} catch (TimeoutException x) {92completedImmediately = false;93}94} while (completedImmediately);9596// drain connection97peer.configureBlocking(false);98ByteBuffer src = ByteBuffer.allocateDirect(10000);99do {100src.clear();101nread = peer.read(src);102if (nread == 0) {103Thread.sleep(100);104nread = peer.read(src);105}106} while (nread > 0);107108// write should complete now109int nsent = r3.get();110if (nsent <= 0)111throw new RuntimeException("Should have wrote at least one byte");112}113114static interface Task<T> {115Future<T> run();116}117118static <T> Future<T> initiateAndDie(final Task<T> task) {119final AtomicReference<Future<T>> result = new AtomicReference<Future<T>>();120Runnable r = new Runnable() {121public void run() {122result.set(task.run());123}124};125Thread t = new Thread(r);126t.start();127while (t.isAlive()) {128try {129t.join();130} catch (InterruptedException x) {131}132}133return result.get();134}135}136137138