1 /*
   2  * Copyright (c) 2018, 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  * @bug 8195160
  26  * @summary Test asynchronous close during a blocking write
  27  * @requires (os.family == "linux")
  28  * @library .. /test/lib
  29  * @build RsocketTest
  30  * @run main/othervm -Djava.net.preferIPv4Stack=true CloseDuringWrite
  31  * @key randomness
  32  */
  33 import java.io.Closeable;
  34 import java.io.IOException;
  35 import java.nio.ByteBuffer;
  36 import java.nio.channels.*;
  37 import java.net.*;
  38 import java.util.concurrent.*;
  39 import java.util.Random;
  40 import jdk.net.Sockets;
  41 
  42 public class CloseDuringWrite {
  43 
  44     static final Random rand = new Random();
  45     static ServerSocketChannel ssc;
  46     static SocketChannel source;
  47     static SocketAddress sa;
  48 
  49     /**
  50      * A task that closes a Closeable
  51      */
  52     static class Closer implements Callable<Void> {
  53         final Closeable c;
  54         Closer(Closeable c) {
  55             this.c = c;
  56         }
  57         public Void call() throws IOException {
  58             c.close();
  59             return null;
  60         }
  61     }
  62 
  63     public static void main(String[] args) throws Exception {
  64         if (!RsocketTest.isRsocketAvailable())
  65             return;
  66 
  67         ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
  68         try {
  69             try {
  70                 ssc = Sockets.openRdmaServerSocketChannel();
  71                 InetAddress lh = InetAddress.getLocalHost();
  72                 ssc.bind(new InetSocketAddress(lh, 0));
  73                 int port = ssc.socket().getLocalPort();
  74                 sa = new InetSocketAddress(lh, port);
  75 
  76                 ByteBuffer bb = ByteBuffer.allocate(2 * 1024 * 1024);
  77 
  78                 for (int i = 0; i < 20; i++) {
  79                     try {
  80                         source = Sockets.openRdmaSocketChannel();
  81                         Runnable runnable = new Runnable() {
  82                             @Override
  83                             public void run() {
  84                                 try {
  85                                     source.connect(sa);
  86                                 } catch (Exception e) {
  87                                     e.printStackTrace();
  88                                     throw new RuntimeException("Test Failed");
  89                                 }
  90                             }
  91                         };
  92 
  93                         Thread t = new Thread(runnable);
  94                         t.start();
  95                         SocketChannel sink = ssc.accept();
  96                         // schedule channel to be closed
  97                         Closer c = new Closer(source);
  98                         int when = 1000 + rand.nextInt(2000);
  99                         Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS);
 100 
 101                         // the write should either succeed or else throw a
 102                         // ClosedChannelException (more likely an
 103                         // AsynchronousCloseException)
 104                         try {
 105                             for (;;) {
 106                                 int limit = rand.nextInt(bb.capacity());
 107                                 bb.position(0);
 108                                 bb.limit(limit);
 109                                 int n = source.write(bb);
 110                                 System.out.format("wrote %d, expected %d%n", n, limit);
 111                             }
 112                         } catch (ClosedChannelException expected) {
 113                             System.out.println(expected + " (expected)");
 114                         } finally {
 115                             result.get();
 116                         }
 117                     } catch (Exception e) {
 118                         e.printStackTrace();
 119                         throw new RuntimeException("Test Failed");
 120                     }
 121                 }
 122             } catch (Exception e) {
 123                 e.printStackTrace();
 124                 throw new RuntimeException("Test Failed");
 125             }
 126         } finally {
 127             pool.shutdown();
 128         }
 129     }
 130 }