Debian Codegraph @

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