Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/mixed/FlowableSwitchMapCompletableTest.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.mixed;
15
16 import static org.junit.Assert.*;
17
18 import java.util.List;
19
20 import org.junit.Test;
21 import org.reactivestreams.Subscriber;
22
23 import io.reactivex.rxjava3.core.*;
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.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.observers.TestObserver;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.PublishProcessor;
31 import io.reactivex.rxjava3.subjects.CompletableSubject;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class FlowableSwitchMapCompletableTest extends RxJavaTest {
35
36 @Test
37 public void normal() {
38 Flowable.range(1, 10)
39 .switchMapCompletable(new Function<Integer, CompletableSource>() {
40 @Override
41 public CompletableSource apply(Integer v) throws Exception {
42 return Completable.complete();
43 }
44 })
45 .test()
46 .assertResult();
47 }
48
49 @Test
50 public void mainError() {
51 Flowable.<Integer>error(new TestException())
52 .switchMapCompletable(new Function<Integer, CompletableSource>() {
53 @Override
54 public CompletableSource apply(Integer v) throws Exception {
55 return Completable.complete();
56 }
57 })
58 .test()
59 .assertFailure(TestException.class);
60 }
61
62 @Test
63 public void innerError() {
64 PublishProcessor<Integer> pp = PublishProcessor.create();
65 CompletableSubject cs = CompletableSubject.create();
66
67 TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs))
68 .test();
69
70 assertTrue(pp.hasSubscribers());
71 assertFalse(cs.hasObservers());
72
73 pp.onNext(1);
74
75 assertTrue(cs.hasObservers());
76
77 to.assertEmpty();
78
79 cs.onError(new TestException());
80
81 to.assertFailure(TestException.class);
82
83 assertFalse(pp.hasSubscribers());
84 assertFalse(cs.hasObservers());
85 }
86
87 @Test
88 public void switchOver() {
89 final CompletableSubject[] css = {
90 CompletableSubject.create(),
91 CompletableSubject.create()
92 };
93
94 PublishProcessor<Integer> pp = PublishProcessor.create();
95
96 TestObserver<Void> to = pp.switchMapCompletable(new Function<Integer, CompletableSource>() {
97 @Override
98 public CompletableSource apply(Integer v) throws Exception {
99 return css[v];
100 }
101 })
102 .test();
103
104 to.assertEmpty();
105
106 pp.onNext(0);
107
108 assertTrue(css[0].hasObservers());
109
110 pp.onNext(1);
111
112 assertFalse(css[0].hasObservers());
113 assertTrue(css[1].hasObservers());
114
115 pp.onComplete();
116
117 to.assertEmpty();
118
119 assertTrue(css[1].hasObservers());
120
121 css[1].onComplete();
122
123 to.assertResult();
124 }
125
126 @Test
127 public void dispose() {
128 PublishProcessor<Integer> pp = PublishProcessor.create();
129 CompletableSubject cs = CompletableSubject.create();
130
131 TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs))
132 .test();
133
134 pp.onNext(1);
135
136 assertTrue(pp.hasSubscribers());
137 assertTrue(cs.hasObservers());
138
139 to.dispose();
140
141 assertFalse(pp.hasSubscribers());
142 assertFalse(cs.hasObservers());
143 }
144
145 @Test
146 public void checkDisposed() {
147 PublishProcessor<Integer> pp = PublishProcessor.create();
148 CompletableSubject cs = CompletableSubject.create();
149
150 TestHelper.checkDisposed(pp.switchMapCompletable(Functions.justFunction(cs)));
151 }
152
153 @Test
154 public void checkBadSource() {
155 TestHelper.checkDoubleOnSubscribeFlowableToCompletable(new Function<Flowable<Object>, Completable>() {
156 @Override
157 public Completable apply(Flowable<Object> f) throws Exception {
158 return f.switchMapCompletable(Functions.justFunction(Completable.never()));
159 }
160 });
161 }
162
163 @Test
164 public void mapperCrash() {
165 Flowable.range(1, 5).switchMapCompletable(new Function<Integer, CompletableSource>() {
166 @Override
167 public CompletableSource apply(Integer f) throws Exception {
168 throw new TestException();
169 }
170 })
171 .test()
172 .assertFailure(TestException.class);
173 }
174
175 @Test
176 public void mapperCancels() {
177 final TestObserver<Void> to = new TestObserver<>();
178
179 Flowable.range(1, 5).switchMapCompletable(new Function<Integer, CompletableSource>() {
180 @Override
181 public CompletableSource apply(Integer f) throws Exception {
182 to.dispose();
183 return Completable.complete();
184 }
185 })
186 .subscribe(to);
187
188 to.assertEmpty();
189 }
190
191 @Test
192 public void onNextInnerCompleteRace() {
193 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
194 final PublishProcessor<Integer> pp = PublishProcessor.create();
195 final CompletableSubject cs = CompletableSubject.create();
196
197 TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs)).test();
198
199 pp.onNext(1);
200
201 Runnable r1 = new Runnable() {
202 @Override
203 public void run() {
204 pp.onNext(2);
205 }
206 };
207
208 Runnable r2 = new Runnable() {
209 @Override
210 public void run() {
211 cs.onComplete();
212 }
213 };
214
215 TestHelper.race(r1, r2);
216
217 to.assertEmpty();
218 }
219 }
220
221 @Test
222 public void onNextInnerErrorRace() {
223 final TestException ex = new TestException();
224 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
225 List<Throwable> errors = TestHelper.trackPluginErrors();
226 try {
227 final PublishProcessor<Integer> pp = PublishProcessor.create();
228 final CompletableSubject cs = CompletableSubject.create();
229
230 TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs)).test();
231
232 pp.onNext(1);
233
234 Runnable r1 = new Runnable() {
235 @Override
236 public void run() {
237 pp.onNext(2);
238 }
239 };
240
241 Runnable r2 = new Runnable() {
242 @Override
243 public void run() {
244 cs.onError(ex);
245 }
246 };
247
248 TestHelper.race(r1, r2);
249
250 to.assertError(new Predicate<Throwable>() {
251 @Override
252 public boolean test(Throwable e) throws Exception {
253 return e instanceof TestException || e instanceof CompositeException;
254 }
255 });
256
257 if (!errors.isEmpty()) {
258 TestHelper.assertUndeliverable(errors, 0, TestException.class);
259 }
260 } finally {
261 RxJavaPlugins.reset();
262 }
263 }
264 }
265
266 @Test
267 public void onErrorInnerErrorRace() {
268 final TestException ex0 = new TestException();
269 final TestException ex = new TestException();
270 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
271 List<Throwable> errors = TestHelper.trackPluginErrors();
272 try {
273 final PublishProcessor<Integer> pp = PublishProcessor.create();
274 final CompletableSubject cs = CompletableSubject.create();
275
276 TestObserver<Void> to = pp.switchMapCompletable(Functions.justFunction(cs)).test();
277
278 pp.onNext(1);
279
280 Runnable r1 = new Runnable() {
281 @Override
282 public void run() {
283 pp.onError(ex0);
284 }
285 };
286
287 Runnable r2 = new Runnable() {
288 @Override
289 public void run() {
290 cs.onError(ex);
291 }
292 };
293
294 TestHelper.race(r1, r2);
295
296 to.assertError(new Predicate<Throwable>() {
297 @Override
298 public boolean test(Throwable e) throws Exception {
299 return e instanceof TestException || e instanceof CompositeException;
300 }
301 });
302
303 if (!errors.isEmpty()) {
304 TestHelper.assertUndeliverable(errors, 0, TestException.class);
305 }
306 } finally {
307 RxJavaPlugins.reset();
308 }
309 }
310 }
311
312 @Test
313 public void innerErrorThenMainError() {
314 List<Throwable> errors = TestHelper.trackPluginErrors();
315 try {
316 new Flowable<Integer>() {
317 @Override
318 protected void subscribeActual(Subscriber<? super Integer> s) {
319 s.onSubscribe(new BooleanSubscription());
320 s.onNext(1);
321 s.onError(new TestException("main"));
322 }
323 }
324 .switchMapCompletable(Functions.justFunction(Completable.error(new TestException("inner"))))
325 .to(TestHelper.testConsumer())
326 .assertFailureAndMessage(TestException.class, "inner");
327
328 TestHelper.assertUndeliverable(errors, 0, TestException.class, "main");
329 } finally {
330 RxJavaPlugins.reset();
331 }
332 }
333
334 @Test
335 public void innerErrorDelayed() {
336 final PublishProcessor<Integer> pp = PublishProcessor.create();
337 final CompletableSubject cs = CompletableSubject.create();
338
339 TestObserver<Void> to = pp.switchMapCompletableDelayError(Functions.justFunction(cs)).test();
340
341 pp.onNext(1);
342
343 cs.onError(new TestException());
344
345 to.assertEmpty();
346
347 assertTrue(pp.hasSubscribers());
348
349 pp.onComplete();
350
351 to.assertFailure(TestException.class);
352 }
353
354 @Test
355 public void mainCompletesinnerErrorDelayed() {
356 final PublishProcessor<Integer> pp = PublishProcessor.create();
357 final CompletableSubject cs = CompletableSubject.create();
358
359 TestObserver<Void> to = pp.switchMapCompletableDelayError(Functions.justFunction(cs)).test();
360
361 pp.onNext(1);
362 pp.onComplete();
363
364 to.assertEmpty();
365
366 cs.onError(new TestException());
367
368 to.assertFailure(TestException.class);
369 }
370
371 @Test
372 public void mainErrorDelayed() {
373 final PublishProcessor<Integer> pp = PublishProcessor.create();
374 final CompletableSubject cs = CompletableSubject.create();
375
376 TestObserver<Void> to = pp.switchMapCompletableDelayError(Functions.justFunction(cs)).test();
377
378 pp.onNext(1);
379
380 pp.onError(new TestException());
381
382 to.assertEmpty();
383
384 assertTrue(cs.hasObservers());
385
386 cs.onComplete();
387
388 to.assertFailure(TestException.class);
389 }
390
391 @Test
392 public void undeliverableUponCancel() {
393 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Completable>() {
394 @Override
395 public Completable apply(Flowable<Integer> upstream) {
396 return upstream.switchMapCompletable(new Function<Integer, Completable>() {
397 @Override
398 public Completable apply(Integer v) throws Throwable {
399 return Completable.complete().hide();
400 }
401 });
402 }
403 });
404 }
405
406 @Test
407 public void undeliverableUponCancelDelayError() {
408 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Completable>() {
409 @Override
410 public Completable apply(Flowable<Integer> upstream) {
411 return upstream.switchMapCompletableDelayError(new Function<Integer, Completable>() {
412 @Override
413 public Completable apply(Integer v) throws Throwable {
414 return Completable.complete().hide();
415 }
416 });
417 }
418 });
419 }
420 }