test/jdk/java/net/httpclient/BufferingSubscriberTest.java
author chegar
Mon, 20 Nov 2017 18:36:57 +0000
branchhttp-client-branch
changeset 55841 5f0b66e83dfa
parent 55828 ac0c821cc75c
child 55846 2a7e2724a422
permissions -rw-r--r--
http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     1
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     4
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     8
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    13
 * accompanied this code).
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    14
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    18
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    21
 * questions.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    22
 */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    23
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    24
import java.nio.ByteBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    25
import java.util.List;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    26
import java.util.Random;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    27
import java.util.concurrent.CompletableFuture;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    28
import java.util.concurrent.CompletionStage;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    29
import java.util.concurrent.Executor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    30
import java.util.concurrent.ExecutorService;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    31
import java.util.concurrent.Executors;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    32
import java.util.concurrent.Flow;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    33
import java.util.concurrent.Flow.Subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    34
import java.util.concurrent.SubmissionPublisher;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    35
import jdk.incubator.http.HttpResponse.BodyHandler;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    36
import jdk.incubator.http.HttpResponse.BodySubscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    37
import jdk.test.lib.RandomFactory;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    38
import org.testng.annotations.DataProvider;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    39
import org.testng.annotations.Test;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    40
import static java.lang.Long.MAX_VALUE;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    41
import static java.lang.System.out;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    42
import static java.util.concurrent.CompletableFuture.delayedExecutor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    43
import static java.util.concurrent.TimeUnit.MILLISECONDS;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    44
import static org.testng.Assert.*;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    45
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    46
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    47
 * @test
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    48
 * @bug 8184285
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    49
 * @summary Direct test for HttpResponse.BodySubscriber.buffering() API
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    50
 * @key randomness
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    51
 * @library /test/lib
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    52
 * @build jdk.test.lib.RandomFactory
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    53
 * @run testng/othervm -Djdk.internal.httpclient.debug=true BufferingSubscriberTest
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    54
 */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    55
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    56
public class BufferingSubscriberTest {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    57
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    58
    static final Random random = RandomFactory.getRandom();
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    59
    static final long start = System.nanoTime();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    60
    static final String START = "start";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    61
    static final String END   = "end  ";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    62
    static long elapsed() { return (System.nanoTime() - start)/1000_000;}
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    63
    static void printStamp(String what, String fmt, Object... args) {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    64
        long elapsed = elapsed();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    65
        long sec = elapsed/1000;
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    66
        long ms  = elapsed % 1000;
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    67
        String time = sec > 0 ? sec + "sec " : "";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    68
        time = time + ms + "ms";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    69
        out.println(what + "\t ["+time+"]\t "+ String.format(fmt,args));
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    70
    }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    71
    @DataProvider(name = "negatives")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    72
    public Object[][] negatives() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    73
        return new Object[][] {  { 0 }, { -1 }, { -1000 } };
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    74
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    75
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    76
    @Test(dataProvider = "negatives", expectedExceptions = IllegalArgumentException.class)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    77
    public void subscriberThrowsIAE(int bufferSize) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    78
        printStamp(START, "subscriberThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    79
        try {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    80
            BodySubscriber<?> bp = BodySubscriber.asByteArray();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    81
            BodySubscriber.buffering(bp, bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    82
        } finally {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    83
            printStamp(END, "subscriberThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    84
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    85
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    86
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
    @Test(dataProvider = "negatives", expectedExceptions = IllegalArgumentException.class)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    88
    public void handlerThrowsIAE(int bufferSize) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    89
        printStamp(START, "handlerThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    90
        try {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    91
            BodyHandler<?> bp = BodyHandler.asByteArray();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    92
            BodyHandler.buffering(bp, bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    93
        } finally {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    94
            printStamp(END, "handlerThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    95
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    96
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    97
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    98
    // ---
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    99
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   100
    @DataProvider(name = "config")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   101
    public Object[][] config() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   102
        return new Object[][] {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   103
            // iterations delayMillis numBuffers bufferSize maxBufferSize minBufferSize
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   104
            { 1,              0,          1,         1,         2,            1   },
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   105
            { 1,              0,          1,         10,        1000,         1   },
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   106
            { 1,              10,         1,         10,        1000,         1   },
55841
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   107
            { 1,              0,          1,         1000,      1000,         10  },
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   108
            { 1,              0,          10,        1000,      1000,         50  },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   109
            { 1,              0,          1000,      10 ,       1000,         50  },
55841
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   110
            { 1,              100,        1,         1000 * 4,  1000,         50  },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   111
            { 100,            0,          1000,      1,         2,            1   },
55841
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   112
            { 3,              0,          4,         5006,      1000,         50  },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   113
            { 20,             0,          100,       4888,      1000,         100 },
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   114
            { 16,             10,         1000,      50 ,       1000,         100 },
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   115
        };
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   116
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   117
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   118
    @Test(dataProvider = "config")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   119
    public void test(int iterations,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   120
                     int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   121
                     int numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   122
                     int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   123
                     int maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   124
                     int minbufferSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   125
        for (long perRequestAmount : new long[] { 1L, MAX_VALUE })
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   126
            test(iterations,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   127
                 delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   128
                 numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   129
                 bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   130
                 maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   131
                 minbufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   132
                 perRequestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   133
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   134
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   135
    public void test(int iterations,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   136
                     int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   137
                     int numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   138
                     int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   139
                     int maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   140
                     int minBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   141
                     long requestAmount) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   142
        ExecutorService executor = Executors.newFixedThreadPool(1);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   143
        try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   144
            out.printf("Iterations %d\n", iterations);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   145
            for (int i=0; i<iterations; i++ ) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   146
                printStamp(START, "Iteration %d", i);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   147
                try {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   148
                    SubmissionPublisher<List<ByteBuffer>> publisher =
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   149
                            new SubmissionPublisher<>(executor, 1);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   150
                    CompletableFuture<?> cf = sink(publisher,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   151
                            delayMillis,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   152
                            numBuffers * bufferSize,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   153
                            requestAmount,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   154
                            maxBufferSize,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   155
                            minBufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   156
                    source(publisher, numBuffers, bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   157
                    publisher.close();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   158
                    cf.join();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   159
                } finally {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   160
                    printStamp(END, "Iteration %d\n", i);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   161
                }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   162
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   163
            out.println("OK");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
        } finally {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
            executor.shutdown();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   166
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   167
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   168
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   169
    static int accumulatedDataSize(List<ByteBuffer> bufs) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   170
        return bufs.stream().mapToInt(ByteBuffer::remaining).sum();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   171
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   172
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   173
    /** Returns a new BB with its contents set to monotonically increasing
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   174
     * values, staring at the given start index and wrapping every 100. */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   175
    static ByteBuffer allocateBuffer(int size, int startIdx) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   176
        ByteBuffer b = ByteBuffer.allocate(size);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   177
        for (int i=0; i<size; i++)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   178
            b.put((byte)((startIdx + i) % 100));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   179
        b.position(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   180
        return b;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   181
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   182
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   183
    static class TestSubscriber implements BodySubscriber<Integer> {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   184
        final int delayMillis;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   185
        final int bufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   186
        final int expectedTotalSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   187
        final long requestAmount;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   188
        final CompletableFuture<Integer> completion;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   189
        final Executor delayedExecutor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   190
        volatile Flow.Subscription subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   191
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   192
        TestSubscriber(int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   193
                       int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
                       int expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   195
                       long requestAmount) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   196
            this.bufferSize = bufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   197
            this.completion = new CompletableFuture<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
            this.delayMillis = delayMillis;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
            this.delayedExecutor = delayedExecutor(delayMillis, MILLISECONDS);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
            this.expectedTotalSize = expectedTotalSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
            this.requestAmount = requestAmount;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
        /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
         * Example of a factory method which would decorate a buffering
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
         * subscriber to create a new subscriber dependent on buffering capability.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   207
         *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   208
         * The integer type parameter simulates the body just by counting the
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   209
         * number of bytes in the body.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   210
         */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
        static BodySubscriber<Integer> createSubscriber(int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
                                                        int delay,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
                                                        int expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   214
                                                        long requestAmount) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   215
            TestSubscriber s = new TestSubscriber(bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   216
                                                delay,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   217
                                                expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
                                                requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
            return BodySubscriber.buffering(s, bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   220
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   221
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   222
        private void requestMore() { subscription.request(requestAmount); }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   223
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
        public void onSubscribe(Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   226
            assertNull(this.subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   227
            this.subscription = subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   228
            if (delayMillis > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   229
                delayedExecutor.execute(this::requestMore);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   230
            else
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   231
                requestMore();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   232
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   233
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   234
        volatile int wrongSizes;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   235
        volatile int totalBytesReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   236
        volatile int onNextInvocations;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   237
        volatile int lastSeenSize = -1;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
        volatile boolean noMoreOnNext; // false
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
        volatile int index; // 0
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   241
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   242
        public void onNext(List<ByteBuffer> items) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   243
            long sz = accumulatedDataSize(items);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   244
            onNextInvocations++;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   245
            assertNotEquals(sz, 0L, "Unexpected empty buffers");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   246
            items.stream().forEach(b -> assertEquals(b.position(), 0));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   247
            assertFalse(noMoreOnNext);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   248
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   249
            if (sz != bufferSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   250
                String msg = sz + ", should be less than bufferSize, " + bufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   251
                assertTrue(sz < bufferSize, msg);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   252
                assertTrue(lastSeenSize == -1 || lastSeenSize == bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
                noMoreOnNext = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
                wrongSizes++;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
                assertEquals(sz, bufferSize, "Expected to receive exactly bufferSize");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   258
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   259
            // Ensure expected contents
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   260
            for (ByteBuffer b : items) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   261
                while (b.hasRemaining()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   262
                    assertEquals(b.get(), (byte) (index % 100));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   263
                    index++;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   264
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   265
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   266
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   267
            totalBytesReceived += sz;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   268
            assertEquals(totalBytesReceived, index );
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   269
            if (delayMillis > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   270
                delayedExecutor.execute(this::requestMore);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   271
            else
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   272
                requestMore();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   273
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   274
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   275
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   276
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   277
            completion.completeExceptionally(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   278
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   279
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   280
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   281
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   282
            if (wrongSizes > 1) { // allow just the final item to be smaller
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   283
                String msg = "Wrong sizes. Expected no more than 1. [" + this + "]";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   284
                completion.completeExceptionally(new Throwable(msg));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   285
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   286
            if (totalBytesReceived != expectedTotalSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   287
                String msg = "Wrong number of bytes. [" + this + "]";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   288
                completion.completeExceptionally(new Throwable(msg));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   289
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   290
                completion.complete(totalBytesReceived);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   291
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   292
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   293
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   294
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   295
        public CompletionStage<Integer> getBody() { return completion; }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   296
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   297
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   298
        public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   299
            StringBuilder sb = new StringBuilder();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   300
            sb.append(super.toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   301
            sb.append(", bufferSize=").append(bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   302
            sb.append(", onNextInvocations=").append(onNextInvocations);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   303
            sb.append(", totalBytesReceived=").append(totalBytesReceived);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   304
            sb.append(", expectedTotalSize=").append(expectedTotalSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   305
            sb.append(", requestAmount=").append(requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   306
            sb.append(", lastSeenSize=").append(lastSeenSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   307
            sb.append(", wrongSizes=").append(wrongSizes);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   308
            sb.append(", index=").append(index);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   309
            return sb.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   310
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   311
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   312
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   313
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   314
     * Publishes data, through the given publisher, using the main thread.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   315
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   316
     * Note: The executor supplied when creating the SubmissionPublisher provides
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   317
     * the threads for executing the Subscribers.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   318
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   319
     * @param publisher the publisher
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   320
     * @param numBuffers the number of buffers to send ( before splitting in two )
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   321
     * @param bufferSize the total size of the data to send ( before splitting in two )
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   322
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   323
    static void source(SubmissionPublisher<List<ByteBuffer>> publisher,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   324
                       int numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   325
                       int bufferSize) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   326
        printStamp("source","Publishing %d buffers of size %d each", numBuffers, bufferSize);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   327
        int index = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   328
        for (int i=0; i<numBuffers; i++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   329
            int chunkSize = random.nextInt(bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   330
            ByteBuffer buf1 = allocateBuffer(chunkSize, index);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   331
            index += chunkSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   332
            ByteBuffer buf2 = allocateBuffer(bufferSize - chunkSize, index);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   333
            index += bufferSize - chunkSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   334
            publisher.submit(List.of(buf1, buf2));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   335
        }
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   336
        printStamp("source", "complete");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   337
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   338
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   339
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   340
     * Creates and subscribes Subscribers that receive data from the given
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   341
     * publisher.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   342
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   343
     * @param publisher the publisher
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   344
     * @param delayMillis time, in milliseconds, to delay the Subscription
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   345
     *                    requesting more bytes ( for simulating slow consumption )
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   346
     * @param expectedTotalSize the total number of bytes expected to be received
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   347
     *                          by the subscribers
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   348
     * @return a CompletableFuture which completes when the subscription is complete
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   349
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   350
    static CompletableFuture<?> sink(SubmissionPublisher<List<ByteBuffer>> publisher,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   351
                                     int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   352
                                     int expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   353
                                     long requestAmount,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   354
                                     int maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   355
                                     int minBufferSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   356
        int bufferSize = random.nextInt(maxBufferSize - minBufferSize) + minBufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
        BodySubscriber<Integer> sub = TestSubscriber.createSubscriber(bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
                                                                    delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   359
                                                                    expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   360
                                                                    requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   361
        publisher.subscribe(sub);
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   362
        printStamp("sink","Subscriber reads data with buffer size: %d", bufferSize);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   363
        out.printf("Subscription delay is %d msec\n", delayMillis);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
        out.printf("Request amount is %d items\n", requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   365
        return sub.getBody().toCompletableFuture();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   368
    // ---
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   369
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
    // TODO: Add a test for cancel
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   371
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   372
    // ---
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   373
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   374
    /* Main entry point for standalone testing of the main functional test. */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   375
    public static void main(String... args) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   376
        BufferingSubscriberTest t = new BufferingSubscriberTest();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   377
        for (Object[] objs : t.config())
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   378
            t.test((int)objs[0], (int)objs[1], (int)objs[2], (int)objs[3], (int)objs[4], (int)objs[5]);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   379
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   380
}