5155N/A/*
5155N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
5155N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5155N/A *
5155N/A * This code is free software; you can redistribute it and/or modify it
5155N/A * under the terms of the GNU General Public License version 2 only, as
5155N/A * published by the Free Software Foundation.
5155N/A *
5155N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5155N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5155N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5155N/A * version 2 for more details (a copy is included in the LICENSE file that
5155N/A * accompanied this code).
5155N/A *
5155N/A * You should have received a copy of the GNU General Public License version
5155N/A * 2 along with this work; if not, write to the Free Software Foundation,
5155N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5155N/A *
5155N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5155N/A * or visit www.oracle.com if you need additional information or have any
5155N/A * questions.
5155N/A */
5155N/A
5155N/A/* @test
5155N/A * @summary Test asynchronous close during a blocking write
5155N/A */
5155N/A
5155N/Aimport java.io.Closeable;
5155N/Aimport java.io.IOException;
5155N/Aimport java.nio.ByteBuffer;
5155N/Aimport java.nio.channels.*;
5155N/Aimport java.net.*;
5155N/Aimport java.util.concurrent.*;
5155N/Aimport java.util.Random;
5155N/A
5155N/Apublic class CloseDuringWrite {
5155N/A
5155N/A static final Random rand = new Random();
5155N/A
5155N/A /**
5155N/A * A task that closes a Closeable
5155N/A */
5155N/A static class Closer implements Callable<Void> {
5155N/A final Closeable c;
5155N/A Closer(Closeable c) {
5155N/A this.c = c;
5155N/A }
5155N/A public Void call() throws IOException {
5155N/A c.close();
5155N/A return null;
5155N/A }
5155N/A }
5155N/A
5155N/A public static void main(String[] args) throws Exception {
5155N/A ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
5155N/A try {
5155N/A try (ServerSocketChannel ssc = ServerSocketChannel.open()) {
5155N/A ssc.bind(new InetSocketAddress(0));
5155N/A InetAddress lh = InetAddress.getLocalHost();
5155N/A int port = ssc.socket().getLocalPort();
5155N/A SocketAddress sa = new InetSocketAddress(lh, port);
5155N/A
5155N/A ByteBuffer bb = ByteBuffer.allocate(2*1024*1024);
5155N/A
5155N/A for (int i=0; i<20; i++) {
5155N/A try (SocketChannel source = SocketChannel.open(sa);
5155N/A SocketChannel sink = ssc.accept())
5155N/A {
5155N/A // schedule channel to be closed
5155N/A Closer c = new Closer(source);
5155N/A int when = 1000 + rand.nextInt(2000);
5155N/A Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS);
5155N/A
5155N/A // the write should either succeed or else throw a
5155N/A // ClosedChannelException (more likely an
5155N/A // AsynchronousCloseException)
5155N/A try {
5155N/A for (;;) {
5155N/A int limit = rand.nextInt(bb.capacity());
5155N/A bb.position(0);
5155N/A bb.limit(limit);
5155N/A int n = source.write(bb);
5155N/A System.out.format("wrote %d, expected %d%n", n, limit);
5155N/A }
5155N/A } catch (ClosedChannelException expected) {
5155N/A System.out.println(expected + " (expected)");
5155N/A } finally {
5155N/A result.get();
5155N/A }
5155N/A }
5155N/A }
5155N/A }
5155N/A } finally {
5155N/A pool.shutdown();
5155N/A }
5155N/A }
5155N/A}