Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/FlowableMapOptionalTest.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.jdk8;
15
16 import static org.junit.Assert.assertFalse;
17
18 import java.util.Optional;
19
20 import org.junit.Test;
21 import org.reactivestreams.Subscriber;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.exceptions.TestException;
25 import io.reactivex.rxjava3.functions.Function;
26 import io.reactivex.rxjava3.internal.schedulers.ImmediateThinScheduler;
27 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
28 import io.reactivex.rxjava3.operators.QueueFuseable;
29 import io.reactivex.rxjava3.processors.*;
30 import io.reactivex.rxjava3.testsupport.*;
31
32 public class FlowableMapOptionalTest extends RxJavaTest {
33
34 static final Function<? super Integer, Optional<? extends Integer>> MODULO = v -> v % 2 == 0 ? Optional.of(v) : Optional.<Integer>empty();
35
36 @Test
37 public void allPresent() {
38 Flowable.range(1, 5)
39 .mapOptional(Optional::of)
40 .test()
41 .assertResult(1, 2, 3, 4, 5);
42 }
43
44 @Test
45 public void allEmpty() {
46 Flowable.range(1, 5)
47 .mapOptional(v -> Optional.<Integer>empty())
48 .test()
49 .assertResult();
50 }
51
52 @Test
53 public void mixed() {
54 Flowable.range(1, 10)
55 .mapOptional(MODULO)
56 .test()
57 .assertResult(2, 4, 6, 8, 10);
58 }
59
60 @Test
61 public void mapperChash() {
62 BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
63
64 source
65 .mapOptional(v -> { throw new TestException(); })
66 .test()
67 .assertFailure(TestException.class);
68
69 assertFalse(source.hasSubscribers());
70 }
71
72 @Test
73 public void mapperNull() {
74 BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
75
76 source
77 .mapOptional(v -> null)
78 .test()
79 .assertFailure(NullPointerException.class);
80
81 assertFalse(source.hasSubscribers());
82 }
83
84 @Test
85 public void crashDropsOnNexts() {
86 Flowable<Integer> source = new Flowable<Integer>() {
87 @Override
88 protected void subscribeActual(Subscriber<? super Integer> s) {
89 s.onSubscribe(new BooleanSubscription());
90 s.onNext(1);
91 s.onNext(2);
92 }
93 };
94
95 source
96 .mapOptional(v -> { throw new TestException(); })
97 .test()
98 .assertFailure(TestException.class);
99 }
100
101 @Test
102 public void backpressureAll() {
103 Flowable.range(1, 5)
104 .mapOptional(Optional::of)
105 .test(0L)
106 .assertEmpty()
107 .requestMore(2)
108 .assertValuesOnly(1, 2)
109 .requestMore(2)
110 .assertValuesOnly(1, 2, 3, 4)
111 .requestMore(1)
112 .assertResult(1, 2, 3, 4, 5);
113 }
114
115 @Test
116 public void backpressureNone() {
117 Flowable.range(1, 5)
118 .mapOptional(v -> Optional.empty())
119 .test(1L)
120 .assertResult();
121 }
122
123 @Test
124 public void backpressureMixed() {
125 Flowable.range(1, 10)
126 .mapOptional(MODULO)
127 .test(0L)
128 .assertEmpty()
129 .requestMore(2)
130 .assertValuesOnly(2, 4)
131 .requestMore(2)
132 .assertValuesOnly(2, 4, 6, 8)
133 .requestMore(1)
134 .assertResult(2, 4, 6, 8, 10);
135 }
136
137 @Test
138 public void syncFusedAll() {
139 Flowable.range(1, 5)
140 .mapOptional(Optional::of)
141 .to(TestHelper.testConsumer(false, QueueFuseable.SYNC))
142 .assertFuseable()
143 .assertFusionMode(QueueFuseable.SYNC)
144 .assertResult(1, 2, 3, 4, 5);
145 }
146
147 @Test
148 public void asyncFusedAll() {
149 UnicastProcessor<Integer> up = UnicastProcessor.create();
150 TestHelper.emit(up, 1, 2, 3, 4, 5);
151
152 up
153 .mapOptional(Optional::of)
154 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC))
155 .assertFuseable()
156 .assertFusionMode(QueueFuseable.ASYNC)
157 .assertResult(1, 2, 3, 4, 5);
158 }
159
160 @Test
161 public void boundaryFusedAll() {
162 UnicastProcessor<Integer> up = UnicastProcessor.create();
163 TestHelper.emit(up, 1, 2, 3, 4, 5);
164
165 up
166 .mapOptional(Optional::of)
167 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC | QueueFuseable.BOUNDARY))
168 .assertFuseable()
169 .assertFusionMode(QueueFuseable.NONE)
170 .assertResult(1, 2, 3, 4, 5);
171 }
172
173 @Test
174 public void syncFusedNone() {
175 Flowable.range(1, 5)
176 .mapOptional(v -> Optional.empty())
177 .to(TestHelper.testConsumer(false, QueueFuseable.SYNC))
178 .assertFuseable()
179 .assertFusionMode(QueueFuseable.SYNC)
180 .assertResult();
181 }
182
183 @Test
184 public void asyncFusedNone() {
185 UnicastProcessor<Integer> up = UnicastProcessor.create();
186 TestHelper.emit(up, 1, 2, 3, 4, 5);
187
188 up
189 .mapOptional(v -> Optional.empty())
190 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC))
191 .assertFuseable()
192 .assertFusionMode(QueueFuseable.ASYNC)
193 .assertResult();
194 }
195
196 @Test
197 public void boundaryFusedNone() {
198 UnicastProcessor<Integer> up = UnicastProcessor.create();
199 TestHelper.emit(up, 1, 2, 3, 4, 5);
200
201 up
202 .mapOptional(v -> Optional.empty())
203 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC | QueueFuseable.BOUNDARY))
204 .assertFuseable()
205 .assertFusionMode(QueueFuseable.NONE)
206 .assertResult();
207 }
208
209 @Test
210 public void syncFusedMixed() {
211 Flowable.range(1, 10)
212 .mapOptional(MODULO)
213 .to(TestHelper.testConsumer(false, QueueFuseable.SYNC))
214 .assertFuseable()
215 .assertFusionMode(QueueFuseable.SYNC)
216 .assertResult(2, 4, 6, 8, 10);
217 }
218
219 @Test
220 public void asyncFusedMixed() {
221 UnicastProcessor<Integer> up = UnicastProcessor.create();
222 TestHelper.emit(up, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
223
224 up
225 .mapOptional(MODULO)
226 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC))
227 .assertFuseable()
228 .assertFusionMode(QueueFuseable.ASYNC)
229 .assertResult(2, 4, 6, 8, 10);
230 }
231
232 @Test
233 public void boundaryFusedMixed() {
234 UnicastProcessor<Integer> up = UnicastProcessor.create();
235 TestHelper.emit(up, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
236
237 up
238 .mapOptional(MODULO)
239 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC | QueueFuseable.BOUNDARY))
240 .assertFuseable()
241 .assertFusionMode(QueueFuseable.NONE)
242 .assertResult(2, 4, 6, 8, 10);
243 }
244
245 @Test
246 public void allPresentConditional() {
247 Flowable.range(1, 5)
248 .mapOptional(Optional::of)
249 .filter(v -> true)
250 .test()
251 .assertResult(1, 2, 3, 4, 5);
252 }
253
254 @Test
255 public void allEmptyConditional() {
256 Flowable.range(1, 5)
257 .mapOptional(v -> Optional.<Integer>empty())
258 .filter(v -> true)
259 .test()
260 .assertResult();
261 }
262
263 @Test
264 public void mixedConditional() {
265 Flowable.range(1, 10)
266 .mapOptional(MODULO)
267 .filter(v -> true)
268 .test()
269 .assertResult(2, 4, 6, 8, 10);
270 }
271
272 @Test
273 public void mapperChashConditional() {
274 BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
275
276 source
277 .mapOptional(v -> { throw new TestException(); })
278 .filter(v -> true)
279 .test()
280 .assertFailure(TestException.class);
281
282 assertFalse(source.hasSubscribers());
283 }
284
285 @Test
286 public void mapperNullConditional() {
287 BehaviorProcessor<Integer> source = BehaviorProcessor.createDefault(1);
288
289 source
290 .mapOptional(v -> null)
291 .filter(v -> true)
292 .test()
293 .assertFailure(NullPointerException.class);
294
295 assertFalse(source.hasSubscribers());
296 }
297
298 @Test
299 public void crashDropsOnNextsConditional() {
300 Flowable<Integer> source = new Flowable<Integer>() {
301 @Override
302 protected void subscribeActual(Subscriber<? super Integer> s) {
303 s.onSubscribe(new BooleanSubscription());
304 s.onNext(1);
305 s.onNext(2);
306 }
307 };
308
309 source
310 .mapOptional(v -> { throw new TestException(); })
311 .filter(v -> true)
312 .test()
313 .assertFailure(TestException.class);
314 }
315
316 @Test
317 public void backpressureAllConditional() {
318 Flowable.range(1, 5)
319 .mapOptional(Optional::of)
320 .filter(v -> true)
321 .test(0L)
322 .assertEmpty()
323 .requestMore(2)
324 .assertValuesOnly(1, 2)
325 .requestMore(2)
326 .assertValuesOnly(1, 2, 3, 4)
327 .requestMore(1)
328 .assertResult(1, 2, 3, 4, 5);
329 }
330
331 @Test
332 public void backpressureNoneConditional() {
333 Flowable.range(1, 5)
334 .mapOptional(v -> Optional.empty())
335 .filter(v -> true)
336 .test(1L)
337 .assertResult();
338 }
339
340 @Test
341 public void backpressureMixedConditional() {
342 Flowable.range(1, 10)
343 .mapOptional(MODULO)
344 .filter(v -> true)
345 .test(0L)
346 .assertEmpty()
347 .requestMore(2)
348 .assertValuesOnly(2, 4)
349 .requestMore(2)
350 .assertValuesOnly(2, 4, 6, 8)
351 .requestMore(1)
352 .assertResult(2, 4, 6, 8, 10);
353 }
354
355 @Test
356 public void syncFusedAllConditional() {
357 Flowable.range(1, 5)
358 .mapOptional(Optional::of)
359 .filter(v -> true)
360 .to(TestHelper.testConsumer(false, QueueFuseable.SYNC))
361 .assertFuseable()
362 .assertFusionMode(QueueFuseable.SYNC)
363 .assertResult(1, 2, 3, 4, 5);
364 }
365
366 @Test
367 public void asyncFusedAllConditional() {
368 UnicastProcessor<Integer> up = UnicastProcessor.create();
369 TestHelper.emit(up, 1, 2, 3, 4, 5);
370
371 up
372 .mapOptional(Optional::of)
373 .filter(v -> true)
374 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC))
375 .assertFuseable()
376 .assertFusionMode(QueueFuseable.ASYNC)
377 .assertResult(1, 2, 3, 4, 5);
378 }
379
380 @Test
381 public void boundaryFusedAllConditiona() {
382 UnicastProcessor<Integer> up = UnicastProcessor.create();
383 TestHelper.emit(up, 1, 2, 3, 4, 5);
384
385 up
386 .mapOptional(Optional::of)
387 .filter(v -> true)
388 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC | QueueFuseable.BOUNDARY))
389 .assertFuseable()
390 .assertFusionMode(QueueFuseable.NONE)
391 .assertResult(1, 2, 3, 4, 5);
392 }
393
394 @Test
395 public void syncFusedNoneConditional() {
396 Flowable.range(1, 5)
397 .mapOptional(v -> Optional.empty())
398 .filter(v -> true)
399 .to(TestHelper.testConsumer(false, QueueFuseable.SYNC))
400 .assertFuseable()
401 .assertFusionMode(QueueFuseable.SYNC)
402 .assertResult();
403 }
404
405 @Test
406 public void asyncFusedNoneConditional() {
407 UnicastProcessor<Integer> up = UnicastProcessor.create();
408 TestHelper.emit(up, 1, 2, 3, 4, 5);
409
410 up
411 .mapOptional(v -> Optional.empty())
412 .filter(v -> true)
413 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC))
414 .assertFuseable()
415 .assertFusionMode(QueueFuseable.ASYNC)
416 .assertResult();
417 }
418
419 @Test
420 public void boundaryFusedNoneConditional() {
421 UnicastProcessor<Integer> up = UnicastProcessor.create();
422 TestHelper.emit(up, 1, 2, 3, 4, 5);
423
424 up
425 .mapOptional(v -> Optional.empty())
426 .filter(v -> true)
427 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC | QueueFuseable.BOUNDARY))
428 .assertFuseable()
429 .assertFusionMode(QueueFuseable.NONE)
430 .assertResult();
431 }
432
433 @Test
434 public void syncFusedMixedConditional() {
435 Flowable.range(1, 10)
436 .mapOptional(MODULO)
437 .filter(v -> true)
438 .to(TestHelper.testConsumer(false, QueueFuseable.SYNC))
439 .assertFuseable()
440 .assertFusionMode(QueueFuseable.SYNC)
441 .assertResult(2, 4, 6, 8, 10);
442 }
443
444 @Test
445 public void asyncFusedMixedConditional() {
446 UnicastProcessor<Integer> up = UnicastProcessor.create();
447 TestHelper.emit(up, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
448
449 up
450 .mapOptional(MODULO)
451 .filter(v -> true)
452 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC))
453 .assertFuseable()
454 .assertFusionMode(QueueFuseable.ASYNC)
455 .assertResult(2, 4, 6, 8, 10);
456 }
457
458 @Test
459 public void boundaryFusedMixedConditional() {
460 UnicastProcessor<Integer> up = UnicastProcessor.create();
461 TestHelper.emit(up, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
462
463 up
464 .mapOptional(MODULO)
465 .filter(v -> true)
466 .to(TestHelper.testConsumer(false, QueueFuseable.ASYNC | QueueFuseable.BOUNDARY))
467 .assertFuseable()
468 .assertFusionMode(QueueFuseable.NONE)
469 .assertResult(2, 4, 6, 8, 10);
470 }
471
472 @Test
473 public void conditionalFusionNoNPE() {
474 TestSubscriberEx<Object> ts = new TestSubscriberEx<>()
475 .setInitialFusionMode(QueueFuseable.ANY);
476
477 Flowable.empty()
478 .observeOn(ImmediateThinScheduler.INSTANCE)
479 .filter(v -> true)
480 .mapOptional(Optional::of)
481 .filter(v -> true)
482 .subscribe(ts)
483 ;
484
485 ts.assertResult();
486 }
487 }