test/jdk/java/nio/channels/SocketChannel/ConnectState.java
author mli
Fri, 16 Mar 2018 18:15:31 +0800
changeset 49255 acdb8531cc8b
parent 47216 71c04702a3d5
permissions -rw-r--r--
8199215: Re-examine getFreePort method in test infrastructure library Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Test socket-channel connection-state transitions
49255
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    26
 * @library .. /test/lib
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    27
 * @build jdk.test.lib.Utils TestServers
acdb8531cc8b 8199215: Re-examine getFreePort method in test infrastructure library
mli
parents: 47216
diff changeset
    28
 * @run main ConnectState
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.*;
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    35
import java.util.Arrays;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    36
import java.util.Collection;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    37
import java.util.Collections;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    38
import java.util.HashSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class ConnectState {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static InetSocketAddress remote;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    final static int ST_UNCONNECTED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    final static int ST_PENDING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    final static int ST_CONNECTED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    final static int ST_CLOSED = 3;
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    51
    final static int ST_PENDING_OR_CONNECTED = 4;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    52
    // NO exceptions expected
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    53
    final static Collection<Class<?>> NONE = Collections.emptySet();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    54
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    55
    // make a set of expected exception.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    56
    static Collection<Class<?>> expectedExceptions(Class<?>... expected) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    57
        final Collection<Class<?>> exceptions;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    58
        if (expected.length == 0) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    59
            exceptions = NONE;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    60
        } else if (expected.length == 1) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    61
            assert expected[0] != null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    62
            exceptions = Collections.<Class<?>>singleton(expected[0]);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    63
        } else {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    64
            exceptions = new HashSet<>(Arrays.asList(expected));
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    65
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    66
        return exceptions;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
    67
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static abstract class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        abstract String go(SocketChannel sc) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        static void check(boolean test, String desc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (!test)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                throw new Exception("Incorrect state: " + desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        static void check(SocketChannel sc, int state) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            case ST_UNCONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                check(!sc.isConnected(), "!isConnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                check(!sc.isConnectionPending(), "!isConnectionPending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                check(sc.isOpen(), "isOpen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            case ST_PENDING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                check(!sc.isConnected(), "!isConnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                check(sc.isConnectionPending(), "isConnectionPending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                check(sc.isOpen(), "isOpen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            case ST_CONNECTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                check(sc.isConnected(), "isConnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                check(!sc.isConnectionPending(), "!isConnectionPending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                check(sc.isOpen(), "isOpen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            case ST_CLOSED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                check(sc.isConnected(), "isConnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                check(!sc.isConnectionPending(), "!isConnectionPending");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                check(sc.isOpen(), "isOpen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                break;
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   100
            case ST_PENDING_OR_CONNECTED:
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   101
                check(sc.isConnected() || sc.isConnectionPending(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   102
                        "isConnected || isConnectionPending");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   103
                check(sc.isOpen(), "isOpen");
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   104
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   108
        Test(String name, Class<?> exception, int state) throws Exception {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   109
            this(name, expectedExceptions(exception), state);
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   110
        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   111
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   112
        // On some architecture we may need to accept several exceptions.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   113
        // For instance on Solaris, when using a server colocated on the
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   114
        // machine we cannot guarantee that we will get a
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   115
        // ConnectionPendingException when connecting twice on the same
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   116
        // non-blocking socket. We may instead get a an
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   117
        // AlreadyConnectedException, which is also valid: it simply means
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   118
        // that the first connection has been immediately accepted.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   119
        Test(String name, Collection<Class<?>> exceptions, int state)
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   120
                throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            SocketChannel sc = SocketChannel.open();
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   122
            String note;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    note = go(sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } catch (Exception x) {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   127
                    Class<?> expectedExceptionClass = null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   128
                    for (Class<?> exception : exceptions) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        if (exception.isInstance(x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                            log.println(name + ": As expected: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                        + x);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   132
                            expectedExceptionClass = exception;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            check(sc, state);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   134
                            break;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   135
                        }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   136
                    }
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   137
                    if (expectedExceptionClass == null
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   138
                            && !exceptions.isEmpty()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   139
                        // we had an exception, but it's not of the set of
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   140
                        // exceptions we expected.
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   141
                        throw new Exception(name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                + ": Incorrect exception",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                                x);
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   144
                    } else if (exceptions.isEmpty()) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   145
                        // we didn't expect any exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        throw new Exception(name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                            + ": Unexpected exception",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                            x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    }
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   150
                    // if we reach here, we have our expected exception
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   151
                    assert expectedExceptionClass != null;
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   152
                    return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   154
                if (!exceptions.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    throw new Exception(name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                        + ": Expected exception not thrown: "
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   157
                                        + exceptions.iterator().next());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   158
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                check(sc, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                log.println(name + ": Returned normally"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            + ((note != null) ? ": " + note : ""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (sc.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static void tests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        log.println(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        new Test("Read unconnected", NotYetConnectedException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                 ST_UNCONNECTED) {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   175
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    ByteBuffer b = ByteBuffer.allocateDirect(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    sc.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        new Test("Write unconnected", NotYetConnectedException.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 ST_UNCONNECTED) {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   184
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    ByteBuffer b = ByteBuffer.allocateDirect(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    sc.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   191
        new Test("Simple connect", NONE, ST_CONNECTED) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   192
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   198
        new Test("Simple connect & finish", NONE, ST_CONNECTED) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   199
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    if (!sc.finishConnect())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        throw new Exception("finishConnect returned false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        new Test("Double connect",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                 AlreadyConnectedException.class, ST_CONNECTED) {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   209
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        new Test("Finish w/o start",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                 NoConnectionPendingException.class, ST_UNCONNECTED) {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   218
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    sc.finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   224
        // Note: using our local EchoServer rather than echo on a distant
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   225
        //       host - we see that Tries to finish = 0 (instead of ~ 18).
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   226
        new Test("NB simple connect", NONE, ST_CONNECTED) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   227
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    while (!sc.finishConnect()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        Thread.sleep(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    sc.finishConnect();         // Check redundant invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    return ("Tries to finish = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   240
        // Note: using our local EchoServer rather than echo on a distant
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   241
        //       host - we cannot guarantee that this test will get a
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   242
        //       a ConnectionPendingException: it may get an
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   243
        //       AlreadyConnectedException, so we should allow for both.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        new Test("NB double connect",
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   245
                 expectedExceptions(ConnectionPendingException.class,
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   246
                                    AlreadyConnectedException.class),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   247
                 ST_PENDING_OR_CONNECTED) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   248
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        new Test("NB finish w/o start",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                 NoConnectionPendingException.class, ST_UNCONNECTED) {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   258
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    sc.finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   265
        new Test("NB connect, B finish", NONE, ST_CONNECTED) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   266
                @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                String go(SocketChannel sc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    sc.connect(remote);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    sc.configureBlocking(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    sc.finishConnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public static void main(String[] args) throws Exception {
14415
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   278
        try (TestServers.EchoServer echoServer
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   279
                = TestServers.EchoServer.startNewServer(500)) {
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   280
            remote = new InetSocketAddress(echoServer.getAddress(),
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   281
                                           echoServer.getPort());
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   282
            tests();
7a31b0e0cfaf 6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents: 5506
diff changeset
   283
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
}