Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableOnBackpressureDropTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.flowable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.concurrent.*;
19 import java.util.concurrent.atomic.*;
20
21 import org.junit.Test;
22 import org.reactivestreams.*;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
27 import io.reactivex.rxjava3.schedulers.Schedulers;
28 import io.reactivex.rxjava3.subscribers.*;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class FlowableOnBackpressureDropTest extends RxJavaTest {
32
33 @Test
34 public void noBackpressureSupport() {
35 TestSubscriber<Long> ts = new TestSubscriber<>(0L);
36 // this will be ignored
37 ts.request(100);
38 // we take 500 so it unsubscribes
39 infinite.take(500).subscribe(ts);
40 // it completely ignores the `request(100)` and we get 500
41 assertEquals(500, ts.values().size());
42 ts.assertNoErrors();
43 }
44
45 @Test
46 public void withObserveOn() throws InterruptedException {
47 TestSubscriber<Integer> ts = new TestSubscriber<>();
48 Flowable.range(0, Flowable.bufferSize() * 10).onBackpressureDrop().observeOn(Schedulers.io()).subscribe(ts);
49 ts.awaitDone(5, TimeUnit.SECONDS);
50 }
51
52 @Test
53 public void fixBackpressureWithBuffer() throws InterruptedException {
54 final CountDownLatch l1 = new CountDownLatch(100);
55 final CountDownLatch l2 = new CountDownLatch(150);
56 TestSubscriber<Long> ts = new TestSubscriber<>(new DefaultSubscriber<Long>() {
57
58 @Override
59 protected void onStart() {
60 }
61
62 @Override
63 public void onComplete() {
64 }
65
66 @Override
67 public void onError(Throwable e) {
68 }
69
70 @Override
71 public void onNext(Long t) {
72 l1.countDown();
73 l2.countDown();
74 }
75
76 }, 0L);
77 // this will be ignored
78 ts.request(100);
79 // we take 500 so it unsubscribes
80 infinite.subscribeOn(Schedulers.computation()).onBackpressureDrop().take(500).subscribe(ts);
81 // it completely ignores the `request(100)` and we get 500
82 l1.await();
83 assertEquals(100, ts.values().size());
84 ts.request(50);
85 l2.await();
86 assertEquals(150, ts.values().size());
87 ts.request(350);
88 ts.awaitDone(5, TimeUnit.SECONDS);
89 assertEquals(500, ts.values().size());
90 ts.assertNoErrors();
91 assertEquals(0, ts.values().get(0).intValue());
92 }
93
94 @Test
95 public void requestOverflow() throws InterruptedException {
96 final AtomicInteger count = new AtomicInteger();
97 int n = 10;
98 range(n).onBackpressureDrop().subscribe(new DefaultSubscriber<Long>() {
99
100 @Override
101 public void onStart() {
102 request(10);
103 }
104
105 @Override
106 public void onComplete() {
107 }
108
109 @Override
110 public void onError(Throwable e) {
111 throw new RuntimeException(e);
112 }
113
114 @Override
115 public void onNext(Long t) {
116 count.incrementAndGet();
117 //cause overflow of requested if not handled properly in onBackpressureDrop operator
118 request(Long.MAX_VALUE - 1);
119 }});
120 assertEquals(n, count.get());
121 }
122
123 static final Flowable<Long> infinite = Flowable.unsafeCreate(new Publisher<Long>() {
124
125 @Override
126 public void subscribe(Subscriber<? super Long> s) {
127 BooleanSubscription bs = new BooleanSubscription();
128 s.onSubscribe(bs);
129 long i = 0;
130 while (!bs.isCancelled()) {
131 s.onNext(i++);
132 }
133 }
134
135 });
136
137 private static Flowable<Long> range(final long n) {
138 return Flowable.unsafeCreate(new Publisher<Long>() {
139
140 @Override
141 public void subscribe(Subscriber<? super Long> s) {
142 BooleanSubscription bs = new BooleanSubscription();
143 s.onSubscribe(bs);
144 for (long i = 0; i < n; i++) {
145 if (bs.isCancelled()) {
146 break;
147 }
148 s.onNext(i);
149 }
150 s.onComplete();
151 }
152
153 });
154 }
155
156 private static final Consumer<Long> THROW_NON_FATAL = new Consumer<Long>() {
157 @Override
158 public void accept(Long n) {
159 throw new RuntimeException();
160 }
161 };
162
163 @Test
164 public void nonFatalExceptionFromOverflowActionIsNotReportedFromUpstreamOperator() {
165 final AtomicBoolean errorOccurred = new AtomicBoolean(false);
166 //request 0
167 TestSubscriber<Long> ts = TestSubscriber.create(0);
168 //range method emits regardless of requests so should trigger onBackpressureDrop action
169 range(2)
170 // if haven't caught exception in onBackpressureDrop operator then would incorrectly
171 // be picked up by this call to doOnError
172 .doOnError(new Consumer<Throwable>() {
173 @Override
174 public void accept(Throwable t) {
175 errorOccurred.set(true);
176 }
177 })
178 .onBackpressureDrop(THROW_NON_FATAL)
179 .subscribe(ts);
180
181 assertFalse(errorOccurred.get());
182 }
183
184 @Test
185 public void badSource() {
186 TestHelper.checkBadSourceFlowable(new Function<Flowable<Integer>, Object>() {
187 @Override
188 public Object apply(Flowable<Integer> f) throws Exception {
189 return f.onBackpressureDrop();
190 }
191 }, false, 1, 1, 1);
192 }
193
194 @Test
195 public void doubleOnSubscribe() {
196 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
197 @Override
198 public Publisher<Object> apply(Flowable<Object> f) throws Exception {
199 return f.onBackpressureDrop();
200 }
201 });
202 }
203
204 @Test
205 public void badRequest() {
206 TestHelper.assertBadRequestReported(Flowable.just(1).onBackpressureDrop());
207 }
208 }