1/*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @summary Test asynchronous close during a blocking write
26 * @key randomness
27 */
28
29import java.io.Closeable;
30import java.io.IOException;
31import java.nio.ByteBuffer;
32import java.nio.channels.*;
33import java.net.*;
34import java.util.concurrent.*;
35import java.util.Random;
36
37public class CloseDuringWrite {
38
39    static final Random rand = new Random();
40
41    /**
42     * A task that closes a Closeable
43     */
44    static class Closer implements Callable<Void> {
45        final Closeable c;
46        Closer(Closeable c) {
47            this.c = c;
48        }
49        public Void call() throws IOException {
50            c.close();
51            return null;
52        }
53    }
54
55    public static void main(String[] args) throws Exception {
56        ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
57        try {
58            try (ServerSocketChannel ssc = ServerSocketChannel.open()) {
59                ssc.bind(new InetSocketAddress(0));
60                InetAddress lh = InetAddress.getLocalHost();
61                int port = ssc.socket().getLocalPort();
62                SocketAddress sa = new InetSocketAddress(lh, port);
63
64                ByteBuffer bb = ByteBuffer.allocate(2*1024*1024);
65
66                for (int i=0; i<20; i++) {
67                    try (SocketChannel source = SocketChannel.open(sa);
68                         SocketChannel sink = ssc.accept())
69                    {
70                        // schedule channel to be closed
71                        Closer c = new Closer(source);
72                        int when = 1000 + rand.nextInt(2000);
73                        Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS);
74
75                        // the write should either succeed or else throw a
76                        // ClosedChannelException (more likely an
77                        // AsynchronousCloseException)
78                        try {
79                            for (;;) {
80                                int limit = rand.nextInt(bb.capacity());
81                                bb.position(0);
82                                bb.limit(limit);
83                                int n = source.write(bb);
84                                System.out.format("wrote %d, expected %d%n", n, limit);
85                            }
86                        } catch (ClosedChannelException expected) {
87                            System.out.println(expected + " (expected)");
88                        } finally {
89                            result.get();
90                        }
91                    }
92                }
93            }
94        } finally {
95            pool.shutdown();
96        }
97    }
98}
99