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