test/jdk/java/net/httpclient/BufferingSubscriberErrorCompleteTest.java
author chegar
Thu, 23 Nov 2017 12:50:34 +0000
branchhttp-client-branch
changeset 55861 0683f22cf2b9
parent 55859 4ca3e578b9c4
child 55973 4d9b002587db
permissions -rw-r--r--
http-client-branch: further review comments on error handling in BufferedSubscriber
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     1
/*
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     4
 *
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     8
 *
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    13
 * accompanied this code).
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    14
 *
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    18
 *
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    21
 * questions.
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    22
 */
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    23
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    24
import java.nio.ByteBuffer;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    25
import java.util.ArrayList;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    26
import java.util.List;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    27
import java.util.concurrent.CompletionStage;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    28
import java.util.concurrent.CountDownLatch;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    29
import java.util.concurrent.CyclicBarrier;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    30
import java.util.concurrent.ExecutorService;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    31
import java.util.concurrent.Executors;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    32
import java.util.concurrent.Flow.Subscription;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    33
import java.util.concurrent.Phaser;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    34
import java.util.concurrent.SubmissionPublisher;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    35
import java.util.function.IntSupplier;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    36
import java.util.stream.IntStream;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    37
import jdk.incubator.http.HttpResponse.BodySubscriber;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    38
import org.testng.annotations.DataProvider;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    39
import org.testng.annotations.Test;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    40
import static java.lang.Long.MAX_VALUE;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    41
import static java.lang.Long.MIN_VALUE;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    42
import static java.lang.System.out;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    43
import static java.nio.ByteBuffer.wrap;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    44
import static java.util.concurrent.TimeUnit.SECONDS;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    45
import static jdk.incubator.http.HttpResponse.BodySubscriber.buffering;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    46
import static org.testng.Assert.*;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    47
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    48
/*
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    49
 * @test
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    50
 * @summary Test for HttpResponse.BodySubscriber.buffering() onError/onComplete
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    51
 * @run testng/othervm BufferingSubscriberErrorCompleteTest
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    52
 */
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    53
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    54
public class BufferingSubscriberErrorCompleteTest {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    55
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    56
    @DataProvider(name = "illegalDemand")
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    57
    public Object[][] illegalDemand() {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    58
        return new Object[][]{
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    59
            {0L}, {-1L}, {-5L}, {-100L}, {-101L}, {-100_001L}, {MIN_VALUE}
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    60
        };
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    61
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    62
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    63
    @Test(dataProvider = "illegalDemand")
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    64
    public void illegalRequest(long demand) throws Exception {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    65
        ExecutorService executor = Executors.newFixedThreadPool(1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    66
        SubmissionPublisher<List<ByteBuffer>> publisher =
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    67
                new SubmissionPublisher<>(executor, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    68
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    69
        Phaser gate = new Phaser(2);  // single onSubscribe and onError
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    70
        ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    71
        BodySubscriber subscriber = buffering(exposingSubscriber, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    72
        publisher.subscribe(subscriber);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    73
        gate.arriveAndAwaitAdvance();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    74
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    75
        Subscription s = exposingSubscriber.subscription;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    76
        int previous = exposingSubscriber.onErrorInvocations;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    77
        s.request(demand);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    78
        gate.arriveAndAwaitAdvance();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    79
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    80
        assertEquals(previous + 1, exposingSubscriber.onErrorInvocations);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    81
        assertTrue(exposingSubscriber.throwable instanceof IllegalArgumentException,
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    82
                "Expected IAE, got:" + exposingSubscriber.throwable);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    83
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    84
        furtherCancelsRequestsShouldBeNoOp(s);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    85
        assertEquals(exposingSubscriber.onErrorInvocations, 1);
55861
0683f22cf2b9 http-client-branch: further review comments on error handling in BufferedSubscriber
chegar
parents: 55859
diff changeset
    86
        executor.shutdown();
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    87
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    88
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    89
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    90
    @DataProvider(name = "bufferAndItemSizes")
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    91
    public Object[][] bufferAndItemSizes() {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    92
        List<Object[]> values = new ArrayList<>();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    93
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    94
        for (int bufferSize : new int[] { 1, 5, 10, 100, 1000 })
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    95
            for (int items : new int[]  { 0, 1, 2, 5, 9, 10, 11, 15, 29, 99 })
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    96
                values.add(new Object[] { bufferSize, items });
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    97
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    98
        return values.stream().toArray(Object[][]::new);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
    99
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   100
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   101
    @Test(dataProvider = "bufferAndItemSizes")
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   102
    public void onErrorFromPublisher(int bufferSize,
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   103
                                     int numberOfItems)
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   104
        throws Exception
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   105
    {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   106
        ExecutorService executor = Executors.newFixedThreadPool(1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   107
        SubmissionPublisher<List<ByteBuffer>> publisher =
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   108
                new SubmissionPublisher<>(executor, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   109
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   110
        // onSubscribe + onError + this thread
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   111
        Phaser gate = new Phaser(3);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   112
        ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   113
        BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   114
        publisher.subscribe(subscriber);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   115
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   116
        List<ByteBuffer> item = List.of(wrap(new byte[] { 1 }));
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   117
        IntStream.range(0, numberOfItems).forEach(x -> publisher.submit(item));
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   118
        Throwable t = new Throwable("a message from me to me");
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   119
        publisher.closeExceptionally(t);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   120
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   121
        gate.arriveAndAwaitAdvance();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   122
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   123
        Subscription s = exposingSubscriber.subscription;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   124
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   125
        assertEquals(exposingSubscriber.onErrorInvocations, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   126
        assertEquals(exposingSubscriber.onCompleteInvocations, 0);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   127
        assertEquals(exposingSubscriber.throwable, t);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   128
        assertEquals(exposingSubscriber.throwable.getMessage(),
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   129
                     "a message from me to me");
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   130
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   131
        furtherCancelsRequestsShouldBeNoOp(s);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   132
        assertEquals(exposingSubscriber.onErrorInvocations, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   133
        assertEquals(exposingSubscriber.onCompleteInvocations, 0);
55861
0683f22cf2b9 http-client-branch: further review comments on error handling in BufferedSubscriber
chegar
parents: 55859
diff changeset
   134
        executor.shutdown();
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   135
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   136
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   137
    @Test(dataProvider = "bufferAndItemSizes")
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   138
    public void onCompleteFromPublisher(int bufferSize,
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   139
                                        int numberOfItems)
55861
0683f22cf2b9 http-client-branch: further review comments on error handling in BufferedSubscriber
chegar
parents: 55859
diff changeset
   140
        throws Exception
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   141
    {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   142
        ExecutorService executor = Executors.newFixedThreadPool(1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   143
        SubmissionPublisher<List<ByteBuffer>> publisher =
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   144
                new SubmissionPublisher<>(executor, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   145
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   146
        // onSubscribe + onComplete + this thread
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   147
        Phaser gate = new Phaser(3);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   148
        ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   149
        BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   150
        publisher.subscribe(subscriber);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   151
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   152
        List<ByteBuffer> item = List.of(wrap(new byte[] { 1 }));
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   153
        IntStream.range(0, numberOfItems).forEach(x -> publisher.submit(item));
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   154
        publisher.close();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   155
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   156
        gate.arriveAndAwaitAdvance();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   157
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   158
        Subscription s = exposingSubscriber.subscription;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   159
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   160
        assertEquals(exposingSubscriber.onErrorInvocations, 0);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   161
        assertEquals(exposingSubscriber.onCompleteInvocations, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   162
        assertEquals(exposingSubscriber.throwable, null);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   163
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   164
        furtherCancelsRequestsShouldBeNoOp(s);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   165
        assertEquals(exposingSubscriber.onErrorInvocations, 0);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   166
        assertEquals(exposingSubscriber.onCompleteInvocations, 1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   167
        assertEquals(exposingSubscriber.throwable, null);
55861
0683f22cf2b9 http-client-branch: further review comments on error handling in BufferedSubscriber
chegar
parents: 55859
diff changeset
   168
        executor.shutdown();
55859
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   169
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   170
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   171
    static class ExposingSubscriber implements BodySubscriber<Void> {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   172
        final Phaser gate;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   173
        volatile Subscription subscription;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   174
        volatile int onNextInvocations;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   175
        volatile int onErrorInvocations;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   176
        volatile int onCompleteInvocations;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   177
        volatile Throwable throwable;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   178
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   179
        ExposingSubscriber(Phaser gate) {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   180
            this.gate = gate;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   181
        }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   182
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   183
        @Override
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   184
        public void onSubscribe(Subscription subscription) {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   185
            //out.println("onSubscribe " + subscription);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   186
            this.subscription = subscription;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   187
            subscription.request(MAX_VALUE);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   188
            gate.arrive();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   189
        }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   190
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   191
        @Override
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   192
        public void onNext(List<ByteBuffer> item) {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   193
            //out.println("onNext " + item);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   194
            onNextInvocations++;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   195
        }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   196
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   197
        @Override
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   198
        public void onError(Throwable throwable) {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   199
            //out.println("onError " + throwable);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   200
            this.throwable = throwable;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   201
            onErrorInvocations++;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   202
            gate.arrive();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   203
        }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   204
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   205
        @Override
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   206
        public void onComplete() {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   207
            //out.println("onComplete ");
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   208
            onCompleteInvocations++;
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   209
            gate.arrive();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   210
        }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   211
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   212
        @Override
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   213
        public CompletionStage<Void> getBody() {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   214
            throw new UnsupportedOperationException("getBody is unsupported");
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   215
        }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   216
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   217
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   218
    static void furtherCancelsRequestsShouldBeNoOp(Subscription s) {
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   219
        s.cancel(); s.request(1);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   220
        s.cancel(); s.request(100); s.cancel();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   221
        s.cancel(); s.request(MAX_VALUE); s.cancel(); s.cancel();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   222
        s.cancel(); s.cancel(); s.cancel(); s.cancel();
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   223
        s.request(MAX_VALUE); s.request(MAX_VALUE); s.request(MAX_VALUE);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   224
        s.request(-1); s.request(-100); s.request(MIN_VALUE);
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   225
    }
4ca3e578b9c4 http-client-branch: review comment on buffering subscriber, and new test to verify the change
chegar
parents:
diff changeset
   226
}