Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableDoFinallyTest.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.*;
19
20 import org.junit.Test;
21 import org.reactivestreams.*;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.operators.QueueFuseable;
28 import io.reactivex.rxjava3.operators.QueueSubscription;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.UnicastProcessor;
31 import io.reactivex.rxjava3.testsupport.*;
32
33 public class FlowableDoFinallyTest extends RxJavaTest implements Action {
34
35 int calls;
36
37 @Override
38 public void run() throws Exception {
39 calls++;
40 }
41
42 @Test
43 public void normalJust() {
44 Flowable.just(1)
45 .doFinally(this)
46 .test()
47 .assertResult(1);
48
49 assertEquals(1, calls);
50 }
51
52 @Test
53 public void normalEmpty() {
54 Flowable.empty()
55 .doFinally(this)
56 .test()
57 .assertResult();
58
59 assertEquals(1, calls);
60 }
61
62 @Test
63 public void normalError() {
64 Flowable.error(new TestException())
65 .doFinally(this)
66 .test()
67 .assertFailure(TestException.class);
68
69 assertEquals(1, calls);
70 }
71
72 @Test
73 public void normalTake() {
74 Flowable.range(1, 10)
75 .doFinally(this)
76 .take(5)
77 .test()
78 .assertResult(1, 2, 3, 4, 5);
79
80 assertEquals(1, calls);
81 }
82
83 @Test
84 public void doubleOnSubscribe() {
85 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
86 @Override
87 public Publisher<Object> apply(Flowable<Object> f) throws Exception {
88 return f.doFinally(FlowableDoFinallyTest.this);
89 }
90 });
91 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Publisher<Object>>() {
92 @Override
93 public Publisher<Object> apply(Flowable<Object> f) throws Exception {
94 return f.doFinally(FlowableDoFinallyTest.this).filter(Functions.alwaysTrue());
95 }
96 });
97 }
98
99 @Test
100 public void syncFused() {
101 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC);
102
103 Flowable.range(1, 5)
104 .doFinally(this)
105 .subscribe(ts);
106
107 ts.assertFusionMode(QueueFuseable.SYNC)
108 .assertResult(1, 2, 3, 4, 5);
109
110 assertEquals(1, calls);
111 }
112
113 @Test
114 public void syncFusedBoundary() {
115 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC | QueueFuseable.BOUNDARY);
116
117 Flowable.range(1, 5)
118 .doFinally(this)
119 .subscribe(ts);
120
121 ts.assertFusionMode(QueueFuseable.NONE)
122 .assertResult(1, 2, 3, 4, 5);
123
124 assertEquals(1, calls);
125 }
126
127 @Test
128 public void asyncFused() {
129 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC);
130
131 UnicastProcessor<Integer> up = UnicastProcessor.create();
132 TestHelper.emit(up, 1, 2, 3, 4, 5);
133
134 up
135 .doFinally(this)
136 .subscribe(ts);
137
138 ts.assertFusionMode(QueueFuseable.ASYNC)
139 .assertResult(1, 2, 3, 4, 5);
140
141 assertEquals(1, calls);
142 }
143
144 @Test
145 public void asyncFusedBoundary() {
146 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC | QueueFuseable.BOUNDARY);
147
148 UnicastProcessor<Integer> up = UnicastProcessor.create();
149 TestHelper.emit(up, 1, 2, 3, 4, 5);
150
151 up
152 .doFinally(this)
153 .subscribe(ts);
154
155 ts.assertFusionMode(QueueFuseable.NONE)
156 .assertResult(1, 2, 3, 4, 5);
157
158 assertEquals(1, calls);
159 }
160
161 @Test
162 public void normalJustConditional() {
163 Flowable.just(1)
164 .doFinally(this)
165 .filter(Functions.alwaysTrue())
166 .test()
167 .assertResult(1);
168
169 assertEquals(1, calls);
170 }
171
172 @Test
173 public void normalEmptyConditional() {
174 Flowable.empty()
175 .doFinally(this)
176 .filter(Functions.alwaysTrue())
177 .test()
178 .assertResult();
179
180 assertEquals(1, calls);
181 }
182
183 @Test
184 public void normalErrorConditional() {
185 Flowable.error(new TestException())
186 .doFinally(this)
187 .filter(Functions.alwaysTrue())
188 .test()
189 .assertFailure(TestException.class);
190
191 assertEquals(1, calls);
192 }
193
194 @Test
195 public void normalTakeConditional() {
196 Flowable.range(1, 10)
197 .doFinally(this)
198 .filter(Functions.alwaysTrue())
199 .take(5)
200 .test()
201 .assertResult(1, 2, 3, 4, 5);
202
203 assertEquals(1, calls);
204 }
205
206 @Test
207 public void syncFusedConditional() {
208 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC);
209
210 Flowable.range(1, 5)
211 .doFinally(this)
212 .compose(TestHelper.conditional())
213 .subscribe(ts);
214
215 ts.assertFusionMode(QueueFuseable.SYNC)
216 .assertResult(1, 2, 3, 4, 5);
217
218 assertEquals(1, calls);
219 }
220
221 @Test
222 public void nonFused() {
223 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC);
224
225 Flowable.range(1, 5).hide()
226 .doFinally(this)
227 .subscribe(ts);
228
229 ts.assertFusionMode(QueueFuseable.NONE)
230 .assertResult(1, 2, 3, 4, 5);
231
232 assertEquals(1, calls);
233 }
234
235 @Test
236 public void nonFusedConditional() {
237 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC);
238
239 Flowable.range(1, 5).hide()
240 .doFinally(this)
241 .compose(TestHelper.conditional())
242 .subscribe(ts);
243
244 ts.assertFusionMode(QueueFuseable.NONE)
245 .assertResult(1, 2, 3, 4, 5);
246
247 assertEquals(1, calls);
248 }
249
250 @Test
251 public void syncFusedBoundaryConditional() {
252 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.SYNC | QueueFuseable.BOUNDARY);
253
254 Flowable.range(1, 5)
255 .doFinally(this)
256 .compose(TestHelper.conditional())
257 .subscribe(ts);
258
259 ts.assertFusionMode(QueueFuseable.NONE)
260 .assertResult(1, 2, 3, 4, 5);
261
262 assertEquals(1, calls);
263 }
264
265 @Test
266 public void asyncFusedConditional() {
267 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC);
268
269 UnicastProcessor<Integer> up = UnicastProcessor.create();
270 TestHelper.emit(up, 1, 2, 3, 4, 5);
271
272 up
273 .doFinally(this)
274 .compose(TestHelper.conditional())
275 .subscribe(ts);
276
277 ts.assertFusionMode(QueueFuseable.ASYNC)
278 .assertResult(1, 2, 3, 4, 5);
279
280 assertEquals(1, calls);
281 }
282
283 @Test
284 public void asyncFusedBoundaryConditional() {
285 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ASYNC | QueueFuseable.BOUNDARY);
286
287 UnicastProcessor<Integer> up = UnicastProcessor.create();
288 TestHelper.emit(up, 1, 2, 3, 4, 5);
289
290 up
291 .doFinally(this)
292 .compose(TestHelper.conditional())
293 .subscribe(ts);
294
295 ts.assertFusionMode(QueueFuseable.NONE)
296 .assertResult(1, 2, 3, 4, 5);
297
298 assertEquals(1, calls);
299 }
300
301 @Test
302 public void actionThrows() {
303 List<Throwable> errors = TestHelper.trackPluginErrors();
304 try {
305 Flowable.just(1)
306 .doFinally(new Action() {
307 @Override
308 public void run() throws Exception {
309 throw new TestException();
310 }
311 })
312 .test()
313 .assertResult(1)
314 .cancel();
315
316 TestHelper.assertUndeliverable(errors, 0, TestException.class);
317 } finally {
318 RxJavaPlugins.reset();
319 }
320 }
321
322 @Test
323 public void actionThrowsConditional() {
324 List<Throwable> errors = TestHelper.trackPluginErrors();
325 try {
326 Flowable.just(1)
327 .doFinally(new Action() {
328 @Override
329 public void run() throws Exception {
330 throw new TestException();
331 }
332 })
333 .filter(Functions.alwaysTrue())
334 .test()
335 .assertResult(1)
336 .cancel();
337
338 TestHelper.assertUndeliverable(errors, 0, TestException.class);
339 } finally {
340 RxJavaPlugins.reset();
341 }
342 }
343
344 @Test
345 public void clearIsEmpty() {
346 Flowable.range(1, 5)
347 .doFinally(this)
348 .subscribe(new FlowableSubscriber<Integer>() {
349
350 @Override
351 public void onSubscribe(Subscription s) {
352 @SuppressWarnings("unchecked")
353 QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s;
354
355 qs.requestFusion(QueueFuseable.ANY);
356
357 assertFalse(qs.isEmpty());
358
359 try {
360 assertEquals(1, qs.poll().intValue());
361 } catch (Throwable ex) {
362 throw new RuntimeException(ex);
363 }
364
365 assertFalse(qs.isEmpty());
366
367 qs.clear();
368
369 assertTrue(qs.isEmpty());
370
371 qs.cancel();
372 }
373
374 @Override
375 public void onNext(Integer t) {
376 }
377
378 @Override
379 public void onError(Throwable t) {
380 }
381
382 @Override
383 public void onComplete() {
384 }
385 });
386
387 assertEquals(1, calls);
388 }
389
390 @Test
391 public void clearIsEmptyConditional() {
392 Flowable.range(1, 5)
393 .doFinally(this)
394 .filter(Functions.alwaysTrue())
395 .subscribe(new FlowableSubscriber<Integer>() {
396
397 @Override
398 public void onSubscribe(Subscription s) {
399 @SuppressWarnings("unchecked")
400 QueueSubscription<Integer> qs = (QueueSubscription<Integer>)s;
401
402 qs.requestFusion(QueueFuseable.ANY);
403
404 assertFalse(qs.isEmpty());
405
406 try {
407 assertEquals(1, qs.poll().intValue());
408 } catch (Throwable ex) {
409 throw new RuntimeException(ex);
410 }
411
412 assertFalse(qs.isEmpty());
413
414 qs.clear();
415
416 assertTrue(qs.isEmpty());
417
418 qs.cancel();
419 }
420
421 @Override
422 public void onNext(Integer t) {
423 }
424
425 @Override
426 public void onError(Throwable t) {
427 }
428
429 @Override
430 public void onComplete() {
431 }
432 });
433
434 assertEquals(1, calls);
435 }
436
437 @Test
438 public void eventOrdering() {
439 final List<String> list = new ArrayList<>();
440
441 Flowable.error(new TestException())
442 .doOnCancel(new Action() {
443 @Override
444 public void run() throws Exception {
445 list.add("cancel");
446 }
447 })
448 .doFinally(new Action() {
449 @Override
450 public void run() throws Exception {
451 list.add("finally");
452 }
453 })
454 .subscribe(
455 new Consumer<Object>() {
456 @Override
457 public void accept(Object v) throws Exception {
458 list.add("onNext");
459 }
460 },
461 new Consumer<Throwable>() {
462 @Override
463 public void accept(Throwable e) throws Exception {
464 list.add("onError");
465 }
466 },
467 new Action() {
468 @Override
469 public void run() throws Exception {
470 list.add("onComplete");
471 }
472 });
473
474 assertEquals(Arrays.asList("onError", "finally"), list);
475 }
476
477 @Test
478 public void eventOrdering2() {
479 final List<String> list = new ArrayList<>();
480
481 Flowable.just(1)
482 .doOnCancel(new Action() {
483 @Override
484 public void run() throws Exception {
485 list.add("cancel");
486 }
487 })
488 .doFinally(new Action() {
489 @Override
490 public void run() throws Exception {
491 list.add("finally");
492 }
493 })
494 .subscribe(
495 new Consumer<Object>() {
496 @Override
497 public void accept(Object v) throws Exception {
498 list.add("onNext");
499 }
500 },
501 new Consumer<Throwable>() {
502 @Override
503 public void accept(Throwable e) throws Exception {
504 list.add("onError");
505 }
506 },
507 new Action() {
508 @Override
509 public void run() throws Exception {
510 list.add("onComplete");
511 }
512 });
513
514 assertEquals(Arrays.asList("onNext", "onComplete", "finally"), list);
515 }
516
517 @Test
518 public void fusionRejected() {
519 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
520 ts.setInitialFusionMode(QueueFuseable.ANY);
521
522 TestHelper.rejectFlowableFusion()
523 .doFinally(() -> { })
524 .subscribeWith(ts);
525
526 ts.assertFuseable()
527 .assertFusionMode(QueueFuseable.NONE);
528 }
529
530 @Test
531 public void fusionRejectedConditional() {
532 TestSubscriberEx<Object> ts = new TestSubscriberEx<>();
533 ts.setInitialFusionMode(QueueFuseable.ANY);
534
535 TestHelper.rejectFlowableFusion()
536 .doFinally(() -> { })
537 .compose(TestHelper.conditional())
538 .subscribeWith(ts);
539
540 ts.assertFuseable()
541 .assertFusionMode(QueueFuseable.NONE);
542 }
543 }