test/jdk/java/net/httpclient/BufferingSubscriberCancelTest.java
author chegar
Wed, 22 Nov 2017 21:29:20 +0000
branchhttp-client-branch
changeset 55859 4ca3e578b9c4
parent 55857 89c904d57ebe
child 55861 0683f22cf2b9
permissions -rw-r--r--
http-client-branch: review comment on buffering subscriber, and new test to verify the change
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55857
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     1
/*
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     4
 *
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     8
 *
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    13
 * accompanied this code).
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    14
 *
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    18
 *
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    21
 * questions.
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    22
 */
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    23
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    24
import java.nio.ByteBuffer;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    25
import java.util.List;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    26
import java.util.concurrent.CompletionStage;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    27
import java.util.concurrent.CountDownLatch;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    28
import java.util.concurrent.ExecutorService;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    29
import java.util.concurrent.Executors;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    30
import java.util.concurrent.Flow.Subscription;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    31
import java.util.concurrent.SubmissionPublisher;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    32
import java.util.function.IntSupplier;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    33
import java.util.stream.IntStream;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    34
import jdk.incubator.http.HttpResponse.BodySubscriber;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    35
import org.testng.annotations.DataProvider;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    36
import org.testng.annotations.Test;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    37
import static java.lang.Long.MAX_VALUE;
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents: 55857
diff changeset
    38
import static java.lang.Long.MIN_VALUE;
55857
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    39
import static java.lang.System.out;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    40
import static java.nio.ByteBuffer.wrap;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    41
import static java.util.concurrent.TimeUnit.SECONDS;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    42
import static jdk.incubator.http.HttpResponse.BodySubscriber.buffering;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    43
import static org.testng.Assert.*;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    44
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    45
/*
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    46
 * @test
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    47
 * @summary Direct test for HttpResponse.BodySubscriber.buffering() cancellation
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    48
 * @run testng/othervm BufferingSubscriberCancelTest
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    49
 */
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    50
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    51
public class BufferingSubscriberCancelTest {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    52
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    53
    @DataProvider(name = "bufferSizes")
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    54
    public Object[][] bufferSizes() {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    55
        return new Object[][]{
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    56
            // bufferSize should be irrelevant
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    57
            {1}, {100}, {511}, {512}, {513}, {1024}, {2047}, {2048}
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    58
        };
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    59
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    60
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    61
    @Test(dataProvider = "bufferSizes")
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    62
    public void cancelWithoutAnyItemsPublished(int bufferSize) throws Exception {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    63
        ExecutorService executor = Executors.newFixedThreadPool(1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    64
        SubmissionPublisher<List<ByteBuffer>> publisher =
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    65
                new SubmissionPublisher<>(executor, 1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    66
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    67
        CountDownLatch gate = new CountDownLatch(1);  // single onSubscribe
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    68
        ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    69
        BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    70
        publisher.subscribe(subscriber);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    71
        gate.await(30, SECONDS);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    72
        assertEqualsWithRetry(publisher::getNumberOfSubscribers, 1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    73
        exposingSubscriber.subscription.cancel();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    74
        assertEqualsWithRetry(publisher::getNumberOfSubscribers, 0);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    75
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    76
        // further cancels/requests should be a no-op
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    77
        Subscription s = exposingSubscriber.subscription;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    78
        s.cancel(); s.request(1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    79
        s.cancel(); s.request(100); s.cancel();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    80
        s.cancel(); s.request(MAX_VALUE); s.cancel(); s.cancel();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    81
        s.cancel(); s.cancel(); s.cancel(); s.cancel();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    82
        s.request(MAX_VALUE); s.request(MAX_VALUE); s.request(MAX_VALUE);
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents: 55857
diff changeset
    83
        s.request(-1); s.request(-100); s.request(MIN_VALUE);
55857
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    84
        assertEqualsWithRetry(publisher::getNumberOfSubscribers, 0);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    85
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    86
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    87
    @DataProvider(name = "sizeAndItems")
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    88
    public Object[][] sizeAndItems() {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    89
        return new Object[][] {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    90
            // bufferSize and item bytes must be equal to count onNext calls
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    91
            // bufferSize        items
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    92
            { 1,   List.of(wrap(new byte[] { 1 }))                             },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    93
            { 2,   List.of(wrap(new byte[] { 1, 2 }))                          },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    94
            { 3,   List.of(wrap(new byte[] { 1, 2, 3}))                        },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    95
            { 4,   List.of(wrap(new byte[] { 1, 2 , 3, 4}))                    },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    96
            { 5,   List.of(wrap(new byte[] { 1, 2 , 3, 4, 5}))                 },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    97
            { 6,   List.of(wrap(new byte[] { 1, 2 , 3, 4, 5, 6}))              },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    98
            { 7,   List.of(wrap(new byte[] { 1, 2 , 3, 4, 5, 6, 7}))           },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
    99
            { 8,   List.of(wrap(new byte[] { 1, 2 , 3, 4, 5, 6, 7, 8}))        },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   100
            { 9,   List.of(wrap(new byte[] { 1, 2 , 3, 4, 5, 6, 7, 8, 9}))     },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   101
            { 10,  List.of(wrap(new byte[] { 1, 2 , 3, 4, 5, 6, 7, 8, 9, 10})) },
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   102
        };
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   103
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   104
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   105
    @Test(dataProvider = "sizeAndItems")
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   106
    public void cancelWithItemsPublished(int bufferSize, List<ByteBuffer> items)
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   107
        throws Exception
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   108
    {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   109
        ExecutorService executor = Executors.newFixedThreadPool(1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   110
        SubmissionPublisher<List<ByteBuffer>> publisher =
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   111
                new SubmissionPublisher<>(executor, 24);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   112
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   113
        final int ITERATION_TIMES = 10;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   114
        // onSubscribe + onNext ITERATION_TIMES
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   115
        CountDownLatch gate = new CountDownLatch(1 + ITERATION_TIMES);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   116
        ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   117
        BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   118
        publisher.subscribe(subscriber);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   119
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   120
        assertEqualsWithRetry(publisher::getNumberOfSubscribers, 1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   121
        IntStream.range(0, ITERATION_TIMES).forEach(x -> publisher.submit(items));
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   122
        gate.await(30, SECONDS);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   123
        exposingSubscriber.subscription.cancel();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   124
        IntStream.range(0, ITERATION_TIMES+1).forEach(x -> publisher.submit(items));
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   125
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   126
        assertEqualsWithRetry(publisher::getNumberOfSubscribers, 0);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   127
        assertEquals(exposingSubscriber.onNextInvocations, ITERATION_TIMES);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   128
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   129
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   130
    // same as above but with more racy conditions, do not wait on the gate
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   131
    @Test(dataProvider = "sizeAndItems")
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   132
    public void cancelWithItemsPublishedNoWait(int bufferSize, List<ByteBuffer> items)
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   133
        throws Exception
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   134
    {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   135
        ExecutorService executor = Executors.newFixedThreadPool(1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   136
        SubmissionPublisher<List<ByteBuffer>> publisher =
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   137
                new SubmissionPublisher<>(executor, 24);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   138
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   139
        final int ITERATION_TIMES = 10;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   140
        // any callback will so, since onSub is guaranteed to be before onNext
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   141
        CountDownLatch gate = new CountDownLatch(1);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   142
        ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   143
        BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   144
        publisher.subscribe(subscriber);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   145
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   146
        IntStream.range(0, ITERATION_TIMES).forEach(x -> publisher.submit(items));
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   147
        gate.await(30, SECONDS);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   148
        exposingSubscriber.subscription.cancel();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   149
        IntStream.range(0, ITERATION_TIMES+1).forEach(x -> publisher.submit(items));
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   150
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   151
        int onNextInvocations = exposingSubscriber.onNextInvocations;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   152
        assertTrue(onNextInvocations <= ITERATION_TIMES,
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   153
                   "Expected <= " + ITERATION_TIMES + ", got " + onNextInvocations);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   154
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   155
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   156
    static class ExposingSubscriber implements BodySubscriber<Void> {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   157
        final CountDownLatch gate;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   158
        volatile Subscription subscription;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   159
        volatile int onNextInvocations;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   160
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   161
        ExposingSubscriber(CountDownLatch gate) {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   162
            this.gate = gate;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   163
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   164
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   165
        @Override
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   166
        public void onSubscribe(Subscription subscription) {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   167
            //out.println("onSubscribe " + subscription);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   168
            this.subscription = subscription;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   169
            gate.countDown();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   170
            subscription.request(MAX_VALUE); // forever
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   171
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   172
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   173
        @Override
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   174
        public void onNext(List<ByteBuffer> item) {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   175
            //out.println("onNext " + item);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   176
            onNextInvocations++;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   177
            gate.countDown();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   178
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   179
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   180
        @Override
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   181
        public void onError(Throwable throwable) {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   182
            out.println("onError " + throwable);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   183
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   184
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   185
        @Override
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   186
        public void onComplete() {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   187
            out.println("onComplete ");
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   188
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   189
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   190
        @Override
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   191
        public CompletionStage<Void> getBody() {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   192
            throw new UnsupportedOperationException("getBody is unsupported");
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   193
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   194
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   195
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   196
    // There is a race between cancellation and subscriber callbacks, the
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   197
    // following mechanism retries a number of times to allow for this race. The
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   198
    // only requirement is that the expected result is actually observed.
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   199
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   200
    static final int TEST_RECHECK_TIMES = 30;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   201
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   202
    static void assertEqualsWithRetry(IntSupplier actualSupplier, int expected)
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   203
        throws Exception
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   204
    {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   205
        int actual = expected + 1; // anything other than expected
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   206
        for (int i=0; i< TEST_RECHECK_TIMES; i++) {
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   207
            actual = actualSupplier.getAsInt();
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   208
            if (actual == expected)
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   209
                return;
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   210
            Thread.sleep(100);
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   211
        }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   212
        assertEquals(actual, expected); // will fail with the usual testng message
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   213
    }
89c904d57ebe http-client-branch: review comments add buffering cancallation test
chegar
parents:
diff changeset
   214
}