Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeUsingTest.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.maybe;
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 MaybeUsingTest extends RxJavaTest {
33
34 @Test
35 public void resourceSupplierThrows() {
36
37 Maybe.using(new Supplier<Object>() {
38 @Override
39 public Object get() throws Exception {
40 throw new TestException();
41 }
42 }, new Function<Object, MaybeSource<Integer>>() {
43 @Override
44 public MaybeSource<Integer> apply(Object v) throws Exception {
45 return Maybe.just(1);
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 Maybe.using(new Supplier<Object>() {
61 @Override
62 public Object get() throws Exception {
63 return 1;
64 }
65 }, new Function<Object, MaybeSource<Integer>>() {
66 @Override
67 public MaybeSource<Integer> apply(Object v) throws Exception {
68 return Maybe.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 Maybe.using(new Supplier<Object>() {
84 @Override
85 public Object get() throws Exception {
86 return 1;
87 }
88 }, new Function<Object, MaybeSource<Integer>>() {
89 @Override
90 public MaybeSource<Integer> apply(Object v) throws Exception {
91 return Maybe.empty();
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 Maybe.using(new Supplier<Object>() {
107 @Override
108 public Object get() throws Exception {
109 return 1;
110 }
111 }, new Function<Object, MaybeSource<Integer>>() {
112 @Override
113 public MaybeSource<Integer> apply(Object v) throws Exception {
114 return Maybe.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 Maybe.using(new Supplier<Object>() {
130 @Override
131 public Object get() throws Exception {
132 return 1;
133 }
134 }, new Function<Object, MaybeSource<Integer>>() {
135 @Override
136 public MaybeSource<Integer> apply(Object v) throws Exception {
137 return Maybe.empty();
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 Maybe.using(new Supplier<Object>() {
153 @Override
154 public Object get() throws Exception {
155 return 1;
156 }
157 }, new Function<Object, MaybeSource<Integer>>() {
158 @Override
159 public MaybeSource<Integer> 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 Maybe.using(new Supplier<Object>() {
176 @Override
177 public Object get() throws Exception {
178 return 1;
179 }
180 }, new Function<Object, MaybeSource<Integer>>() {
181 @Override
182 public MaybeSource<Integer> 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<Integer> to = Maybe.using(new Supplier<Object>() {
198 @Override
199 public Object get() throws Exception {
200 return 1;
201 }
202 }, new Function<Object, MaybeSource<Integer>>() {
203 @Override
204 public MaybeSource<Integer> 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.<Integer>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 Maybe.using(new Supplier<Object>() {
227 @Override
228 public Object get() throws Exception {
229 return 1;
230 }
231 }, new Function<Object, MaybeSource<Integer>>() {
232 @Override
233 public MaybeSource<Integer> 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.<Integer>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 TestObserver<Integer> to = Maybe.using(new Supplier<Object>() {
256 @Override
257 public Object get() throws Exception {
258 return 1;
259 }
260 }, new Function<Object, MaybeSource<Integer>>() {
261 @Override
262 public MaybeSource<Integer> apply(Object v) throws Exception {
263 return Maybe.never();
264 }
265 }, new Consumer<Object>() {
266 @Override
267 public void accept(Object d) throws Exception {
268 call[0]++;
269 }
270 }, false)
271 .test();
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<Integer> to = Maybe.using(new Supplier<Object>() {
283 @Override
284 public Object get() throws Exception {
285 return 1;
286 }
287 }, new Function<Object, MaybeSource<Integer>>() {
288 @Override
289 public MaybeSource<Integer> apply(Object v) throws Exception {
290 return Maybe.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(Maybe.using(new Supplier<Object>() {
311 @Override
312 public Object get() throws Exception {
313 return 1;
314 }
315 }, new Function<Object, MaybeSource<Integer>>() {
316 @Override
317 public MaybeSource<Integer> apply(Object v) throws Exception {
318 return Maybe.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 Maybe.using(new Supplier<Object>() {
331 @Override
332 public Object get() throws Exception {
333 return 1;
334 }
335 }, new Function<Object, MaybeSource<Integer>>() {
336 @Override
337 public MaybeSource<Integer> apply(Object v) throws Exception {
338 return Maybe.just(1);
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 Maybe.using(new Supplier<Object>() {
353 @Override
354 public Object get() throws Exception {
355 return 1;
356 }
357 }, new Function<Object, MaybeSource<Integer>>() {
358 @Override
359 public MaybeSource<Integer> apply(Object v) throws Exception {
360 return Maybe.empty();
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<Integer> to = Maybe.using(new Supplier<Object>() {
375 @Override
376 public Object get() throws Exception {
377 return 1;
378 }
379 }, new Function<Object, MaybeSource<Integer>>() {
380 @Override
381 public MaybeSource<Integer> apply(Object v) throws Exception {
382 return Maybe.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.<Integer>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 Maybe.using(new Supplier<Object>() {
404 @Override
405 public Object get() throws Exception {
406 return 1;
407 }
408 }, new Function<Object, MaybeSource<Integer>>() {
409 @Override
410 public MaybeSource<Integer> apply(Object v) throws Exception {
411 return Maybe.wrap(new MaybeSource<Integer>() {
412 @Override
413 public void subscribe(MaybeObserver<? super Integer> 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 TestObserver<Integer> to = Maybe.using(new Supplier<Object>() {
447 @Override
448 public Object get() throws Exception {
449 return 1;
450 }
451 }, new Function<Object, MaybeSource<Integer>>() {
452 @Override
453 public MaybeSource<Integer> apply(Object v) throws Exception {
454 return ps.lastElement();
455 }
456 }, new Consumer<Object>() {
457 @Override
458 public void accept(Object d) throws Exception {
459 }
460 }, true)
461 .test();
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 @SuppressUndeliverable
485 public void errorDisposeRace() {
486 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
487
488 final PublishSubject<Integer> ps = PublishSubject.create();
489
490 final TestObserver<Integer> to = Maybe.using(new Supplier<Object>() {
491 @Override
492 public Object get() throws Exception {
493 return 1;
494 }
495 }, new Function<Object, MaybeSource<Integer>>() {
496 @Override
497 public MaybeSource<Integer> apply(Object v) throws Exception {
498 return ps.firstElement();
499 }
500 }, new Consumer<Object>() {
501 @Override
502 public void accept(Object d) throws Exception {
503 }
504 }, true)
505 .test();
506
507 final TestException ex = new TestException();
508
509 Runnable r1 = new Runnable() {
510 @Override
511 public void run() {
512 to.dispose();
513 }
514 };
515
516 Runnable r2 = new Runnable() {
517 @Override
518 public void run() {
519 ps.onError(ex);
520 }
521 };
522
523 TestHelper.race(r1, r2);
524 }
525 }
526
527 @Test
528 public void emptyDisposeRace() {
529 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
530
531 final PublishSubject<Integer> ps = PublishSubject.create();
532
533 final TestObserver<Integer> to = Maybe.using(new Supplier<Object>() {
534 @Override
535 public Object get() throws Exception {
536 return 1;
537 }
538 }, new Function<Object, MaybeSource<Integer>>() {
539 @Override
540 public MaybeSource<Integer> apply(Object v) throws Exception {
541 return ps.firstElement();
542 }
543 }, new Consumer<Object>() {
544 @Override
545 public void accept(Object d) throws Exception {
546
547 }
548 }, true)
549 .test();
550
551 Runnable r1 = new Runnable() {
552 @Override
553 public void run() {
554 to.dispose();
555 }
556 };
557
558 Runnable r2 = new Runnable() {
559 @Override
560 public void run() {
561 ps.onComplete();
562 }
563 };
564
565 TestHelper.race(r1, r2);
566 }
567 }
568
569 @Test
570 public void eagerDisposeResourceThenDisposeUpstream() {
571 final StringBuilder sb = new StringBuilder();
572
573 TestObserver<Integer> to = Maybe.using(Functions.justSupplier(1),
574 new Function<Integer, Maybe<Integer>>() {
575 @Override
576 public Maybe<Integer> apply(Integer t) throws Throwable {
577 return Maybe.<Integer>never()
578 .doOnDispose(new Action() {
579 @Override
580 public void run() throws Throwable {
581 sb.append("Dispose");
582 }
583 })
584 ;
585 }
586 }, new Consumer<Integer>() {
587 @Override
588 public void accept(Integer t) throws Throwable {
589 sb.append("Resource");
590 }
591 }, true)
592 .test()
593 ;
594 to.assertEmpty();
595
596 to.dispose();
597
598 assertEquals("ResourceDispose", sb.toString());
599 }
600
601 @Test
602 public void nonEagerDisposeUpstreamThenDisposeResource() {
603 final StringBuilder sb = new StringBuilder();
604
605 TestObserver<Integer> to = Maybe.using(Functions.justSupplier(1),
606 new Function<Integer, Maybe<Integer>>() {
607 @Override
608 public Maybe<Integer> apply(Integer t) throws Throwable {
609 return Maybe.<Integer>never()
610 .doOnDispose(new Action() {
611 @Override
612 public void run() throws Throwable {
613 sb.append("Dispose");
614 }
615 })
616 ;
617 }
618 }, new Consumer<Integer>() {
619 @Override
620 public void accept(Integer t) throws Throwable {
621 sb.append("Resource");
622 }
623 }, false)
624 .test()
625 ;
626 to.assertEmpty();
627
628 to.dispose();
629
630 assertEquals("DisposeResource", sb.toString());
631 }
632 }