test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/RawChannelTest.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 test/jdk/java/net/httpclient/whitebox/java.net.http/java/net/http/internal/RawChannelTest.java@42208b2f224e
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
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;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    30
import java.net.ServerSocket;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    31
import java.net.Socket;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    32
import java.net.URI;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    33
import java.nio.ByteBuffer;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    34
import java.nio.channels.SelectionKey;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    35
import java.util.Random;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    36
import java.util.concurrent.CountDownLatch;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    37
import java.util.concurrent.TimeUnit;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    38
import java.util.concurrent.atomic.AtomicInteger;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    39
import java.util.concurrent.atomic.AtomicLong;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    40
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    41
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    42
import java.net.http.HttpResponse;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    43
import jdk.internal.net.http.websocket.RawChannel;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    44
import jdk.internal.net.http.websocket.WebSocketRequest;
56079
d23b02f37fce http-client-branch: more remaining impl types to internal
chegar
parents: 48083
diff changeset
    45
import org.testng.annotations.Test;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56082
diff changeset
    46
import static java.net.http.HttpResponse.BodyHandler.discard;
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    47
import static org.testng.Assert.assertEquals;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    48
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    49
/*
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    50
 * 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
    51
 * 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
    52
 * 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
    53
 */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    54
public class RawChannelTest {
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
    private final AtomicLong clientWritten = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    57
    private final AtomicLong serverWritten = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    58
    private final AtomicLong clientRead = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    59
    private final AtomicLong serverRead = new AtomicLong();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    60
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    61
    /*
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    62
     * 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
    63
     * 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
    64
     * once (socket's send buffer filled up).
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    65
     */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    66
    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
    67
    private final CountDownLatch initialWriteStall = new CountDownLatch(1);
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    68
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    69
    /*
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    70
     * 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
    71
     * 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
    72
     */
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    73
    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
    74
    private final CountDownLatch initialReadStall = new CountDownLatch(1);
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    75
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    76
    private final AtomicInteger writeHandles = new AtomicInteger();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    77
    private final AtomicInteger readHandles = new AtomicInteger();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    78
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    79
    private final CountDownLatch exit = new CountDownLatch(1);
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
    @Test
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    82
    public void test() throws Exception {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    83
        try (ServerSocket server = new ServerSocket(0)) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    84
            int port = server.getLocalPort();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    85
            new TestServer(server).start();
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
    86
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    87
            final RawChannel chan = channelOf(port);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
            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
    89
            initialWriteStall.await();
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    90
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    91
            // 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
    92
            // left from the HTTP thingy
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    93
            int initialBytes = chan.initialByteBuffer().remaining();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    94
            print("RawChannel has %s initial bytes", initialBytes);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    95
            clientRead.addAndGet(initialBytes);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
    96
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
    97
            // 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
    98
            // 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
    99
            // 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
   100
            // channel dry.
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   101
            initialReadStall.countDown();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   102
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   103
            chan.registerEvent(new RawChannel.RawEvent() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   104
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   105
                private final ByteBuffer reusableBuffer = ByteBuffer.allocate(32768);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   106
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   107
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   108
                public int interestOps() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   109
                    return SelectionKey.OP_WRITE;
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
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   112
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   113
                public void handle() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   114
                    int i = writeHandles.incrementAndGet();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   115
                    print("OP_WRITE #%s", i);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   116
                    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
   117
                        try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   118
                            chan.shutdownOutput();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   119
                        } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   120
                            e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   121
                        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   122
                        return;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   123
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   124
                    long total = 0;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   125
                    try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   126
                        long n;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   127
                        do {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   128
                            ByteBuffer[] array = {reusableBuffer.slice()};
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   129
                            n = chan.write(array, 0, 1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   130
                            total += n;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   131
                        } while (n > 0);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   132
                        print("OP_WRITE clogged SNDBUF with %s bytes", total);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   133
                        clientWritten.addAndGet(total);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   134
                        chan.registerEvent(this);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   135
                        writeStall.countDown(); // signal send buffer is full
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   136
                    } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   137
                        throw new UncheckedIOException(e);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   138
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   139
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   140
            });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   141
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   142
            chan.registerEvent(new RawChannel.RawEvent() {
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
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   145
                public int interestOps() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   146
                    return SelectionKey.OP_READ;
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
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   149
                @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   150
                public void handle() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   151
                    int i = readHandles.incrementAndGet();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   152
                    print("OP_READ #%s", i);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   153
                    ByteBuffer read = null;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   154
                    long total = 0;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   155
                    while (true) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   156
                        try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   157
                            read = chan.read();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   158
                        } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   159
                            e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   160
                        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   161
                        if (read == null) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   162
                            print("OP_READ EOF");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   163
                            break;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   164
                        } else if (!read.hasRemaining()) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   165
                            print("OP_READ stall");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   166
                            try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   167
                                chan.registerEvent(this);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   168
                            } catch (IOException e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   169
                                e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   170
                            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   171
                            readStall.countDown();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   172
                            break;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   173
                        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   174
                        int r = read.remaining();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   175
                        total += r;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   176
                        clientRead.addAndGet(r);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   177
                    }
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   178
                    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
   179
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   180
            });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   181
            exit.await(); // All done, we need to compare results:
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   182
            assertEquals(clientRead.get(), serverWritten.get());
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   183
            assertEquals(serverRead.get(), clientWritten.get());
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
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   186
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   187
    private static RawChannel channelOf(int port) throws Exception {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   188
        URI uri = URI.create("http://127.0.0.1:" + port + "/");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   189
        print("raw channel to %s", uri.toString());
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   190
        HttpRequest req = HttpRequest.newBuilder(uri).build();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
        // Switch on isWebSocket flag to prevent the connection from
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
        // being returned to the pool.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
        ((WebSocketRequest)req).isWebSocket(true);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   194
        HttpClient client = HttpClient.newHttpClient();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   195
        try {
56082
1da51fab3032 http-client-branch: added mapping subscriber, miscellaneous bug fixes and change to discard()/replace() subscribers
michaelm
parents: 56079
diff changeset
   196
            HttpResponse<?> r = client.send(req, discard());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   197
            r.body();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   198
            return ((HttpResponseImpl) r).rawChannel();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
        } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   200
           // Need to hold onto the client until the RawChannel is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
           // created. This would not be needed if we had created
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   202
           // a WebSocket, but here we are fiddling directly
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   203
           // with the internals of HttpResponseImpl!
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   204
           java.lang.ref.Reference.reachabilityFence(client);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   205
        }
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   206
    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   207
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   208
    private class TestServer extends Thread { // Powered by Slowpokes
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   209
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   210
        private final ServerSocket server;
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
        TestServer(ServerSocket server) throws IOException {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   213
            this.server = server;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   214
        }
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
        @Override
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   217
        public void run() {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   218
            try (Socket s = server.accept()) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   219
                InputStream is = s.getInputStream();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   220
                OutputStream os = s.getOutputStream();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   221
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   222
                processHttp(is, os);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   223
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   224
                Thread reader = new Thread(() -> {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   225
                    try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   226
                        long n = readSlowly(is);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   227
                        print("Server read %s bytes", n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   228
                        serverRead.addAndGet(n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   229
                        s.shutdownInput();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   230
                    } catch (Exception e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   231
                        e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   232
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   233
                });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   234
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   235
                Thread writer = new Thread(() -> {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   236
                    try {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   237
                        long n = writeSlowly(os);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   238
                        print("Server written %s bytes", n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   239
                        serverWritten.addAndGet(n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   240
                        s.shutdownOutput();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   241
                    } catch (Exception e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   242
                        e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   243
                    }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   244
                });
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   245
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   246
                reader.start();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   247
                writer.start();
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
                reader.join();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   250
                writer.join();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   251
            } catch (Exception e) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   252
                e.printStackTrace();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   253
            } finally {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   254
                exit.countDown();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   255
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   256
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   257
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   258
        private void processHttp(InputStream is, OutputStream os)
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   259
                throws IOException
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
            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
   262
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   263
            // write some initial bytes
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   264
            byte[] initial = byteArrayOfSize(1024);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   265
            os.write(initial);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   266
            os.flush();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   267
            serverWritten.addAndGet(initial.length);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   268
            initialWriteStall.countDown();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   269
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   270
            byte[] buf = new byte[1024];
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   271
            String s = "";
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   272
            while (true) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   273
                int n = is.read(buf);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   274
                if (n <= 0) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   275
                    throw new RuntimeException("Unexpected end of request");
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   276
                }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   277
                s = s + new String(buf, 0, n);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   278
                if (s.contains("\r\n\r\n")) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   279
                    break;
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
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   282
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   283
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   284
        private long writeSlowly(OutputStream os) throws Exception {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   285
            byte[] first = byteArrayOfSize(1024);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   286
            long total = first.length;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   287
            os.write(first);
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   288
            os.flush();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   289
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   290
            // wait until initial bytes were read
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   291
            initialReadStall.await();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   292
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   293
            // 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
   294
            // will never be counted down.
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   295
            first = byteArrayOfSize(1024);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   296
            os.write(first);
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   297
            os.flush();
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   298
            total += first.length;
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   299
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   300
            // 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
   301
            // stalled, and then continue sending a bit more stuff
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   302
            readStall.await();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   303
            for (int i = 0; i < 32; i++) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   304
                byte[] b = byteArrayOfSize(1024);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   305
                os.write(b);
45338
f760c3a27d1c 8180279: java/net/httpclient/whitebox/Driver.java failed due to timeout
dfuchs
parents: 45119
diff changeset
   306
                os.flush();
45119
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   307
                total += b.length;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   308
                TimeUnit.MILLISECONDS.sleep(1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   309
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   310
            return total;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   311
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   312
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   313
        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
   314
            // 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
   315
            writeStall.await();
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   316
            long overall = 0;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   317
            byte[] array = new byte[1024];
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   318
            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
   319
                TimeUnit.MILLISECONDS.sleep(1);
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   320
                overall += n;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   321
            }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   322
            return overall;
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   323
        }
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   324
    }
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
    private static void print(String format, Object... args) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   327
        System.out.println(Thread.currentThread() + ": " + String.format(format, args));
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 byte[] byteArrayOfSize(int bound) {
decbbff9fdb4 8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
diff changeset
   331
        return new byte[new Random().nextInt(1 + bound)];
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
}