test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/RawChannelTest.java
author chegar
Thu, 08 Mar 2018 17:42:16 +0000
branchhttp-client-branch
changeset 56265 ec34ae013fbe
parent 56233 1753108d07b9
child 56326 63422db47911
permissions -rw-r--r--
http-client-branch: tests should bind to the loopback only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     1
/*
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     4
 *
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     8
 *
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    13
 * accompanied this code).
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    14
 *
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    18
 *
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    21
 * questions.
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    22
 */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    23
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    24
package jdk.internal.net.http;
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    25
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    26
import java.io.IOException;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    27
import java.io.InputStream;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    28
import java.io.OutputStream;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    29
import java.io.UncheckedIOException;
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
    30
import java.net.InetAddress;
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    31
import java.net.InetSocketAddress;
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    32
import java.net.ServerSocket;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    33
import java.net.Socket;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    34
import java.net.URI;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    35
import java.nio.ByteBuffer;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    36
import java.nio.channels.SelectionKey;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    37
import java.util.Random;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    38
import java.util.concurrent.CountDownLatch;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    39
import java.util.concurrent.TimeUnit;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    40
import java.util.concurrent.atomic.AtomicInteger;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicLong;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    42
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    43
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    44
import java.net.http.HttpResponse;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    45
import jdk.internal.net.http.websocket.RawChannel;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    46
import jdk.internal.net.http.websocket.WebSocketRequest;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 48083
diff changeset
    47
import org.testng.annotations.Test;
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56092
diff changeset
    48
import static java.net.http.HttpResponse.BodyHandlers.discarding;
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    49
import static org.testng.Assert.assertEquals;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    50
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    51
/*
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    52
 * This test exercises mechanics of _independent_ reads and writes on the
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    53
 * RawChannel. It verifies that the underlying implementation can manage more
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    54
 * than a single type of notifications at the same time.
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    55
 */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    56
public class RawChannelTest {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    57
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    58
    private final AtomicLong clientWritten = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    59
    private final AtomicLong serverWritten = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    60
    private final AtomicLong clientRead = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    61
    private final AtomicLong serverRead = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    62
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    63
    /*
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    64
     * Since at this level we don't have any control over the low level socket
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    65
     * parameters, this latch ensures a write to the channel will stall at least
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    66
     * once (socket's send buffer filled up).
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    67
     */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    68
    private final CountDownLatch writeStall = new CountDownLatch(1);
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
    69
    private final CountDownLatch initialWriteStall = new CountDownLatch(1);
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    70
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    71
    /*
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    72
     * This one works similarly by providing means to ensure a read from the
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    73
     * channel will stall at least once (no more data available on the socket).
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    74
     */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    75
    private final CountDownLatch readStall = new CountDownLatch(1);
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
    76
    private final CountDownLatch initialReadStall = new CountDownLatch(1);
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    77
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    78
    private final AtomicInteger writeHandles = new AtomicInteger();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    79
    private final AtomicInteger readHandles = new AtomicInteger();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    80
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    81
    private final CountDownLatch exit = new CountDownLatch(1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    82
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    83
    @Test
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    84
    public void test() throws Exception {
56233
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    85
        try (ServerSocket server = new ServerSocket()) {
1753108d07b9 http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents: 56167
diff changeset
    86
            server.setReuseAddress(false);
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
    87
            server.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    88
            int port = server.getLocalPort();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    89
            new TestServer(server).start();
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
    90
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    91
            final RawChannel chan = channelOf(port);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
            print("RawChannel is %s", String.valueOf(chan));
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
    93
            initialWriteStall.await();
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    94
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    95
            // It's very important not to forget the initial bytes, possibly
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    96
            // left from the HTTP thingy
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    97
            int initialBytes = chan.initialByteBuffer().remaining();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    98
            print("RawChannel has %s initial bytes", initialBytes);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    99
            clientRead.addAndGet(initialBytes);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   100
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   101
            // tell the server we have read the initial bytes, so
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   102
            // that it makes sure there is something for us to
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   103
            // read next in case the initialBytes have already drained the
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   104
            // channel dry.
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   105
            initialReadStall.countDown();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   106
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   107
            chan.registerEvent(new RawChannel.RawEvent() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   108
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   109
                private final ByteBuffer reusableBuffer = ByteBuffer.allocate(32768);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   110
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   111
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   112
                public int interestOps() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   113
                    return SelectionKey.OP_WRITE;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   114
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   115
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   116
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   117
                public void handle() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   118
                    int i = writeHandles.incrementAndGet();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   119
                    print("OP_WRITE #%s", i);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   120
                    if (i > 3) { // Fill up the send buffer not more than 3 times
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   121
                        try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   122
                            chan.shutdownOutput();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   123
                        } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   124
                            e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   125
                        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   126
                        return;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   127
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   128
                    long total = 0;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   129
                    try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   130
                        long n;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   131
                        do {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   132
                            ByteBuffer[] array = {reusableBuffer.slice()};
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   133
                            n = chan.write(array, 0, 1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   134
                            total += n;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   135
                        } while (n > 0);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   136
                        print("OP_WRITE clogged SNDBUF with %s bytes", total);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   137
                        clientWritten.addAndGet(total);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   138
                        chan.registerEvent(this);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   139
                        writeStall.countDown(); // signal send buffer is full
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   140
                    } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   141
                        throw new UncheckedIOException(e);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   142
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   143
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   144
            });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   145
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   146
            chan.registerEvent(new RawChannel.RawEvent() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   147
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   148
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   149
                public int interestOps() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   150
                    return SelectionKey.OP_READ;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   151
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   152
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   153
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   154
                public void handle() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   155
                    int i = readHandles.incrementAndGet();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   156
                    print("OP_READ #%s", i);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   157
                    ByteBuffer read = null;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   158
                    long total = 0;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   159
                    while (true) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   160
                        try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   161
                            read = chan.read();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   162
                        } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   163
                            e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   164
                        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   165
                        if (read == null) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   166
                            print("OP_READ EOF");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   167
                            break;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   168
                        } else if (!read.hasRemaining()) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   169
                            print("OP_READ stall");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   170
                            try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   171
                                chan.registerEvent(this);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   172
                            } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   173
                                e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   174
                            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   175
                            readStall.countDown();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   176
                            break;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   177
                        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   178
                        int r = read.remaining();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   179
                        total += r;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   180
                        clientRead.addAndGet(r);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   181
                    }
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   182
                    print("OP_READ read %s bytes (%s total)", total, clientRead.get());
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   183
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   184
            });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   185
            exit.await(); // All done, we need to compare results:
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   186
            assertEquals(clientRead.get(), serverWritten.get());
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   187
            assertEquals(serverRead.get(), clientWritten.get());
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   188
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   189
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   190
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   191
    private static RawChannel channelOf(int port) throws Exception {
56265
ec34ae013fbe http-client-branch: tests should bind to the loopback only
chegar
parents: 56233
diff changeset
   192
        URI uri = URI.create("http://localhost:" + port + "/");
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   193
        print("raw channel to %s", uri.toString());
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   194
        HttpRequest req = HttpRequest.newBuilder(uri).build();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
        // Switch on isWebSocket flag to prevent the connection from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   196
        // being returned to the pool.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
        ((WebSocketRequest)req).isWebSocket(true);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   198
        HttpClient client = HttpClient.newHttpClient();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
        try {
56167
96fa4f49a9ff http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents: 56092
diff changeset
   200
            HttpResponse<?> r = client.send(req, discarding());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
            r.body();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   202
            return ((HttpResponseImpl) r).rawChannel();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   203
        } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
           // Need to hold onto the client until the RawChannel is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   205
           // created. This would not be needed if we had created
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   206
           // a WebSocket, but here we are fiddling directly
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   207
           // with the internals of HttpResponseImpl!
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   208
           java.lang.ref.Reference.reachabilityFence(client);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   209
        }
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   210
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   211
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   212
    private class TestServer extends Thread { // Powered by Slowpokes
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   213
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   214
        private final ServerSocket server;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   215
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   216
        TestServer(ServerSocket server) throws IOException {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   217
            this.server = server;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   218
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   219
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   220
        @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   221
        public void run() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   222
            try (Socket s = server.accept()) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   223
                InputStream is = s.getInputStream();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   224
                OutputStream os = s.getOutputStream();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   225
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   226
                processHttp(is, os);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   227
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   228
                Thread reader = new Thread(() -> {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   229
                    try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   230
                        long n = readSlowly(is);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   231
                        print("Server read %s bytes", n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   232
                        serverRead.addAndGet(n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   233
                        s.shutdownInput();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   234
                    } catch (Exception e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   235
                        e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   236
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   237
                });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   238
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   239
                Thread writer = new Thread(() -> {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   240
                    try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   241
                        long n = writeSlowly(os);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   242
                        print("Server written %s bytes", n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   243
                        serverWritten.addAndGet(n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   244
                        s.shutdownOutput();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   245
                    } catch (Exception e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   246
                        e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   247
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   248
                });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   249
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   250
                reader.start();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   251
                writer.start();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   252
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   253
                reader.join();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   254
                writer.join();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   255
            } catch (Exception e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   256
                e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   257
            } finally {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   258
                exit.countDown();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   259
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   260
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   261
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   262
        private void processHttp(InputStream is, OutputStream os)
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   263
                throws IOException
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   264
        {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   265
            os.write("HTTP/1.1 200 OK\r\nContent-length: 0\r\n\r\n".getBytes());
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   266
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   267
            // write some initial bytes
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   268
            byte[] initial = byteArrayOfSize(1024);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   269
            os.write(initial);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   270
            os.flush();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   271
            serverWritten.addAndGet(initial.length);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   272
            initialWriteStall.countDown();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   273
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   274
            byte[] buf = new byte[1024];
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   275
            String s = "";
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   276
            while (true) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   277
                int n = is.read(buf);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   278
                if (n <= 0) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   279
                    throw new RuntimeException("Unexpected end of request");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   280
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   281
                s = s + new String(buf, 0, n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   282
                if (s.contains("\r\n\r\n")) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   283
                    break;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   284
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   285
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   286
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   287
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   288
        private long writeSlowly(OutputStream os) throws Exception {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   289
            byte[] first = byteArrayOfSize(1024);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   290
            long total = first.length;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   291
            os.write(first);
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   292
            os.flush();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   293
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   294
            // wait until initial bytes were read
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   295
            initialReadStall.await();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   296
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   297
            // make sure there is something to read, otherwise readStall
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   298
            // will never be counted down.
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   299
            first = byteArrayOfSize(1024);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   300
            os.write(first);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   301
            os.flush();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   302
            total += first.length;
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   303
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   304
            // Let's wait for the signal from the raw channel that its read has
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   305
            // stalled, and then continue sending a bit more stuff
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   306
            readStall.await();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   307
            for (int i = 0; i < 32; i++) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   308
                byte[] b = byteArrayOfSize(1024);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   309
                os.write(b);
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   310
                os.flush();
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   311
                total += b.length;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   312
                TimeUnit.MILLISECONDS.sleep(1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   313
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   314
            return total;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   315
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   316
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   317
        private long readSlowly(InputStream is) throws Exception {
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   318
            // Wait for the raw channel to fill up its send buffer
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   319
            writeStall.await();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   320
            long overall = 0;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   321
            byte[] array = new byte[1024];
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   322
            for (int n = 0; n != -1; n = is.read(array)) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   323
                TimeUnit.MILLISECONDS.sleep(1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   324
                overall += n;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   325
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   326
            return overall;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   327
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   328
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   329
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   330
    private static void print(String format, Object... args) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   331
        System.out.println(Thread.currentThread() + ": " + String.format(format, args));
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   332
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   333
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   334
    private static byte[] byteArrayOfSize(int bound) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   335
        return new byte[new Random().nextInt(1 + bound)];
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   336
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   337
}