Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableMapTest.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.flowable;
15
16 import static org.junit.Assert.assertNull;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.io.IOException;
21 import java.util.*;
22
23 import org.junit.*;
24 import org.reactivestreams.*;
25
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.exceptions.TestException;
28 import io.reactivex.rxjava3.functions.*;
29 import io.reactivex.rxjava3.internal.functions.Functions;
30 import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler;
31 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
32 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
33 import io.reactivex.rxjava3.operators.QueueFuseable;
34 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
35 import io.reactivex.rxjava3.processors.*;
36 import io.reactivex.rxjava3.schedulers.Schedulers;
37 import io.reactivex.rxjava3.subscribers.TestSubscriber;
38 import io.reactivex.rxjava3.testsupport.*;
39
40 public class FlowableMapTest extends RxJavaTest {
41
42 Subscriber<String> stringSubscriber;
43 Subscriber<String> stringSubscriber2;
44
45 static final BiFunction<String, Integer, String> APPEND_INDEX = new BiFunction<String, Integer, String>() {
46 @Override
47 public String apply(String value, Integer index) {
48 return value + index;
49 }
50 };
51
52 @Before
53 public void before() {
54 stringSubscriber = TestHelper.mockSubscriber();
55 stringSubscriber2 = TestHelper.mockSubscriber();
56 }
57
58 @Test
59 public void map() {
60 Map<String, String> m1 = getMap("One");
61 Map<String, String> m2 = getMap("Two");
62 Flowable<Map<String, String>> flowable = Flowable.just(m1, m2);
63
64 Flowable<String> m = flowable.map(new Function<Map<String, String>, String>() {
65 @Override
66 public String apply(Map<String, String> map) {
67 return map.get("firstName");
68 }
69 });
70
71 m.subscribe(stringSubscriber);
72
73 verify(stringSubscriber, never()).onError(any(Throwable.class));
74 verify(stringSubscriber, times(1)).onNext("OneFirst");
75 verify(stringSubscriber, times(1)).onNext("TwoFirst");
76 verify(stringSubscriber, times(1)).onComplete();
77 }
78
79 @Test
80 public void mapMany() {
81 /* simulate a top-level async call which returns IDs */
82 Flowable<Integer> ids = Flowable.just(1, 2);
83
84 /* now simulate the behavior to take those IDs and perform nested async calls based on them */
85 Flowable<String> m = ids.flatMap(new Function<Integer, Flowable<String>>() {
86
87 @Override
88 public Flowable<String> apply(Integer id) {
89 /* simulate making a nested async call which creates another Flowable */
90 Flowable<Map<String, String>> subFlowable = null;
91 if (id == 1) {
92 Map<String, String> m1 = getMap("One");
93 Map<String, String> m2 = getMap("Two");
94 subFlowable = Flowable.just(m1, m2);
95 } else {
96 Map<String, String> m3 = getMap("Three");
97 Map<String, String> m4 = getMap("Four");
98 subFlowable = Flowable.just(m3, m4);
99 }
100
101 /* simulate kicking off the async call and performing a select on it to transform the data */
102 return subFlowable.map(new Function<Map<String, String>, String>() {
103 @Override
104 public String apply(Map<String, String> map) {
105 return map.get("firstName");
106 }
107 });
108 }
109
110 });
111 m.subscribe(stringSubscriber);
112
113 verify(stringSubscriber, never()).onError(any(Throwable.class));
114 verify(stringSubscriber, times(1)).onNext("OneFirst");
115 verify(stringSubscriber, times(1)).onNext("TwoFirst");
116 verify(stringSubscriber, times(1)).onNext("ThreeFirst");
117 verify(stringSubscriber, times(1)).onNext("FourFirst");
118 verify(stringSubscriber, times(1)).onComplete();
119 }
120
121 @Test
122 public void mapMany2() {
123 Map<String, String> m1 = getMap("One");
124 Map<String, String> m2 = getMap("Two");
125 Flowable<Map<String, String>> flowable1 = Flowable.just(m1, m2);
126
127 Map<String, String> m3 = getMap("Three");
128 Map<String, String> m4 = getMap("Four");
129 Flowable<Map<String, String>> flowable2 = Flowable.just(m3, m4);
130
131 Flowable<Flowable<Map<String, String>>> f = Flowable.just(flowable1, flowable2);
132
133 Flowable<String> m = f.flatMap(new Function<Flowable<Map<String, String>>, Flowable<String>>() {
134
135 @Override
136 public Flowable<String> apply(Flowable<Map<String, String>> f) {
137 return f.map(new Function<Map<String, String>, String>() {
138
139 @Override
140 public String apply(Map<String, String> map) {
141 return map.get("firstName");
142 }
143 });
144 }
145
146 });
147 m.subscribe(stringSubscriber);
148
149 verify(stringSubscriber, never()).onError(any(Throwable.class));
150 verify(stringSubscriber, times(1)).onNext("OneFirst");
151 verify(stringSubscriber, times(1)).onNext("TwoFirst");
152 verify(stringSubscriber, times(1)).onNext("ThreeFirst");
153 verify(stringSubscriber, times(1)).onNext("FourFirst");
154 verify(stringSubscriber, times(1)).onComplete();
155
156 }
157
158 @Test
159 public void mapWithError() {
160 final List<Throwable> errors = new ArrayList<>();
161
162 Flowable<String> w = Flowable.just("one", "fail", "two", "three", "fail");
163 Flowable<String> m = w.map(new Function<String, String>() {
164 @Override
165 public String apply(String s) {
166 if ("fail".equals(s)) {
167 throw new TestException("Forced Failure");
168 }
169 return s;
170 }
171 }).doOnError(new Consumer<Throwable>() {
172
173 @Override
174 public void accept(Throwable t1) {
175 errors.add(t1);
176 }
177
178 });
179
180 m.subscribe(stringSubscriber);
181 verify(stringSubscriber, times(1)).onNext("one");
182 verify(stringSubscriber, never()).onNext("two");
183 verify(stringSubscriber, never()).onNext("three");
184 verify(stringSubscriber, never()).onComplete();
185 verify(stringSubscriber, times(1)).onError(any(TestException.class));
186
187 TestHelper.assertError(errors, 0, TestException.class, "Forced Failure");
188 }
189
190 @Test(expected = IllegalArgumentException.class)
191 public void mapWithIssue417() {
192 Flowable.just(1).observeOn(Schedulers.computation())
193 .map(new Function<Integer, Integer>() {
194 @Override
195 public Integer apply(Integer arg0) {
196 throw new IllegalArgumentException("any error");
197 }
198 }).blockingSingle();
199 }
200
201 @Test(expected = IllegalArgumentException.class)
202 public void mapWithErrorInFuncAndThreadPoolScheduler() throws InterruptedException {
203 // The error will throw in one of threads in the thread pool.
204 // If map does not handle it, the error will disappear.
205 // so map needs to handle the error by itself.
206 Flowable<String> m = Flowable.just("one")
207 .observeOn(Schedulers.computation())
208 .map(new Function<String, String>() {
209 @Override
210 public String apply(String arg0) {
211 throw new IllegalArgumentException("any error");
212 }
213 });
214
215 // block for response, expecting exception thrown
216 m.blockingLast();
217 }
218
219 /**
220 * While mapping over range(1,0).last() we expect NoSuchElementException since the sequence is empty.
221 */
222 @Test
223 public void errorPassesThruMap() {
224 assertNull(Flowable.range(1, 0).lastElement().map(new Function<Integer, Integer>() {
225
226 @Override
227 public Integer apply(Integer i) {
228 return i;
229 }
230
231 }).blockingGet());
232 }
233
234 /**
235 * We expect IllegalStateException to pass thru map.
236 */
237 @Test(expected = IllegalStateException.class)
238 public void errorPassesThruMap2() {
239 Flowable.error(new IllegalStateException()).map(new Function<Object, Object>() {
240
241 @Override
242 public Object apply(Object i) {
243 return i;
244 }
245
246 }).blockingSingle();
247 }
248
249 /**
250 * We expect an ArithmeticException exception here because last() emits a single value
251 * but then we divide by 0.
252 */
253 @Test(expected = ArithmeticException.class)
254 public void mapWithErrorInFunc() {
255 Flowable.range(1, 1).lastElement().map(new Function<Integer, Integer>() {
256
257 @Override
258 public Integer apply(Integer i) {
259 return i / 0;
260 }
261
262 }).blockingGet();
263 }
264
265 private static Map<String, String> getMap(String prefix) {
266 Map<String, String> m = new HashMap<>();
267 m.put("firstName", prefix + "First");
268 m.put("lastName", prefix + "Last");
269 return m;
270 }
271
272 @Test
273 public void functionCrashUnsubscribes() {
274
275 PublishProcessor<Integer> pp = PublishProcessor.create();
276
277 TestSubscriber<Integer> ts = new TestSubscriber<>();
278
279 pp.map(new Function<Integer, Integer>() {
280 @Override
281 public Integer apply(Integer v) {
282 throw new TestException();
283 }
284 }).subscribe(ts);
285
286 Assert.assertTrue("Not subscribed?", pp.hasSubscribers());
287
288 pp.onNext(1);
289
290 Assert.assertFalse("Subscribed?", pp.hasSubscribers());
291
292 ts.assertError(TestException.class);
293 }
294
295 @Test
296 public void mapFilter() {
297 Flowable.range(1, 2)
298 .map(new Function<Integer, Integer>() {
299 @Override
300 public Integer apply(Integer v) throws Exception {
301 return v + 1;
302 }
303 })
304 .filter(new Predicate<Integer>() {
305 @Override
306 public boolean test(Integer v) throws Exception {
307 return true;
308 }
309 })
310 .test()
311 .assertResult(2, 3);
312 }
313
314 @Test
315 public void mapFilterMapperCrash() {
316 Flowable.range(1, 2)
317 .map(new Function<Integer, Integer>() {
318 @Override
319 public Integer apply(Integer v) throws Exception {
320 throw new TestException();
321 }
322 })
323 .filter(new Predicate<Integer>() {
324 @Override
325 public boolean test(Integer v) throws Exception {
326 return true;
327 }
328 })
329 .test()
330 .assertFailure(TestException.class);
331 }
332
333 @Test
334 public void mapFilterHidden() {
335 Flowable.range(1, 2).hide()
336 .map(new Function<Integer, Integer>() {
337 @Override
338 public Integer apply(Integer v) throws Exception {
339 return v + 1;
340 }
341 })
342 .filter(new Predicate<Integer>() {
343 @Override
344 public boolean test(Integer v) throws Exception {
345 return true;
346 }
347 })
348 .test()
349 .assertResult(2, 3);
350 }
351
352 @Test
353 public void mapFilterFused() {
354 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
355
356 Flowable.range(1, 2)
357 .map(new Function<Integer, Integer>() {
358 @Override
359 public Integer apply(Integer v) throws Exception {
360 return v + 1;
361 }
362 })
363 .filter(new Predicate<Integer>() {
364 @Override
365 public boolean test(Integer v) throws Exception {
366 return true;
367 }
368 })
369 .subscribe(ts);
370
371 ts.assertFuseable()
372 .assertFusionMode(QueueFuseable.SYNC)
373 .assertResult(2, 3);
374 }
375
376 @Test
377 public void mapFilterFusedHidden() {
378 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
379
380 Flowable.range(1, 2).hide()
381 .map(new Function<Integer, Integer>() {
382 @Override
383 public Integer apply(Integer v) throws Exception {
384 return v + 1;
385 }
386 })
387 .filter(new Predicate<Integer>() {
388 @Override
389 public boolean test(Integer v) throws Exception {
390 return true;
391 }
392 })
393 .subscribe(ts);
394
395 ts.assertFuseable()
396 .assertFusionMode(QueueFuseable.NONE)
397 .assertResult(2, 3);
398 }
399
400 @Test
401 public void sourceIgnoresCancel() {
402 List<Throwable> errors = TestHelper.trackPluginErrors();
403
404 try {
405 Flowable.fromPublisher(new Publisher<Integer>() {
406 @Override
407 public void subscribe(Subscriber<? super Integer> s) {
408 s.onSubscribe(new BooleanSubscription());
409 s.onNext(1);
410 s.onNext(2);
411 s.onError(new IOException());
412 s.onComplete();
413 }
414 })
415 .map(new Function<Integer, Object>() {
416 @Override
417 public Object apply(Integer v) throws Exception {
418 throw new TestException();
419 }
420 })
421 .test()
422 .assertFailure(TestException.class);
423
424 TestHelper.assertUndeliverable(errors, 0, IOException.class);
425 } finally {
426 RxJavaPlugins.reset();
427 }
428 }
429
430 @Test
431 public void mapFilterMapperCrashFused() {
432 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
433
434 Flowable.range(1, 2).hide()
435 .map(new Function<Integer, Integer>() {
436 @Override
437 public Integer apply(Integer v) throws Exception {
438 throw new TestException();
439 }
440 })
441 .filter(new Predicate<Integer>() {
442 @Override
443 public boolean test(Integer v) throws Exception {
444 return true;
445 }
446 })
447 .subscribe(ts);
448
449 ts.assertFuseable()
450 .assertFusionMode(QueueFuseable.NONE)
451 .assertFailure(TestException.class);
452 }
453
454 @Test
455 public void sourceIgnoresCancelFilter() {
456 List<Throwable> errors = TestHelper.trackPluginErrors();
457
458 try {
459 Flowable.fromPublisher(new Publisher<Integer>() {
460 @Override
461 public void subscribe(Subscriber<? super Integer> s) {
462 s.onSubscribe(new BooleanSubscription());
463 s.onNext(1);
464 s.onNext(2);
465 s.onError(new IOException());
466 s.onComplete();
467 }
468 })
469 .map(new Function<Integer, Integer>() {
470 @Override
471 public Integer apply(Integer v) throws Exception {
472 throw new TestException();
473 }
474 })
475 .filter(new Predicate<Integer>() {
476 @Override
477 public boolean test(Integer v) throws Exception {
478 return true;
479 }
480 })
481 .test()
482 .assertFailure(TestException.class);
483
484 TestHelper.assertUndeliverable(errors, 0, IOException.class);
485 } finally {
486 RxJavaPlugins.reset();
487 }
488 }
489
490 @Test
491 public void mapFilterFused2() {
492 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
493
494 UnicastProcessor<Integer> up = UnicastProcessor.create();
495
496 up
497 .map(new Function<Integer, Integer>() {
498 @Override
499 public Integer apply(Integer v) throws Exception {
500 return v + 1;
501 }
502 })
503 .filter(new Predicate<Integer>() {
504 @Override
505 public boolean test(Integer v) throws Exception {
506 return true;
507 }
508 })
509 .subscribe(ts);
510
511 up.onNext(1);
512 up.onNext(2);
513 up.onComplete();
514
515 ts.assertFuseable()
516 .assertFusionMode(QueueFuseable.ASYNC)
517 .assertResult(2, 3);
518 }
519
520 @Test
521 public void sourceIgnoresCancelConditional() {
522 List<Throwable> errors = TestHelper.trackPluginErrors();
523
524 try {
525 Flowable.fromPublisher(new Publisher<Integer>() {
526 @Override
527 public void subscribe(Subscriber<? super Integer> s) {
528 ConditionalSubscriber<? super Integer> cs = (ConditionalSubscriber<? super Integer>)s;
529 cs.onSubscribe(new BooleanSubscription());
530 cs.tryOnNext(1);
531 cs.tryOnNext(2);
532 cs.onError(new IOException());
533 cs.onComplete();
534 }
535 })
536 .map(new Function<Integer, Integer>() {
537 @Override
538 public Integer apply(Integer v) throws Exception {
539 throw new TestException();
540 }
541 })
542 .filter(new Predicate<Integer>() {
543 @Override
544 public boolean test(Integer v) throws Exception {
545 return true;
546 }
547 })
548 .test()
549 .assertFailure(TestException.class);
550
551 TestHelper.assertUndeliverable(errors, 0, IOException.class);
552 } finally {
553 RxJavaPlugins.reset();
554 }
555 }
556
557 @Test
558 public void dispose() {
559 TestHelper.checkDisposed(Flowable.range(1, 5).map(Functions.identity()));
560 }
561
562 @Test
563 public void doubleOnSubscribe() {
564 TestHelper.checkDoubleOnSubscribeFlowable(new Function<Flowable<Object>, Flowable<Object>>() {
565 @Override
566 public Flowable<Object> apply(Flowable<Object> f) throws Exception {
567 return f.map(Functions.identity());
568 }
569 });
570 }
571
572 @Test
573 public void fusedSync() {
574 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
575
576 Flowable.range(1, 5)
577 .map(Functions.<Integer>identity())
578 .subscribe(ts);
579
580 ts.assertFusionMode(QueueFuseable.SYNC)
581 .assertResult(1, 2, 3, 4, 5);
582 }
583
584 @Test
585 public void fusedAsync() {
586 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY);
587
588 UnicastProcessor<Integer> up = UnicastProcessor.create();
589
590 up
591 .map(Functions.<Integer>identity())
592 .subscribe(ts);
593
594 TestHelper.emit(up, 1, 2, 3, 4, 5);
595
596 ts.assertFusionMode(QueueFuseable.ASYNC)
597 .assertResult(1, 2, 3, 4, 5);
598 }
599
600 @Test
601 public void fusedReject() {
602 TestSubscriberEx<Integer> ts = new TestSubscriberEx<Integer>().setInitialFusionMode(QueueFuseable.ANY | QueueFuseable.BOUNDARY);
603
604 Flowable.range(1, 5)
605 .map(Functions.<Integer>identity())
606 .subscribe(ts);
607
608 ts.assertFusionMode(QueueFuseable.NONE)
609 .assertResult(1, 2, 3, 4, 5);
610 }
611
612 @Test
613 public void badSource() {
614 TestHelper.checkBadSourceFlowable(new Function<Flowable<Object>, Object>() {
615 @Override
616 public Object apply(Flowable<Object> f) throws Exception {
617 return f.map(Functions.identity());
618 }
619 }, false, 1, 1, 1);
620 }
621
622 @Test
623 public void conditionalFusionNoNPE() {
624 TestSubscriberEx<Object> ts = new TestSubscriberEx<>()
625 .setInitialFusionMode(QueueFuseable.ANY);
626
627 Flowable.empty()
628 .observeOn(ImmediateThinScheduler.INSTANCE)
629 .filter(v -> true)
630 .map(v -> v)
631 .filter(v -> true)
632 .subscribe(ts)
633 ;
634
635 ts.assertResult();
636 }
637 }