Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableOnBackpressureBufferTest.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.List;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.AtomicBoolean;
21
22 import org.junit.Test;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.observers.TestObserver;
31 import io.reactivex.rxjava3.operators.QueueFuseable;
32 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
33 import io.reactivex.rxjava3.processors.PublishProcessor;
34 import io.reactivex.rxjava3.schedulers.Schedulers;
35 import io.reactivex.rxjava3.subscribers.*;
36 import io.reactivex.rxjava3.testsupport.*;
37
38 public class FlowableOnBackpressureBufferTest extends RxJavaTest {
39
40 @Test
41 public void noBackpressureSupport() {
42 TestSubscriber<Long> ts = new TestSubscriber<>(0L);
43 // this will be ignored
44 ts.request(100);
45 // we take 500 so it unsubscribes
46 infinite.take(500).subscribe(ts);
47 // it completely ignores the `request(100)` and we get 500
48 assertEquals(500, ts.values().size());
49 ts.assertNoErrors();
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())
81 .onBackpressureBuffer()
82 .take(500)
83 .subscribe(ts);
84
85 // it completely ignores the `request(100)` and we get 500
86 l1.await();
87 assertEquals(100, ts.values().size());
88 ts.request(50);
89 l2.await();
90 assertEquals(150, ts.values().size());
91 ts.request(350);
92 ts.awaitDone(5, TimeUnit.SECONDS);
93 assertEquals(500, ts.values().size());
94 ts.assertNoErrors();
95 assertEquals(0, ts.values().get(0).intValue());
96 assertEquals(499, ts.values().get(499).intValue());
97 }
98
99 @Test(expected = IllegalArgumentException.class)
100 public void fixBackpressureBufferNegativeCapacity() throws InterruptedException {
101 Flowable.empty().onBackpressureBuffer(-1);
102 }
103
104 @Test(expected = IllegalArgumentException.class)
105 public void fixBackpressureBufferZeroCapacity() throws InterruptedException {
106 Flowable.empty().onBackpressureBuffer(0);
107 }
108
109 @Test
110 public void fixBackpressureBoundedBuffer() throws InterruptedException {
111 final CountDownLatch l1 = new CountDownLatch(100);
112 final CountDownLatch backpressureCallback = new CountDownLatch(1);
113 TestSubscriber<Long> ts = new TestSubscriber<>(new DefaultSubscriber<Long>() {
114
115 @Override
116 protected void onStart() {
117 }
118
119 @Override
120 public void onComplete() {
121 }
122
123 @Override
124 public void onError(Throwable e) {
125 }
126
127 @Override
128 public void onNext(Long t) {
129 l1.countDown();
130 }
131
132 }, 0L);
133
134 ts.request(100);
135 infinite.subscribeOn(Schedulers.computation())
136 .onBackpressureBuffer(500, new Action() {
137 @Override
138 public void run() {
139 backpressureCallback.countDown();
140 }
141 })
142 /*.take(1000)*/
143 .subscribe(ts);
144 l1.await();
145
146 ts.request(50);
147
148 assertTrue(backpressureCallback.await(500, TimeUnit.MILLISECONDS));
149 ts.awaitDone(1, TimeUnit.SECONDS);
150 ts.assertError(MissingBackpressureException.class);
151
152 int size = ts.values().size();
153 assertTrue(size <= 150); // will get up to 50 more
154 assertEquals((long)ts.values().get(size - 1), size - 1);
155 }
156
157 static final Flowable<Long> infinite = Flowable.unsafeCreate(new Publisher<Long>() {
158
159 @Override
160 public void subscribe(Subscriber<? super Long> s) {
161 BooleanSubscription bs = new BooleanSubscription();
162 s.onSubscribe(bs);
163 long i = 0;
164 while (!bs.isCancelled()) {
165 s.onNext(i++);
166 }
167 }
168
169 });
170
171 private static final Action THROWS_NON_FATAL = new Action() {
172
173 @Override
174 public void run() {
175 throw new RuntimeException();
176 }
177 };
178
179 @Test
180 public void nonFatalExceptionThrownByOnOverflowIsNotReportedByUpstream() {
181 final AtomicBoolean errorOccurred = new AtomicBoolean(false);
182 TestSubscriber<Long> ts = TestSubscriber.create(0);
183 infinite
184 .subscribeOn(Schedulers.computation())
185 .doOnError(new Consumer<Throwable>() {
186 @Override
187 public void accept(Throwable t) {
188 errorOccurred.set(true);
189 }
190 })
191 .onBackpressureBuffer(1, THROWS_NON_FATAL)
192 .subscribe(ts);
193 ts.awaitDone(5, TimeUnit.SECONDS);
194 assertFalse(errorOccurred.get());
195 }
196
197 @Test
198 public void maxSize() {
199 TestSubscriber<Integer> ts = TestSubscriber.create(0);
200
201 Flowable.range(1, 10).onBackpressureBuffer(1).subscribe(ts);
202
203 ts.assertNoValues();
204 ts.assertError(MissingBackpressureException.class);
205 ts.assertNotComplete();
206 }
207
208 @Test(expected = IllegalArgumentException.class)
209 public void fixBackpressureBufferNegativeCapacity2() throws InterruptedException {
210 Flowable.empty().onBackpressureBuffer(-1);
211 }
212
213 @Test(expected = IllegalArgumentException.class)
214 public void fixBackpressureBufferZeroCapacity2() throws InterruptedException {
215 Flowable.empty().onBackpressureBuffer(0);
216 }
217
218 @Test
219 public void noDelayError() {
220
221 Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
222 .onBackpressureBuffer(false)
223 .test(0L)
224 .assertFailure(TestException.class);
225 }
226
227 @Test
228 public void delayError() {
229 TestSubscriber<Integer> ts = Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
230 .onBackpressureBuffer(true)
231 .test(0L)
232 .assertEmpty();
233
234 ts.request(1);
235 ts.assertFailure(TestException.class, 1);
236
237 }
238
239 @Test
240 public void delayErrorBuffer() {
241 TestSubscriber<Integer> ts = Flowable.just(1).concatWith(Flowable.<Integer>error(new TestException()))
242 .onBackpressureBuffer(16, true)
243 .test(0L)
244 .assertEmpty();
245
246 ts.request(1);
247 ts.assertFailure(TestException.class, 1);
248 }
249
250 @Test
251 public void fusedNormal() {
252 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
253
254 Flowable.range(1, 10).onBackpressureBuffer().subscribe(ts);
255
256 ts.assertFuseable()
257 .assertFusionMode(QueueFuseable.ASYNC)
258 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
259 }
260
261 @Test
262 public void fusedError() {
263 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
264
265 Flowable.<Integer>error(new TestException()).onBackpressureBuffer().subscribe(ts);
266
267 ts.assertFuseable()
268 .assertFusionMode(QueueFuseable.ASYNC)
269 .assertFailure(TestException.class);
270 }
271
272 @Test
273 public void fusedPreconsume() throws Exception {
274
275 TestSubscriber<Integer> ts = Flowable.range(1, 1000 * 1000)
276 .onBackpressureBuffer()
277 .observeOn(Schedulers.single())
278 .test(0L);
279
280 ts.assertEmpty();
281
282 Thread.sleep(100);
283
284 ts.request(1000 * 1000);
285
286 ts
287 .awaitDone(5, TimeUnit.SECONDS)
288 .assertValueCount(1000 * 1000)
289 .assertNoErrors()
290 .assertComplete();
291 }
292
293 @Test
294 public void emptyDelayError() {
295 Flowable.empty()
296 .onBackpressureBuffer(true)
297 .test()
298 .assertResult();
299 }
300
301 @Test
302 public void fusionRejected() {
303 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC);
304
305 Flowable.<Integer>never().onBackpressureBuffer().subscribe(ts);
306
307 ts.assertFusionMode(QueueFuseable.NONE)
308 .assertEmpty();
309 }
310
311 @Test
312 public void fusedNoConcurrentCleanDueToCancel() {
313 for (int j = 0; j < TestHelper.RACE_LONG_LOOPS; j++) {
314 List<Throwable> errors = TestHelper.trackPluginErrors();
315 try {
316 final PublishProcessor<Integer> pp = PublishProcessor.create();
317
318 TestObserver<Integer> to = pp.onBackpressureBuffer(4, false, true)
319 .observeOn(Schedulers.io())
320 .map(Functions.<Integer>identity())
321 .observeOn(Schedulers.single())
322 .firstOrError()
323 .test();
324
325 for (int i = 0; pp.hasSubscribers(); i++) {
326 pp.onNext(i);
327 }
328
329 to
330 .awaitDone(5, TimeUnit.SECONDS)
331 ;
332
333 if (!errors.isEmpty()) {
334 throw new CompositeException(errors);
335 }
336
337 to.assertResult(0);
338 } finally {
339 RxJavaPlugins.reset();
340 }
341 }
342 }
343
344 @Test
345 public void doubleOnSubscribe() {
346 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.onBackpressureBuffer());
347 }
348
349 @Test
350 public void badRequest() {
351 TestHelper.assertBadRequestReported(Flowable.never().onBackpressureBuffer());
352 }
353 }