test/jdk/jdk/net/RdmaSockets/rsocket/ServerSocketChannel/SocketOptionTests.java
author alanb
Sat, 26 Jan 2019 14:02:35 +0000
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
Initial load of JEP 337 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
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 Unit test for ServerSocketChannel setOption/getOption/options
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
 *          methods.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
 * @modules jdk.net
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
 * @requires !vm.graal.enabled
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
 * @requires (os.family == "linux")
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
 * @library .. /test/lib
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
 * @build RsocketTest
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
 * @run main/othervm SocketOptionTests
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
import java.io.IOException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
import java.net.StandardProtocolFamily;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
import java.net.SocketOption;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
import java.nio.channels.ClosedChannelException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
import java.nio.channels.ServerSocketChannel;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
import java.util.Set;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
import jdk.net.RdmaSockets;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
import static java.net.StandardSocketOptions.*;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
import static jdk.net.RdmaSocketOptions.*;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
import jtreg.SkippedException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
public class SocketOptionTests {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
    static void checkOption(ServerSocketChannel ssc, SocketOption name, Object expectedValue)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
        throws IOException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
    {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
        Object value = ssc.getOption(name);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
        if (!value.equals(expectedValue))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
            throw new RuntimeException("value not as expected");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
    public static void main(String[] args) throws IOException {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
        if (!RsocketTest.isRsocketAvailable())
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
            throw new SkippedException("rsocket is not available");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
        ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
            StandardProtocolFamily.INET);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
        // check supported options
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
        Set<SocketOption<?>> options = ssc.supportedOptions();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
        if (!options.contains(SO_REUSEADDR))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
            throw new RuntimeException("SO_REUSEADDR should be supported");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
        if (!options.contains(SO_RCVBUF))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
            throw new RuntimeException("SO_RCVBUF should be supported");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
        if (!options.contains(RDMA_SQSIZE))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
            throw new RuntimeException("RDMA_SQSIZE should be supported");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
        if (!options.contains(RDMA_RQSIZE))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
            throw new RuntimeException("RDMA_RQSIZE should be supported");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
        if (!options.contains(RDMA_INLINE))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
            throw new RuntimeException("RDMA_INLINE should be supported");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
        // allowed to change when not bound
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        ssc.setOption(SO_RCVBUF, 256*1024);     // can't check
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
        int before = ssc.getOption(SO_RCVBUF);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
        int after = ssc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
        if (after < before)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
            throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
        ssc.setOption(SO_REUSEADDR, true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
        checkOption(ssc, SO_REUSEADDR, true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
        ssc.setOption(SO_REUSEADDR, false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
        checkOption(ssc, SO_REUSEADDR, false);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
        // NullPointerException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
            ssc.setOption(null, "value");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
            throw new RuntimeException("NullPointerException not thrown");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
        } catch (NullPointerException x) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
            ssc.getOption(null);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
            throw new RuntimeException("NullPointerException not thrown");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
        } catch (NullPointerException x) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
        // ClosedChannelException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
        ssc.close();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
            ssc.setOption(SO_REUSEADDR, true);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
            throw new RuntimeException("ClosedChannelException not thrown");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
        } catch (ClosedChannelException x) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
}