Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeSwitchIfEmptySingleTest.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 org.junit.Test;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.TestException;
22 import io.reactivex.rxjava3.functions.Function;
23 import io.reactivex.rxjava3.internal.fuseable.HasUpstreamMaybeSource;
24 import io.reactivex.rxjava3.observers.TestObserver;
25 import io.reactivex.rxjava3.processors.PublishProcessor;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class MaybeSwitchIfEmptySingleTest extends RxJavaTest {
29
30 @Test
31 public void nonEmpty() {
32 Maybe.just(1).switchIfEmpty(Single.just(2)).test().assertResult(1);
33 }
34
35 @Test
36 public void empty() {
37 Maybe.<Integer>empty().switchIfEmpty(Single.just(2)).test().assertResult(2);
38 }
39
40 @Test
41 public void error() {
42 Maybe.<Integer>error(new TestException()).switchIfEmpty(Single.just(2))
43 .test().assertFailure(TestException.class);
44 }
45
46 @Test
47 public void errorOther() {
48 Maybe.empty().switchIfEmpty(Single.<Integer>error(new TestException()))
49 .test().assertFailure(TestException.class);
50 }
51
52 @Test
53 public void dispose() {
54 PublishProcessor<Integer> pp = PublishProcessor.create();
55
56 TestObserver<Integer> to = pp.singleElement().switchIfEmpty(Single.just(2)).test();
57
58 assertTrue(pp.hasSubscribers());
59
60 to.dispose();
61
62 assertFalse(pp.hasSubscribers());
63 }
64
65 @Test
66 public void isDisposed() {
67 PublishProcessor<Integer> pp = PublishProcessor.create();
68
69 TestHelper.checkDisposed(pp.singleElement().switchIfEmpty(Single.just(2)));
70 }
71
72 @Test
73 public void doubleOnSubscribe() {
74 TestHelper.checkDoubleOnSubscribeMaybeToSingle(new Function<Maybe<Integer>, Single<Integer>>() {
75 @Override
76 public Single<Integer> apply(Maybe<Integer> f) throws Exception {
77 return f.switchIfEmpty(Single.just(2));
78 }
79 });
80 }
81
82 @Test
83 public void emptyCancelRace() {
84 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
85 final PublishProcessor<Integer> pp = PublishProcessor.create();
86
87 final TestObserver<Integer> to = pp.singleElement().switchIfEmpty(Single.just(2)).test();
88
89 Runnable r1 = new Runnable() {
90 @Override
91 public void run() {
92 pp.onComplete();
93 }
94 };
95
96 Runnable r2 = new Runnable() {
97 @Override
98 public void run() {
99 to.dispose();
100 }
101 };
102
103 TestHelper.race(r1, r2);
104 }
105 }
106
107 @SuppressWarnings("rawtypes")
108 @Test
109 public void source() {
110 assertSame(Maybe.empty(),
111 ((HasUpstreamMaybeSource)(Maybe.<Integer>empty().switchIfEmpty(Single.just(1)))).source()
112 );
113 }
114 }