test/jdk/java/net/httpclient/BufferingSubscriberTest.java
author chegar
Wed, 06 Dec 2017 18:40:15 +0000
branchhttp-client-branch
changeset 55971 3ae165556d48
parent 55965 2911b5fbc229
child 55973 4d9b002587db
permissions -rw-r--r--
http-client-branch: BufferingSubscriberTest needs to be lock-step with the Publisher
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;
55960
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
    35
import java.util.function.BiConsumer;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    36
import jdk.incubator.http.HttpResponse.BodyHandler;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    37
import jdk.incubator.http.HttpResponse.BodySubscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    38
import jdk.test.lib.RandomFactory;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    39
import org.testng.annotations.DataProvider;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    40
import org.testng.annotations.Test;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    41
import static java.lang.Long.MAX_VALUE;
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    42
import static java.lang.Long.min;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    43
import static java.lang.System.out;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    44
import static java.util.concurrent.CompletableFuture.delayedExecutor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    45
import static java.util.concurrent.TimeUnit.MILLISECONDS;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    46
import static org.testng.Assert.*;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    47
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    48
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    49
 * @test
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    50
 * @bug 8184285
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    51
 * @summary Direct test for HttpResponse.BodySubscriber.buffering() API
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    52
 * @key randomness
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    53
 * @library /test/lib
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    54
 * @build jdk.test.lib.RandomFactory
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    55
 * @run testng/othervm -Djdk.internal.httpclient.debug=true BufferingSubscriberTest
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    56
 */
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
public class BufferingSubscriberTest {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    59
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    60
    // If we compute that a test will take less that 10s
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    61
    // we judge it acceptable
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    62
    static final long LOWER_THRESHOLD = 10_000; // 10 sec.
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    63
    // If we compute that a test will take more than 20 sec
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    64
    // we judge it problematic: we will try to adjust the
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    65
    // buffer sizes, and if we can't we will print a warning
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    66
    static final long UPPER_THRESHOLD = 20_000; // 20 sec.
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
    67
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    68
    static final Random random = RandomFactory.getRandom();
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    69
    static final long start = System.nanoTime();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    70
    static final String START = "start";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    71
    static final String END   = "end  ";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    72
    static long elapsed() { return (System.nanoTime() - start)/1000_000;}
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    73
    static void printStamp(String what, String fmt, Object... args) {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    74
        long elapsed = elapsed();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    75
        long sec = elapsed/1000;
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    76
        long ms  = elapsed % 1000;
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    77
        String time = sec > 0 ? sec + "sec " : "";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    78
        time = time + ms + "ms";
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    79
        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
    80
    }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    81
    @DataProvider(name = "negatives")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    82
    public Object[][] negatives() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    83
        return new Object[][] {  { 0 }, { -1 }, { -1000 } };
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    84
    }
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
    @Test(dataProvider = "negatives", expectedExceptions = IllegalArgumentException.class)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
    public void subscriberThrowsIAE(int bufferSize) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    88
        printStamp(START, "subscriberThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    89
        try {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    90
            BodySubscriber<?> bp = BodySubscriber.asByteArray();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    91
            BodySubscriber.buffering(bp, bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    92
        } finally {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    93
            printStamp(END, "subscriberThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    94
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    95
    }
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
    @Test(dataProvider = "negatives", expectedExceptions = IllegalArgumentException.class)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    98
    public void handlerThrowsIAE(int bufferSize) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
    99
        printStamp(START, "handlerThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   100
        try {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   101
            BodyHandler<?> bp = BodyHandler.asByteArray();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   102
            BodyHandler.buffering(bp, bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   103
        } finally {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   104
            printStamp(END, "handlerThrowsIAE(%d)", bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   105
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   106
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   107
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   108
    // ---
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   109
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   110
    @DataProvider(name = "config")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   111
    public Object[][] config() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   112
        return new Object[][] {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   113
            // iterations delayMillis numBuffers bufferSize maxBufferSize minBufferSize
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   114
            { 1,              0,          1,         1,         2,            1   },
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   115
            { 1,              5,          1,         100,       2,            1   },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   116
            { 1,              0,          1,         10,        1000,         1   },
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   117
            { 1,              10,         1,         10,        1000,         1   },
55841
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   118
            { 1,              0,          1,         1000,      1000,         10  },
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   119
            { 1,              0,          10,        1000,      1000,         50  },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   120
            { 1,              0,          1000,      10 ,       1000,         50  },
55841
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   121
            { 1,              100,        1,         1000 * 4,  1000,         50  },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   122
            { 100,            0,          1000,      1,         2,            1   },
55841
5f0b66e83dfa http-client-branch: remove some degenerate test cases from BufferingSubscriberTest
chegar
parents: 55828
diff changeset
   123
            { 3,              0,          4,         5006,      1000,         50  },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   124
            { 20,             0,          100,       4888,      1000,         100 },
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   125
            { 16,             10,         1000,      50 ,       1000,         100 },
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   126
            { 16,             10,         1000,      50 ,       657,          657 },
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   127
        };
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   128
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   129
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   130
    @Test(dataProvider = "config")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   131
    public void test(int iterations,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   132
                     int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   133
                     int numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   134
                     int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   135
                     int maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   136
                     int minbufferSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   137
        for (long perRequestAmount : new long[] { 1L, MAX_VALUE })
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   138
            test(iterations,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   139
                 delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   140
                 numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   141
                 bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   142
                 maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   143
                 minbufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   144
                 perRequestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   145
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   146
55960
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   147
    volatile boolean onNextThrew;
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   148
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   149
    BiConsumer<Flow.Subscriber<?>, ? super Throwable> onNextThrowHandler =
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   150
            (sub, ex) -> {
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   151
                onNextThrew = true;
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   152
                System.out.println("onNext threw " + ex);
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   153
                ex.printStackTrace();
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   154
    };
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   155
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   156
    public void test(int iterations,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   157
                     int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   158
                     int numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   159
                     int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   160
                     int maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   161
                     int minBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   162
                     long requestAmount) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   163
        ExecutorService executor = Executors.newFixedThreadPool(1);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
        try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
            out.printf("Iterations %d\n", iterations);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   166
            for (int i=0; i<iterations; i++ ) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   167
                printStamp(START, "Iteration %d", i);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   168
                try {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   169
                    SubmissionPublisher<List<ByteBuffer>> publisher =
55960
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   170
                            new SubmissionPublisher<>(executor,
55971
3ae165556d48 http-client-branch: BufferingSubscriberTest needs to be lock-step with the Publisher
chegar
parents: 55965
diff changeset
   171
                                                      1, // lock-step with the publisher, for now
55960
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   172
                                                      onNextThrowHandler);
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   173
                    CompletableFuture<?> cf = sink(publisher,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   174
                            delayMillis,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   175
                            numBuffers * bufferSize,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   176
                            requestAmount,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   177
                            maxBufferSize,
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   178
                            minBufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   179
                    source(publisher, numBuffers, bufferSize);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   180
                    publisher.close();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   181
                    cf.join();
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   182
                } finally {
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   183
                    printStamp(END, "Iteration %d\n", i);
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   184
                }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   185
            }
55960
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   186
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   187
            assertFalse(onNextThrew, "Unexpected onNextThrew, check output");
7553e90ba43c http-client-branch: addditional BufferingSubscriberTest checks
chegar
parents: 55857
diff changeset
   188
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   189
            out.println("OK");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   190
        } finally {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   191
            executor.shutdown();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   192
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   193
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   195
    static long accumulatedDataSize(List<ByteBuffer> bufs) {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   196
        return bufs.stream().mapToLong(ByteBuffer::remaining).sum();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   197
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
    /** Returns a new BB with its contents set to monotonically increasing
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
     * values, staring at the given start index and wrapping every 100. */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
    static ByteBuffer allocateBuffer(int size, int startIdx) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
        ByteBuffer b = ByteBuffer.allocate(size);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
        for (int i=0; i<size; i++)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
            b.put((byte)((startIdx + i) % 100));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
        b.position(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
        return b;
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
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   209
    static class TestSubscriber implements BodySubscriber<Integer> {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   210
        final int delayMillis;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
        final int bufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
        final int expectedTotalSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
        final long requestAmount;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   214
        final CompletableFuture<Integer> completion;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   215
        final Executor delayedExecutor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   216
        volatile Flow.Subscription subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   217
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
        TestSubscriber(int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
                       int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   220
                       int expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   221
                       long requestAmount) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   222
            this.bufferSize = bufferSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   223
            this.completion = new CompletableFuture<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
            this.delayMillis = delayMillis;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
            this.delayedExecutor = delayedExecutor(delayMillis, MILLISECONDS);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   226
            this.expectedTotalSize = expectedTotalSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   227
            this.requestAmount = requestAmount;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   228
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   229
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   230
        /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   231
         * Example of a factory method which would decorate a buffering
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   232
         * subscriber to create a new subscriber dependent on buffering capability.
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   233
         * <p>
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   234
         * The integer type parameter simulates the body just by counting the
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   235
         * number of bytes in the body.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   236
         */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   237
        static BodySubscriber<Integer> createSubscriber(int bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
                                                        int delay,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
                                                        int expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
                                                        long requestAmount) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   241
            TestSubscriber s = new TestSubscriber(bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   242
                                                delay,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   243
                                                expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   244
                                                requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   245
            return BodySubscriber.buffering(s, bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   246
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   247
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   248
        private void requestMore() {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   249
            subscription.request(requestAmount);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   250
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   251
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   252
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
        public void onSubscribe(Subscription subscription) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
            assertNull(this.subscription);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
            this.subscription = subscription;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
            if (delayMillis > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
                delayedExecutor.execute(this::requestMore);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   258
            else
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   259
                requestMore();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   260
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   261
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   262
        volatile int wrongSizes;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   263
        volatile int totalBytesReceived;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   264
        volatile int onNextInvocations;
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   265
        volatile long lastSeenSize = -1;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   266
        volatile boolean noMoreOnNext; // false
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   267
        volatile int index; // 0
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   268
        volatile long count;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   269
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   270
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   271
        public void onNext(List<ByteBuffer> items) {
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   272
            try {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   273
                long sz = accumulatedDataSize(items);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   274
                boolean printStamp = delayMillis > 0
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   275
                        && requestAmount < Long.MAX_VALUE
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   276
                        && count % 20 == 0;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   277
                if (printStamp) {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   278
                    printStamp("stamp", "count=%d sz=%d accumulated=%d",
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   279
                            count, sz, (totalBytesReceived + sz));
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   280
                }
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   281
                count++;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   282
                onNextInvocations++;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   283
                assertNotEquals(sz, 0L, "Unexpected empty buffers");
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   284
                items.stream().forEach(b -> assertEquals(b.position(), 0));
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   285
                assertFalse(noMoreOnNext);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   286
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   287
                if (sz != bufferSize) {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   288
                    String msg = sz + ", should be less than bufferSize, " + bufferSize;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   289
                    assertTrue(sz < bufferSize, msg);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   290
                    assertTrue(lastSeenSize == -1 || lastSeenSize == bufferSize);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   291
                    noMoreOnNext = true;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   292
                    wrongSizes++;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   293
                    printStamp("onNext",
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   294
                            "Possibly received last buffer: sz=%d, accumulated=%d, total=%d",
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   295
                            sz, totalBytesReceived, totalBytesReceived + sz);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   296
                } else {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   297
                    assertEquals(sz, bufferSize, "Expected to receive exactly bufferSize");
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   298
                }
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   299
                lastSeenSize = sz;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   300
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   301
                // Ensure expected contents
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   302
                for (ByteBuffer b : items) {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   303
                    while (b.hasRemaining()) {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   304
                        assertEquals(b.get(), (byte) (index % 100));
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   305
                        index++;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   306
                    }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   307
                }
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   308
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   309
                totalBytesReceived += sz;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   310
                assertEquals(totalBytesReceived, index);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   311
                if (delayMillis > 0 && ((expectedTotalSize - totalBytesReceived) > bufferSize))
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   312
                    delayedExecutor.execute(this::requestMore);
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   313
                else
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   314
                    requestMore();
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   315
            } catch (Throwable t) {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   316
                completion.completeExceptionally(t);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   317
            }
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
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   320
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   321
        public void onError(Throwable throwable) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   322
            completion.completeExceptionally(throwable);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   323
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   324
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   325
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   326
        public void onComplete() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   327
            if (wrongSizes > 1) { // allow just the final item to be smaller
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   328
                String msg = "Wrong sizes. Expected no more than 1. [" + this + "]";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   329
                completion.completeExceptionally(new Throwable(msg));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   330
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   331
            if (totalBytesReceived != expectedTotalSize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   332
                String msg = "Wrong number of bytes. [" + this + "]";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   333
                completion.completeExceptionally(new Throwable(msg));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   334
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   335
                completion.complete(totalBytesReceived);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   336
            }
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
        @Override
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   340
        public CompletionStage<Integer> getBody() {
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   341
            return completion;
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   342
        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   343
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   344
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   345
        public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   346
            StringBuilder sb = new StringBuilder();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   347
            sb.append(super.toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   348
            sb.append(", bufferSize=").append(bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   349
            sb.append(", onNextInvocations=").append(onNextInvocations);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   350
            sb.append(", totalBytesReceived=").append(totalBytesReceived);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   351
            sb.append(", expectedTotalSize=").append(expectedTotalSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   352
            sb.append(", requestAmount=").append(requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   353
            sb.append(", lastSeenSize=").append(lastSeenSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   354
            sb.append(", wrongSizes=").append(wrongSizes);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   355
            sb.append(", index=").append(index);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   356
            return sb.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   359
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   360
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   361
     * Publishes data, through the given publisher, using the main thread.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   362
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   363
     * Note: The executor supplied when creating the SubmissionPublisher provides
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
     * the threads for executing the Subscribers.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   365
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
     * @param publisher the publisher
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
     * @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
   368
     * @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
   369
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
    static void source(SubmissionPublisher<List<ByteBuffer>> publisher,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   371
                       int numBuffers,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   372
                       int bufferSize) {
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   373
        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
   374
        int index = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   375
        for (int i=0; i<numBuffers; i++) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   376
            int chunkSize = random.nextInt(bufferSize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   377
            ByteBuffer buf1 = allocateBuffer(chunkSize, index);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   378
            index += chunkSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   379
            ByteBuffer buf2 = allocateBuffer(bufferSize - chunkSize, index);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   380
            index += bufferSize - chunkSize;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   381
            publisher.submit(List.of(buf1, buf2));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   382
        }
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   383
        printStamp("source", "complete");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   384
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   385
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   386
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   387
     * Creates and subscribes Subscribers that receive data from the given
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   388
     * publisher.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   389
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   390
     * @param publisher the publisher
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   391
     * @param delayMillis time, in milliseconds, to delay the Subscription
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   392
     *                    requesting more bytes ( for simulating slow consumption )
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   393
     * @param expectedTotalSize the total number of bytes expected to be received
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   394
     *                          by the subscribers
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   395
     * @return a CompletableFuture which completes when the subscription is complete
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   396
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   397
    static CompletableFuture<?> sink(SubmissionPublisher<List<ByteBuffer>> publisher,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   398
                                     int delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   399
                                     int expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   400
                                     long requestAmount,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   401
                                     int maxBufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   402
                                     int minBufferSize) {
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   403
        int bufferSize = chooseBufferSize(maxBufferSize,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   404
                                          minBufferSize,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   405
                                          delayMillis,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   406
                                          expectedTotalSize,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   407
                                          requestAmount);
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   408
        assert bufferSize > 0;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   409
        assert bufferSize >= minBufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   410
        assert bufferSize <= maxBufferSize;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   411
        BodySubscriber<Integer> sub = TestSubscriber.createSubscriber(bufferSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   412
                                                                    delayMillis,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   413
                                                                    expectedTotalSize,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   414
                                                                    requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   415
        publisher.subscribe(sub);
55828
ac0c821cc75c http-client-branch: adding time information to BufferingSubscriberTest.java
dfuchs
parents: 55763
diff changeset
   416
        printStamp("sink","Subscriber reads data with buffer size: %d", bufferSize);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   417
        out.printf("Subscription delay is %d msec\n", delayMillis);
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   418
        long delay = (((long)delayMillis * expectedTotalSize) / bufferSize) / requestAmount;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   419
        out.printf("Minimum total delay is %d sec %d ms\n", delay / 1000, delay % 1000);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   420
        out.printf("Request amount is %d items\n", requestAmount);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   421
        return sub.getBody().toCompletableFuture();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   422
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   423
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   424
    static int chooseBufferSize(int maxBufferSize,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   425
                                int minBufferSize,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   426
                                int delaysMillis,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   427
                                int expectedTotalSize,
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   428
                                long requestAmount) {
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   429
        assert minBufferSize > 0 && maxBufferSize > 0 && requestAmount > 0;
55965
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   430
        int bufferSize = maxBufferSize == minBufferSize ? maxBufferSize :
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   431
                (random.nextInt(maxBufferSize - minBufferSize)
2911b5fbc229 http-client-branch: work around issue with SP::close in BufferingSubscriberTest
dfuchs
parents: 55960
diff changeset
   432
                    + minBufferSize);
55846
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   433
        if (requestAmount == Long.MAX_VALUE) return bufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   434
        long minDelay = (((long)delaysMillis * expectedTotalSize) / maxBufferSize)
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   435
                / requestAmount;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   436
        long maxDelay = (((long)delaysMillis * expectedTotalSize) / minBufferSize)
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   437
                / requestAmount;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   438
        // if the maximum delay is < 10s just take a random number between min and max.
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   439
        if (maxDelay <= LOWER_THRESHOLD) {
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   440
            return bufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   441
        }
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   442
        // if minimum delay is greater than 20s then print a warning and use max buffer.
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   443
        if (minDelay >= UPPER_THRESHOLD) {
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   444
            System.out.println("Warning: minimum delay is "
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   445
                    + minDelay/1000 + "sec " + minDelay%1000 + "ms");
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   446
            System.err.println("Warning: minimum delay is "
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   447
                    + minDelay/1000 + "sec " + minDelay%1000 + "ms");
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   448
            return maxBufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   449
        }
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   450
        // maxDelay could be anything, but minDelay is below the UPPER_THRESHOLD
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   451
        // try to pick up a buffer size that keeps the delay below the
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   452
        // UPPER_THRESHOLD
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   453
        while (minBufferSize < maxBufferSize) {
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   454
            bufferSize = random.nextInt(maxBufferSize - minBufferSize)
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   455
                    + minBufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   456
            long delay = (((long)delaysMillis * expectedTotalSize) / bufferSize)
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   457
                    / requestAmount;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   458
            if (delay < UPPER_THRESHOLD) return bufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   459
            minBufferSize++;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   460
        }
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   461
        return minBufferSize;
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   462
    }
2a7e2724a422 http-client-branch: avoid picking buffer size resulting in too long delays for BufferingSubscriberTest.java
dfuchs
parents: 55841
diff changeset
   463
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   464
    // ---
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   465
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   466
    /* Main entry point for standalone testing of the main functional test. */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   467
    public static void main(String... args) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   468
        BufferingSubscriberTest t = new BufferingSubscriberTest();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   469
        for (Object[] objs : t.config())
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   470
            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
   471
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   472
}