Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/ObservableScalarXMapTest.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.observable;
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.*;
23 import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
24 import io.reactivex.rxjava3.internal.operators.observable.ObservableScalarXMap.ScalarDisposable;
25 import io.reactivex.rxjava3.observers.TestObserver;
26 import io.reactivex.rxjava3.operators.QueueFuseable;
27 import io.reactivex.rxjava3.testsupport.TestHelper;
28
29 public class ObservableScalarXMapTest extends RxJavaTest {
30
31 @Test
32 public void utilityClass() {
33 TestHelper.checkUtilityClass(ObservableScalarXMap.class);
34 }
35
36 static final class CallablePublisher implements ObservableSource<Integer>, Supplier<Integer> {
37 @Override
38 public void subscribe(Observer<? super Integer> observer) {
39 EmptyDisposable.error(new TestException(), observer);
40 }
41
42 @Override
43 public Integer get() throws Exception {
44 throw new TestException();
45 }
46 }
47
48 static final class EmptyCallablePublisher implements ObservableSource<Integer>, Supplier<Integer> {
49 @Override
50 public void subscribe(Observer<? super Integer> observer) {
51 EmptyDisposable.complete(observer);
52 }
53
54 @Override
55 public Integer get() throws Exception {
56 return null;
57 }
58 }
59
60 static final class OneCallablePublisher implements ObservableSource<Integer>, Supplier<Integer> {
61 @Override
62 public void subscribe(Observer<? super Integer> observer) {
63 ScalarDisposable<Integer> sd = new ScalarDisposable<>(observer, 1);
64 observer.onSubscribe(sd);
65 sd.run();
66 }
67
68 @Override
69 public Integer get() throws Exception {
70 return 1;
71 }
72 }
73
74 @Test
75 public void tryScalarXMap() {
76 TestObserver<Integer> to = new TestObserver<>();
77 assertTrue(ObservableScalarXMap.tryScalarXMapSubscribe(new CallablePublisher(), to, new Function<Integer, ObservableSource<Integer>>() {
78 @Override
79 public ObservableSource<Integer> apply(Integer f) throws Exception {
80 return Observable.just(1);
81 }
82 }));
83
84 to.assertFailure(TestException.class);
85 }
86
87 @Test
88 public void emptyXMap() {
89 TestObserver<Integer> to = new TestObserver<>();
90
91 assertTrue(ObservableScalarXMap.tryScalarXMapSubscribe(new EmptyCallablePublisher(), to, new Function<Integer, ObservableSource<Integer>>() {
92 @Override
93 public ObservableSource<Integer> apply(Integer f) throws Exception {
94 return Observable.just(1);
95 }
96 }));
97
98 to.assertResult();
99 }
100
101 @Test
102 public void mapperCrashes() {
103 TestObserver<Integer> to = new TestObserver<>();
104
105 assertTrue(ObservableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), to, new Function<Integer, ObservableSource<Integer>>() {
106 @Override
107 public ObservableSource<Integer> apply(Integer f) throws Exception {
108 throw new TestException();
109 }
110 }));
111
112 to.assertFailure(TestException.class);
113 }
114
115 @Test
116 public void mapperToJust() {
117 TestObserver<Integer> to = new TestObserver<>();
118
119 assertTrue(ObservableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), to, new Function<Integer, ObservableSource<Integer>>() {
120 @Override
121 public ObservableSource<Integer> apply(Integer f) throws Exception {
122 return Observable.just(1);
123 }
124 }));
125
126 to.assertResult(1);
127 }
128
129 @Test
130 public void mapperToEmpty() {
131 TestObserver<Integer> to = new TestObserver<>();
132
133 assertTrue(ObservableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), to, new Function<Integer, ObservableSource<Integer>>() {
134 @Override
135 public ObservableSource<Integer> apply(Integer f) throws Exception {
136 return Observable.empty();
137 }
138 }));
139
140 to.assertResult();
141 }
142
143 @Test
144 public void mapperToCrashingCallable() {
145 TestObserver<Integer> to = new TestObserver<>();
146
147 assertTrue(ObservableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), to, new Function<Integer, ObservableSource<Integer>>() {
148 @Override
149 public ObservableSource<Integer> apply(Integer f) throws Exception {
150 return new CallablePublisher();
151 }
152 }));
153
154 to.assertFailure(TestException.class);
155 }
156
157 @Test
158 public void scalarMapToEmpty() {
159 ObservableScalarXMap.scalarXMap(1, new Function<Integer, ObservableSource<Integer>>() {
160 @Override
161 public ObservableSource<Integer> apply(Integer v) throws Exception {
162 return Observable.empty();
163 }
164 })
165 .test()
166 .assertResult();
167 }
168
169 @Test
170 public void scalarMapToCrashingCallable() {
171 ObservableScalarXMap.scalarXMap(1, new Function<Integer, ObservableSource<Integer>>() {
172 @Override
173 public ObservableSource<Integer> apply(Integer v) throws Exception {
174 return new CallablePublisher();
175 }
176 })
177 .test()
178 .assertFailure(TestException.class);
179 }
180
181 @Test
182 public void scalarDisposableStateCheck() {
183 TestObserver<Integer> to = new TestObserver<>();
184 ScalarDisposable<Integer> sd = new ScalarDisposable<>(to, 1);
185 to.onSubscribe(sd);
186
187 assertFalse(sd.isDisposed());
188
189 assertTrue(sd.isEmpty());
190
191 sd.run();
192
193 assertTrue(sd.isDisposed());
194
195 assertTrue(sd.isEmpty());
196
197 to.assertResult(1);
198
199 try {
200 sd.offer(1);
201 fail("Should have thrown");
202 } catch (UnsupportedOperationException ex) {
203 // expected
204 }
205
206 try {
207 sd.offer(1, 2);
208 fail("Should have thrown");
209 } catch (UnsupportedOperationException ex) {
210 // expected
211 }
212 }
213
214 @Test
215 public void scalarDisposableRunDisposeRace() {
216 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
217 TestObserver<Integer> to = new TestObserver<>();
218 final ScalarDisposable<Integer> sd = new ScalarDisposable<>(to, 1);
219 to.onSubscribe(sd);
220
221 Runnable r1 = new Runnable() {
222 @Override
223 public void run() {
224 sd.run();
225 }
226 };
227
228 Runnable r2 = new Runnable() {
229 @Override
230 public void run() {
231 sd.dispose();
232 }
233 };
234
235 TestHelper.race(r1, r2);
236 }
237 }
238
239 @Test
240 public void scalarDisposbleWrongFusion() {
241 TestObserver<Integer> to = new TestObserver<>();
242 final ScalarDisposable<Integer> sd = new ScalarDisposable<>(to, 1);
243 to.onSubscribe(sd);
244
245 assertEquals(QueueFuseable.NONE, sd.requestFusion(QueueFuseable.ASYNC));
246 }
247 }