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  */
  27 
  28 import java.io.Closeable;
  29 import java.io.IOException;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.*;
  32 import java.net.*;
  33 import java.util.concurrent.*;
  34 import java.util.Random;
  35 
  36 public class CloseDuringWrite {
  37 
  38     static final Random rand = new Random();
  39 
  40     /**
  41      * A task that closes a Closeable
  42      */
  43     static class Closer implements Callable<Void> {
  44         final Closeable c;
  45         Closer(Closeable c) {
  46             this.c = c;
  47         }
  48         public Void call() throws IOException {
  49             c.close();
  50             return null;
  51         }
  52     }
  53 
  54     public static void main(String[] args) throws Exception {
  55         ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
  56         try {
  57             try (ServerSocketChannel ssc = ServerSocketChannel.open()) {
  58                 ssc.bind(new InetSocketAddress(0));
  59                 InetAddress lh = InetAddress.getLocalHost();
  60                 int port = ssc.socket().getLocalPort();
  61                 SocketAddress sa = new InetSocketAddress(lh, port);
  62 
  63                 ByteBuffer bb = ByteBuffer.allocate(2*1024*1024);
  64 
  65                 for (int i=0; i<20; i++) {
  66                     try (SocketChannel source = SocketChannel.open(sa);
  67                          SocketChannel sink = ssc.accept())
  68                     {
  69                         // schedule channel to be closed
  70                         Closer c = new Closer(source);
  71                         int when = 1000 + rand.nextInt(2000);
  72                         Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS);
  73 
  74                         // the write should either succeed or else throw a
  75                         // ClosedChannelException (more likely an
  76                         // AsynchronousCloseException)
  77                         try {
  78                             for (;;) {
  79                                 int limit = rand.nextInt(bb.capacity());
  80                                 bb.position(0);
  81                                 bb.limit(limit);
  82                                 int n = source.write(bb);
  83                                 System.out.format("wrote %d, expected %d%n", n, limit);
  84                             }
  85                         } catch (ClosedChannelException expected) {
  86                             System.out.println(expected + " (expected)");
  87                         } finally {
  88                             result.get();
  89                         }
  90                     }
  91                 }
  92             }
  93         } finally {
  94             pool.shutdown();
  95         }
  96     }
  97 }
--- EOF ---