test/jdk/java/net/httpclient/websocket/PendingOperations.java
author chegar
Thu, 26 Apr 2018 14:42:36 +0100
branchhttp-client-branch
changeset 56486 b664a887e277
parent 56451 9585061fdb04
permissions -rw-r--r--
http-client-branch: set NO_PROXY in several WebSocket tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
import org.testng.annotations.AfterTest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
import org.testng.annotations.DataProvider;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.net.http.WebSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.util.concurrent.Callable;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.util.concurrent.CompletionStage;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.util.function.BooleanSupplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
/* Common infrastructure for tests that check pending operations */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
public class PendingOperations {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
    static final Class<IllegalStateException> ISE = IllegalStateException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
    static final Class<IOException> IOE = IOException.class;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
    // Time after which we deem that the local send buffer and remote
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
    // receive buffer must be full. This has been heuristically determined.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
    // At the time of writing, using anything <= 5s on Mac will make the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
    // tests fail intermittently.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
    static final long MAX_WAIT_SEC = 10; // seconds.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
    DummyWebSocketServer server;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
    WebSocket webSocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
    @AfterTest
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
    public void cleanup() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
        server.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
        webSocket.abort();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
    /* shortcut */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
    static void assertHangs(CompletionStage<?> stage) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
        Support.assertHangs(stage);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
    /* shortcut */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
    static void assertFails(Class<? extends Throwable> clazz,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
                            CompletionStage<?> stage) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
        Support.assertCompletesExceptionally(clazz, stage);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
    @DataProvider(name = "booleans")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
    public Object[][] booleans() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
        return new Object[][]{{Boolean.TRUE}, {Boolean.FALSE}};
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
    static boolean isMacOS() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
        return System.getProperty("os.name").contains("OS X");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    private static final int ITERATIONS = 3;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
    static void repeatable(Callable<Void> callable,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
                           BooleanSupplier repeatCondition)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
        throws Exception
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
    {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
        int iterations = 0;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
        do {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
            iterations++;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
            System.out.println("--- iteration " + iterations + " ---");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
            try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
                callable.call();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
                break;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
            } catch (AssertionError e) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
                if (isMacOS() && repeatCondition.getAsBoolean()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
                    // ## This is loathsome, but necessary because of observed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
                    // ## automagic socket buffer resizing on recent macOS platforms
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
                    continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
                } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
                    throw e;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
        } while (iterations <= ITERATIONS);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
}