test/jdk/jdk/net/RdmaSockets/rsocket/SocketChannel/CloseDuringWrite.java
author bpb
Thu, 07 Feb 2019 11:09:09 -0800
branchrsocket-branch
changeset 57160 c502c299d41e
parent 57156 81e4a12fd1a4
permissions -rw-r--r--
rsocket-branch: correct copyright year
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57160
c502c299d41e rsocket-branch: correct copyright year
bpb
parents: 57156
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
/* @test
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
 * @bug 8195160
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
 * @summary Test asynchronous close during a blocking write
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
 * @library .. /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
 * @build RsocketTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
 * @run main/othervm CloseDuringWrite
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
 * @key randomness
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
import java.io.Closeable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.net.Inet6Address;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.InetAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.net.InetSocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.net.ProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.net.SocketAddress;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.nio.ByteBuffer;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import java.nio.channels.ClosedChannelException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import java.nio.channels.ServerSocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import java.nio.channels.SocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
import java.util.concurrent.Callable;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
import java.util.concurrent.Executors;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
import java.util.concurrent.Future;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
import java.util.concurrent.ScheduledExecutorService;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
import java.util.concurrent.TimeUnit;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
import java.util.Random;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
import static java.net.StandardProtocolFamily.INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
import static java.net.StandardProtocolFamily.INET6;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
public class CloseDuringWrite {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
    static final Random rand = new Random();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
    /**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
     * A task that closes a Closeable
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
     */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
    static class Closer implements Callable<Void> {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
        final Closeable c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
        Closer(Closeable c) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
            this.c = c;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
        public Void call() throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
            c.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
            return null;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
    public static void main(String[] args) throws Exception {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
        InetAddress lh = InetAddress.getLocalHost();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        ProtocolFamily family = lh instanceof Inet6Address ? INET6 : INET;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
        System.out.printf("local address: %s%n", lh);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
        ScheduledExecutorService pool = Executors.newSingleThreadScheduledExecutor();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
        try (ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
            ssc.bind(new InetSocketAddress(lh, 0));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
            int port = ssc.socket().getLocalPort();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
            SocketAddress sa = new InetSocketAddress(lh, port);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
            ByteBuffer bb = ByteBuffer.allocate(2 * 1024 * 1024);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
            for (int i = 0; i < 20; i++) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
                System.out.println(i);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
                try (SocketChannel source = RdmaSockets.openSocketChannel(family)) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
                    Runnable runnable = new Runnable() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
                        @Override
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
                        public void run() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
                            try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
                                System.out.println("connecting ...");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
                                source.connect(sa);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
                                System.out.println("connected");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
                            } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
                                e.printStackTrace();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
                                throw new RuntimeException("Test Failed");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
                            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
                    };
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
                    Thread t = new Thread(runnable);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
                    t.start();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
                    try (SocketChannel sink = ssc.accept()) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   110
                        System.out.println("accepted new connection");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
                        // schedule channel to be closed
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
                        Closer c = new Closer(source);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
                        int when = 1000 + rand.nextInt(2000);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   114
                        Future<Void> result = pool.schedule(c, when, TimeUnit.MILLISECONDS);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
                        // the write should either succeed or else throw a
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
                        // ClosedChannelException (more likely an
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
                        // AsynchronousCloseException)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
                        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
                            for (;;) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
                                int limit = rand.nextInt(bb.capacity());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
                                bb.position(0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
                                bb.limit(limit);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
                                int n = source.write(bb);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
                                System.out.format("wrote %d, expected %d%n", n, limit);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   126
                            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
                        } catch (ClosedChannelException expected) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
                            System.out.println(expected + " (expected)");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
                        } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
                            result.get();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   131
                        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   132
                    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   133
                } catch (Exception e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   134
                    e.printStackTrace();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   135
                    throw new RuntimeException("Test Failed");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   136
                }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   137
            }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   138
        } finally {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   139
            pool.shutdown();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   140
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   141
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   142
}