Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/completable/CompletableUsingTest.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.completable;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.disposables.*;
24 import io.reactivex.rxjava3.exceptions.*;
25 import io.reactivex.rxjava3.functions.*;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.observers.TestObserver;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.subjects.PublishSubject;
30 import io.reactivex.rxjava3.testsupport.*;
31
32 public class CompletableUsingTest extends RxJavaTest {
33
34 @Test
35 public void resourceSupplierThrows() {
36
37 Completable.using(new Supplier<Object>() {
38 @Override
39 public Object get() throws Exception {
40 throw new TestException();
41 }
42 }, new Function<Object, CompletableSource>() {
43 @Override
44 public CompletableSource apply(Object v) throws Exception {
45 return Completable.complete();
46 }
47 }, new Consumer<Object>() {
48 @Override
49 public void accept(Object d) throws Exception {
50
51 }
52 })
53 .test()
54 .assertFailure(TestException.class);
55 }
56
57 @Test
58 public void errorEager() {
59
60 Completable.using(new Supplier<Object>() {
61 @Override
62 public Object get() throws Exception {
63 return 1;
64 }
65 }, new Function<Object, CompletableSource>() {
66 @Override
67 public CompletableSource apply(Object v) throws Exception {
68 return Completable.error(new TestException());
69 }
70 }, new Consumer<Object>() {
71 @Override
72 public void accept(Object d) throws Exception {
73
74 }
75 }, true)
76 .test()
77 .assertFailure(TestException.class);
78 }
79
80 @Test
81 public void emptyEager() {
82
83 Completable.using(new Supplier<Object>() {
84 @Override
85 public Object get() throws Exception {
86 return 1;
87 }
88 }, new Function<Object, CompletableSource>() {
89 @Override
90 public CompletableSource apply(Object v) throws Exception {
91 return Completable.complete();
92 }
93 }, new Consumer<Object>() {
94 @Override
95 public void accept(Object d) throws Exception {
96
97 }
98 }, true)
99 .test()
100 .assertResult();
101 }
102
103 @Test
104 public void errorNonEager() {
105
106 Completable.using(new Supplier<Object>() {
107 @Override
108 public Object get() throws Exception {
109 return 1;
110 }
111 }, new Function<Object, CompletableSource>() {
112 @Override
113 public CompletableSource apply(Object v) throws Exception {
114 return Completable.error(new TestException());
115 }
116 }, new Consumer<Object>() {
117 @Override
118 public void accept(Object d) throws Exception {
119
120 }
121 }, false)
122 .test()
123 .assertFailure(TestException.class);
124 }
125
126 @Test
127 public void emptyNonEager() {
128
129 Completable.using(new Supplier<Object>() {
130 @Override
131 public Object get() throws Exception {
132 return 1;
133 }
134 }, new Function<Object, CompletableSource>() {
135 @Override
136 public CompletableSource apply(Object v) throws Exception {
137 return Completable.complete();
138 }
139 }, new Consumer<Object>() {
140 @Override
141 public void accept(Object d) throws Exception {
142
143 }
144 }, false)
145 .test()
146 .assertResult();
147 }
148
149 @Test
150 public void supplierCrashEager() {
151
152 Completable.using(new Supplier<Object>() {
153 @Override
154 public Object get() throws Exception {
155 return 1;
156 }
157 }, new Function<Object, CompletableSource>() {
158 @Override
159 public CompletableSource apply(Object v) throws Exception {
160 throw new TestException();
161 }
162 }, new Consumer<Object>() {
163 @Override
164 public void accept(Object d) throws Exception {
165
166 }
167 }, true)
168 .test()
169 .assertFailure(TestException.class);
170 }
171
172 @Test
173 public void supplierCrashNonEager() {
174
175 Completable.using(new Supplier<Object>() {
176 @Override
177 public Object get() throws Exception {
178 return 1;
179 }
180 }, new Function<Object, CompletableSource>() {
181 @Override
182 public CompletableSource apply(Object v) throws Exception {
183 throw new TestException();
184 }
185 }, new Consumer<Object>() {
186 @Override
187 public void accept(Object d) throws Exception {
188
189 }
190 }, false)
191 .test()
192 .assertFailure(TestException.class);
193 }
194
195 @Test
196 public void supplierAndDisposerCrashEager() {
197 TestObserverEx<Void> to = Completable.using(new Supplier<Object>() {
198 @Override
199 public Object get() throws Exception {
200 return 1;
201 }
202 }, new Function<Object, CompletableSource>() {
203 @Override
204 public CompletableSource apply(Object v) throws Exception {
205 throw new TestException("Main");
206 }
207 }, new Consumer<Object>() {
208 @Override
209 public void accept(Object d) throws Exception {
210 throw new TestException("Disposer");
211 }
212 }, true)
213 .to(TestHelper.<Void>testConsumer())
214 .assertFailure(CompositeException.class);
215
216 List<Throwable> list = TestHelper.compositeList(to.errors().get(0));
217
218 TestHelper.assertError(list, 0, TestException.class, "Main");
219 TestHelper.assertError(list, 1, TestException.class, "Disposer");
220 }
221
222 @Test
223 public void supplierAndDisposerCrashNonEager() {
224 List<Throwable> errors = TestHelper.trackPluginErrors();
225 try {
226 Completable.using(new Supplier<Object>() {
227 @Override
228 public Object get() throws Exception {
229 return 1;
230 }
231 }, new Function<Object, CompletableSource>() {
232 @Override
233 public CompletableSource apply(Object v) throws Exception {
234 throw new TestException("Main");
235 }
236 }, new Consumer<Object>() {
237 @Override
238 public void accept(Object d) throws Exception {
239 throw new TestException("Disposer");
240 }
241 }, false)
242 .to(TestHelper.<Void>testConsumer())
243 .assertFailureAndMessage(TestException.class, "Main");
244
245 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Disposer");
246 } finally {
247 RxJavaPlugins.reset();
248 }
249 }
250
251 @Test
252 public void dispose() {
253 final int[] call = {0 };
254
255 TestObserverEx<Void> to = Completable.using(new Supplier<Object>() {
256 @Override
257 public Object get() throws Exception {
258 return 1;
259 }
260 }, new Function<Object, CompletableSource>() {
261 @Override
262 public CompletableSource apply(Object v) throws Exception {
263 return Completable.never();
264 }
265 }, new Consumer<Object>() {
266 @Override
267 public void accept(Object d) throws Exception {
268 call[0]++;
269 }
270 }, false)
271 .to(TestHelper.<Void>testConsumer());
272
273 to.dispose();
274
275 assertEquals(1, call[0]);
276 }
277
278 @Test
279 public void disposeCrashes() {
280 List<Throwable> errors = TestHelper.trackPluginErrors();
281 try {
282 TestObserver<Void> to = Completable.using(new Supplier<Object>() {
283 @Override
284 public Object get() throws Exception {
285 return 1;
286 }
287 }, new Function<Object, CompletableSource>() {
288 @Override
289 public CompletableSource apply(Object v) throws Exception {
290 return Completable.never();
291 }
292 }, new Consumer<Object>() {
293 @Override
294 public void accept(Object d) throws Exception {
295 throw new TestException();
296 }
297 }, false)
298 .test();
299
300 to.dispose();
301
302 TestHelper.assertUndeliverable(errors, 0, TestException.class);
303 } finally {
304 RxJavaPlugins.reset();
305 }
306 }
307
308 @Test
309 public void isDisposed() {
310 TestHelper.checkDisposed(Completable.using(new Supplier<Object>() {
311 @Override
312 public Object get() throws Exception {
313 return 1;
314 }
315 }, new Function<Object, CompletableSource>() {
316 @Override
317 public CompletableSource apply(Object v) throws Exception {
318 return Completable.never();
319 }
320 }, new Consumer<Object>() {
321 @Override
322 public void accept(Object d) throws Exception {
323
324 }
325 }, false));
326 }
327
328 @Test
329 public void justDisposerCrashes() {
330 Completable.using(new Supplier<Object>() {
331 @Override
332 public Object get() throws Exception {
333 return 1;
334 }
335 }, new Function<Object, CompletableSource>() {
336 @Override
337 public CompletableSource apply(Object v) throws Exception {
338 return Completable.complete();
339 }
340 }, new Consumer<Object>() {
341 @Override
342 public void accept(Object d) throws Exception {
343 throw new TestException("Disposer");
344 }
345 }, true)
346 .test()
347 .assertFailure(TestException.class);
348 }
349
350 @Test
351 public void emptyDisposerCrashes() {
352 Completable.using(new Supplier<Object>() {
353 @Override
354 public Object get() throws Exception {
355 return 1;
356 }
357 }, new Function<Object, CompletableSource>() {
358 @Override
359 public CompletableSource apply(Object v) throws Exception {
360 return Completable.complete();
361 }
362 }, new Consumer<Object>() {
363 @Override
364 public void accept(Object d) throws Exception {
365 throw new TestException("Disposer");
366 }
367 }, true)
368 .test()
369 .assertFailure(TestException.class);
370 }
371
372 @Test
373 public void errorDisposerCrash() {
374 TestObserverEx<Void> to = Completable.using(new Supplier<Object>() {
375 @Override
376 public Object get() throws Exception {
377 return 1;
378 }
379 }, new Function<Object, CompletableSource>() {
380 @Override
381 public CompletableSource apply(Object v) throws Exception {
382 return Completable.error(new TestException("Main"));
383 }
384 }, new Consumer<Object>() {
385 @Override
386 public void accept(Object d) throws Exception {
387 throw new TestException("Disposer");
388 }
389 }, true)
390 .to(TestHelper.<Void>testConsumer())
391 .assertFailure(CompositeException.class);
392
393 List<Throwable> list = TestHelper.compositeList(to.errors().get(0));
394
395 TestHelper.assertError(list, 0, TestException.class, "Main");
396 TestHelper.assertError(list, 1, TestException.class, "Disposer");
397 }
398
399 @Test
400 public void doubleOnSubscribe() {
401 List<Throwable> errors = TestHelper.trackPluginErrors();
402 try {
403 Completable.using(new Supplier<Object>() {
404 @Override
405 public Object get() throws Exception {
406 return 1;
407 }
408 }, new Function<Object, CompletableSource>() {
409 @Override
410 public CompletableSource apply(Object v) throws Exception {
411 return Completable.wrap(new CompletableSource() {
412 @Override
413 public void subscribe(CompletableObserver observer) {
414 Disposable d1 = Disposable.empty();
415
416 observer.onSubscribe(d1);
417
418 Disposable d2 = Disposable.empty();
419
420 observer.onSubscribe(d2);
421
422 assertFalse(d1.isDisposed());
423
424 assertTrue(d2.isDisposed());
425 }
426 });
427 }
428 }, new Consumer<Object>() {
429 @Override
430 public void accept(Object d) throws Exception {
431
432 }
433 }, false).test();
434 TestHelper.assertError(errors, 0, IllegalStateException.class, "Disposable already set!");
435 } finally {
436 RxJavaPlugins.reset();
437 }
438 }
439
440 @Test
441 public void successDisposeRace() {
442 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
443
444 final PublishSubject<Integer> ps = PublishSubject.create();
445
446 final TestObserverEx<Void> to = Completable.using(new Supplier<Object>() {
447 @Override
448 public Object get() throws Exception {
449 return 1;
450 }
451 }, new Function<Object, CompletableSource>() {
452 @Override
453 public CompletableSource apply(Object v) throws Exception {
454 return ps.ignoreElements();
455 }
456 }, new Consumer<Object>() {
457 @Override
458 public void accept(Object d) throws Exception {
459 }
460 }, true)
461 .to(TestHelper.<Void>testConsumer());
462
463 ps.onNext(1);
464
465 Runnable r1 = new Runnable() {
466 @Override
467 public void run() {
468 to.dispose();
469 }
470 };
471
472 Runnable r2 = new Runnable() {
473 @Override
474 public void run() {
475 ps.onComplete();
476 }
477 };
478
479 TestHelper.race(r1, r2);
480 }
481 }
482
483 @Test
484 public void errorDisposeRace() {
485 RxJavaPlugins.setErrorHandler(Functions.emptyConsumer());
486 try {
487 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
488
489 final PublishSubject<Integer> ps = PublishSubject.create();
490
491 final TestObserver<Void> to = Completable.using(new Supplier<Object>() {
492 @Override
493 public Object get() throws Exception {
494 return 1;
495 }
496 }, new Function<Object, CompletableSource>() {
497 @Override
498 public CompletableSource apply(Object v) throws Exception {
499 return ps.ignoreElements();
500 }
501 }, new Consumer<Object>() {
502 @Override
503 public void accept(Object d) throws Exception {
504 }
505 }, true)
506 .test();
507
508 final TestException ex = new TestException();
509
510 Runnable r1 = new Runnable() {
511 @Override
512 public void run() {
513 to.dispose();
514 }
515 };
516
517 Runnable r2 = new Runnable() {
518 @Override
519 public void run() {
520 ps.onError(ex);
521 }
522 };
523
524 TestHelper.race(r1, r2);
525 }
526 } finally {
527 RxJavaPlugins.reset();
528 }
529 }
530
531 @Test
532 public void emptyDisposeRace() {
533 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
534
535 final PublishSubject<Integer> ps = PublishSubject.create();
536
537 final TestObserver<Void> to = Completable.using(new Supplier<Object>() {
538 @Override
539 public Object get() throws Exception {
540 return 1;
541 }
542 }, new Function<Object, CompletableSource>() {
543 @Override
544 public CompletableSource apply(Object v) throws Exception {
545 return ps.ignoreElements();
546 }
547 }, new Consumer<Object>() {
548 @Override
549 public void accept(Object d) throws Exception {
550
551 }
552 }, true)
553 .test();
554
555 Runnable r1 = new Runnable() {
556 @Override
557 public void run() {
558 to.dispose();
559 }
560 };
561
562 Runnable r2 = new Runnable() {
563 @Override
564 public void run() {
565 ps.onComplete();
566 }
567 };
568
569 TestHelper.race(r1, r2);
570 }
571 }
572
573 @Test
574 public void eagerDisposeResourceThenDisposeUpstream() {
575 final StringBuilder sb = new StringBuilder();
576
577 TestObserver<Void> to = Completable.using(Functions.justSupplier(1),
578 new Function<Integer, Completable>() {
579 @Override
580 public Completable apply(Integer t) throws Throwable {
581 return Completable.never()
582 .doOnDispose(new Action() {
583 @Override
584 public void run() throws Throwable {
585 sb.append("Dispose");
586 }
587 })
588 ;
589 }
590 }, new Consumer<Integer>() {
591 @Override
592 public void accept(Integer t) throws Throwable {
593 sb.append("Resource");
594 }
595 }, true)
596 .test()
597 ;
598 to.assertEmpty();
599
600 to.dispose();
601
602 assertEquals("ResourceDispose", sb.toString());
603 }
604
605 @Test
606 public void nonEagerDisposeUpstreamThenDisposeResource() {
607 final StringBuilder sb = new StringBuilder();
608
609 TestObserver<Void> to = Completable.using(Functions.justSupplier(1),
610 new Function<Integer, Completable>() {
611 @Override
612 public Completable apply(Integer t) throws Throwable {
613 return Completable.never()
614 .doOnDispose(new Action() {
615 @Override
616 public void run() throws Throwable {
617 sb.append("Dispose");
618 }
619 })
620 ;
621 }
622 }, new Consumer<Integer>() {
623 @Override
624 public void accept(Integer t) throws Throwable {
625 sb.append("Resource");
626 }
627 }, false)
628 .test()
629 ;
630 to.assertEmpty();
631
632 to.dispose();
633
634 assertEquals("DisposeResource", sb.toString());
635 }
636 }