Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/FlowableFlatMapStreamTest.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.*;
17 import static org.mockito.Mockito.*;
18
19 import java.io.IOException;
20 import java.util.Iterator;
21 import java.util.concurrent.atomic.AtomicInteger;
22 import java.util.stream.*;
23
24 import org.junit.Test;
25 import org.reactivestreams.Subscriber;
26
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.exceptions.*;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.processors.*;
31 import io.reactivex.rxjava3.subscribers.TestSubscriber;
32 import io.reactivex.rxjava3.testsupport.TestHelper;
33
34 public class FlowableFlatMapStreamTest extends RxJavaTest {
35
36 @Test
37 public void empty() {
38 Flowable.empty()
39 .flatMapStream(v -> Stream.of(1, 2, 3, 4, 5))
40 .test()
41 .assertResult();
42 }
43
44 @Test
45 public void emptyHidden() {
46 Flowable.empty()
47 .hide()
48 .flatMapStream(v -> Stream.of(1, 2, 3, 4, 5))
49 .test()
50 .assertResult();
51 }
52
53 @Test
54 public void just() {
55 Flowable.just(1)
56 .flatMapStream(v -> Stream.of(v + 1, v + 2, v + 3, v + 4, v + 5))
57 .test()
58 .assertResult(2, 3, 4, 5, 6);
59 }
60
61 @Test
62 public void justHidden() {
63 Flowable.just(1).hide()
64 .flatMapStream(v -> Stream.of(v + 1, v + 2, v + 3, v + 4, v + 5))
65 .test()
66 .assertResult(2, 3, 4, 5, 6);
67 }
68
69 @Test
70 public void error() {
71 Flowable.error(new TestException())
72 .flatMapStream(v -> Stream.of(1, 2, 3, 4, 5))
73 .test()
74 .assertFailure(TestException.class);
75 }
76
77 @Test
78 public void supplierFusedError() {
79 Flowable.fromCallable(() -> { throw new TestException(); })
80 .flatMapStream(v -> Stream.of(1, 2, 3, 4, 5))
81 .test()
82 .assertFailure(TestException.class);
83 }
84
85 @Test
86 public void errorHidden() {
87 Flowable.error(new TestException())
88 .hide()
89 .flatMapStream(v -> Stream.of(1, 2, 3, 4, 5))
90 .test()
91 .assertFailure(TestException.class);
92 }
93
94 @Test
95 public void range() {
96 Flowable.range(1, 5)
97 .flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed())
98 .test()
99 .assertResult(
100 10, 11, 12, 13, 14,
101 20, 21, 22, 23, 24,
102 30, 31, 32, 33, 34,
103 40, 41, 42, 43, 44,
104 50, 51, 52, 53, 54
105 );
106 }
107
108 @Test
109 public void rangeHidden() {
110 Flowable.range(1, 5)
111 .hide()
112 .flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed())
113 .test()
114 .assertResult(
115 10, 11, 12, 13, 14,
116 20, 21, 22, 23, 24,
117 30, 31, 32, 33, 34,
118 40, 41, 42, 43, 44,
119 50, 51, 52, 53, 54
120 );
121 }
122
123 @Test
124 public void rangeToEmpty() {
125 Flowable.range(1, 5)
126 .flatMapStream(v -> Stream.of())
127 .test()
128 .assertResult();
129 }
130
131 @Test
132 public void rangeTake() {
133 Flowable.range(1, 5)
134 .flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed())
135 .take(12)
136 .test()
137 .assertResult(
138 10, 11, 12, 13, 14,
139 20, 21, 22, 23, 24,
140 30, 31
141 );
142 }
143
144 @Test
145 public void rangeTakeHidden() {
146 Flowable.range(1, 5)
147 .hide()
148 .flatMapStream(v -> IntStream.range(v * 10, v * 10 + 5).boxed())
149 .take(12)
150 .test()
151 .assertResult(
152 10, 11, 12, 13, 14,
153 20, 21, 22, 23, 24,
154 30, 31
155 );
156 }
157
158 @Test
159 public void upstreamCancelled() {
160 PublishProcessor<Integer> pp = PublishProcessor.create();
161
162 AtomicInteger calls = new AtomicInteger();
163
164 TestSubscriber<Integer> ts = pp
165 .flatMapStream(v -> Stream.of(v + 1, v + 2).onClose(() -> calls.getAndIncrement()))
166 .test(1);
167
168 assertTrue(pp.hasSubscribers());
169
170 pp.onNext(1);
171
172 ts.assertValuesOnly(2);
173 ts.cancel();
174
175 assertFalse(pp.hasSubscribers());
176
177 assertEquals(1, calls.get());
178 }
179
180 @Test
181 public void upstreamCancelledCloseCrash() throws Throwable {
182 TestHelper.withErrorTracking(errors -> {
183 PublishProcessor<Integer> pp = PublishProcessor.create();
184
185 TestSubscriber<Integer> ts = pp
186 .flatMapStream(v -> Stream.of(v + 1, v + 2).onClose(() -> { throw new TestException(); }))
187 .test(1);
188
189 assertTrue(pp.hasSubscribers());
190
191 pp.onNext(1);
192
193 ts.assertValuesOnly(2);
194 ts.cancel();
195
196 assertFalse(pp.hasSubscribers());
197
198 TestHelper.assertUndeliverable(errors, 0, TestException.class);
199 });
200 }
201
202 @Test
203 public void crossMap() {
204 Flowable.range(1, 1000)
205 .flatMapStream(v -> IntStream.range(v * 1000, v * 1000 + 1000).boxed())
206 .test()
207 .assertValueCount(1_000_000)
208 .assertNoErrors()
209 .assertComplete();
210 }
211
212 @Test
213 public void crossMapHidden() {
214 Flowable.range(1, 1000)
215 .hide()
216 .flatMapStream(v -> IntStream.range(v * 1000, v * 1000 + 1000).boxed())
217 .test()
218 .assertValueCount(1_000_000)
219 .assertNoErrors()
220 .assertComplete();
221 }
222
223 @Test
224 public void crossMapBackpressured() {
225 for (int n = 1; n < 2048; n *= 2) {
226 Flowable.range(1, 1000)
227 .flatMapStream(v -> IntStream.range(v * 1000, v * 1000 + 1000).boxed())
228 .rebatchRequests(n)
229 .test()
230 .withTag("rebatch: " + n)
231 .assertValueCount(1_000_000)
232 .assertNoErrors()
233 .assertComplete();
234 }
235 }
236
237 @Test
238 public void crossMapBackpressuredHidden() {
239 for (int n = 1; n < 2048; n *= 2) {
240 Flowable.range(1, 1000)
241 .hide()
242 .flatMapStream(v -> IntStream.range(v * 1000, v * 1000 + 1000).boxed())
243 .rebatchRequests(n)
244 .test()
245 .withTag("rebatch: " + n)
246 .assertValueCount(1_000_000)
247 .assertNoErrors()
248 .assertComplete();
249 }
250 }
251
252 @Test
253 public void onSubscribe() {
254 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.flatMapStream(v -> Stream.of(1, 2)));
255 }
256
257 @Test
258 public void badRequest() {
259 TestHelper.assertBadRequestReported(UnicastProcessor.create().flatMapStream(v -> Stream.of(1, 2)));
260 }
261
262 @Test
263 public void queueOverflow() throws Throwable {
264 TestHelper.withErrorTracking(errors -> {
265 new Flowable<Integer>() {
266 @Override
267 protected void subscribeActual(Subscriber<? super Integer> s) {
268 s.onSubscribe(new BooleanSubscription());
269 s.onNext(1);
270 s.onNext(2);
271 s.onNext(3);
272 s.onError(new TestException());
273 }
274 }
275 .flatMapStream(v -> Stream.of(1, 2), 1)
276 .test(0)
277 .assertFailure(MissingBackpressureException.class);
278
279 TestHelper.assertUndeliverable(errors, 0, TestException.class);
280 });
281 }
282
283 @Test
284 public void mapperThrows() {
285 Flowable.just(1).hide()
286 .concatMapStream(v -> { throw new TestException(); })
287 .test()
288 .assertFailure(TestException.class);
289 }
290
291 @Test
292 public void mapperNull() {
293 Flowable.just(1).hide()
294 .concatMapStream(v -> null)
295 .test()
296 .assertFailure(NullPointerException.class);
297 }
298
299 @Test
300 public void streamNull() {
301 Flowable.just(1).hide()
302 .concatMapStream(v -> Stream.of(1, null))
303 .test()
304 .assertFailure(NullPointerException.class, 1);
305 }
306
307 @Test
308 public void hasNextThrows() {
309 Flowable.just(1).hide()
310 .concatMapStream(v -> Stream.generate(() -> { throw new TestException(); }))
311 .test()
312 .assertFailure(TestException.class);
313 }
314
315 @Test
316 public void hasNextThrowsLater() {
317 AtomicInteger counter = new AtomicInteger();
318 Flowable.just(1).hide()
319 .concatMapStream(v -> Stream.generate(() -> {
320 if (counter.getAndIncrement() == 0) {
321 return 1;
322 }
323 throw new TestException();
324 }))
325 .test()
326 .assertFailure(TestException.class, 1);
327 }
328
329 @Test
330 public void mapperThrowsWhenUpstreamErrors() throws Throwable {
331 TestHelper.withErrorTracking(errors -> {
332 PublishProcessor<Integer> pp = PublishProcessor.create();
333
334 AtomicInteger counter = new AtomicInteger();
335
336 TestSubscriber<Integer> ts = pp.hide()
337 .concatMapStream(v -> {
338 if (counter.getAndIncrement() == 0) {
339 return Stream.of(1, 2);
340 }
341 pp.onError(new IOException());
342 throw new TestException();
343 })
344 .test();
345
346 pp.onNext(1);
347 pp.onNext(2);
348
349 ts
350 .assertFailure(IOException.class, 1, 2);
351
352 TestHelper.assertUndeliverable(errors, 0, TestException.class);
353 });
354 }
355
356 @Test
357 public void rangeBackpressured() {
358 Flowable.range(1, 5)
359 .hide()
360 .concatMapStream(v -> Stream.of(v), 1)
361 .test(0)
362 .assertEmpty()
363 .requestMore(5)
364 .assertResult(1, 2, 3, 4, 5);
365 }
366
367 @Test
368 public void cancelAfterIteratorNext() throws Exception {
369 TestSubscriber<Integer> ts = new TestSubscriber<>();
370
371 @SuppressWarnings("unchecked")
372 Stream<Integer> stream = mock(Stream.class);
373 when(stream.iterator()).thenReturn(new Iterator<Integer>() {
374
375 @Override
376 public boolean hasNext() {
377 return true;
378 }
379
380 @Override
381 public Integer next() {
382 ts.cancel();
383 return 1;
384 }
385 });
386
387 Flowable.just(1)
388 .hide()
389 .concatMapStream(v -> stream)
390 .subscribe(ts);
391
392 ts.assertEmpty();
393 }
394
395 @Test
396 public void asyncUpstreamFused() {
397 UnicastProcessor<Integer> up = UnicastProcessor.create();
398
399 TestSubscriber<Integer> ts = up.flatMapStream(v -> Stream.of(1, 2))
400 .test();
401
402 assertTrue(up.hasSubscribers());
403
404 up.onNext(1);
405
406 ts.assertValuesOnly(1, 2);
407
408 up.onComplete();
409
410 ts.assertResult(1, 2);
411 }
412
413 @Test
414 public void asyncUpstreamFusionBoundary() {
415 UnicastProcessor<Integer> up = UnicastProcessor.create();
416
417 TestSubscriber<Integer> ts = up
418 .map(v -> v + 1)
419 .flatMapStream(v -> Stream.of(1, 2))
420 .test();
421
422 assertTrue(up.hasSubscribers());
423
424 up.onNext(1);
425
426 ts.assertValuesOnly(1, 2);
427
428 up.onComplete();
429
430 ts.assertResult(1, 2);
431 }
432
433 @Test
434 public void fusedPollCrash() {
435 UnicastProcessor<Integer> up = UnicastProcessor.create();
436
437 TestSubscriber<Integer> ts = up
438 .map(v -> { throw new TestException(); })
439 .compose(TestHelper.flowableStripBoundary())
440 .flatMapStream(v -> Stream.of(1, 2))
441 .test();
442
443 assertTrue(up.hasSubscribers());
444
445 up.onNext(1);
446
447 assertFalse(up.hasSubscribers());
448
449 ts.assertFailure(TestException.class);
450 }
451 }