|
| 1 | +/* |
| 2 | + * Copyright 2025 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package com.linecorp.armeria.common.stream; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.awaitility.Awaitility.await; |
| 20 | + |
| 21 | +import java.util.concurrent.atomic.AtomicLong; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.reactivestreams.Subscriber; |
| 25 | +import org.reactivestreams.Subscription; |
| 26 | + |
| 27 | +final class DuplicatorCumulativeDemandTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + void cumulativeDemand() throws InterruptedException { |
| 31 | + final AtomicLong demand = new AtomicLong(); |
| 32 | + final PublisherBasedStreamMessage<Integer> streamMessage = new PublisherBasedStreamMessage<>( |
| 33 | + s -> s.onSubscribe(new Subscription() { |
| 34 | + @Override |
| 35 | + public void request(long n) { |
| 36 | + demand.addAndGet(n); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void cancel() {} |
| 41 | + })); |
| 42 | + streamMessage.toDuplicator().duplicate().subscribe(new Subscriber<Integer>() { |
| 43 | + @Override |
| 44 | + public void onSubscribe(Subscription s) { |
| 45 | + s.request(1); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void onNext(Integer integer) {} |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onError(Throwable t) {} |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onComplete() {} |
| 56 | + }); |
| 57 | + await().untilAsserted(() -> assertThat(demand.get()).isEqualTo(1)); |
| 58 | + Thread.sleep(100); |
| 59 | + // The demand is still 1. |
| 60 | + assertThat(demand.get()).isEqualTo(1); |
| 61 | + } |
| 62 | +} |
0 commit comments