Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/MaybeFlattenStreamAsFlowableTest.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.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.Iterator;
21 import java.util.concurrent.atomic.AtomicReference;
22 import java.util.stream.*;
23
24 import org.junit.Test;
25 import org.reactivestreams.Subscription;
26
27 import io.reactivex.rxjava3.annotations.NonNull;
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.exceptions.TestException;
30 import io.reactivex.rxjava3.functions.Function;
31 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
32 import io.reactivex.rxjava3.operators.QueueFuseable;
33 import io.reactivex.rxjava3.operators.QueueSubscription;
34 import io.reactivex.rxjava3.subjects.MaybeSubject;
35 import io.reactivex.rxjava3.subscribers.TestSubscriber;
36 import io.reactivex.rxjava3.testsupport.*;
37
38 public class MaybeFlattenStreamAsFlowableTest extends RxJavaTest {
39
40 @Test
41 public void successJust() {
42 Maybe.just(1)
43 .flattenStreamAsFlowable(Stream::of)
44 .test()
45 .assertResult(1);
46 }
47
48 @Test
49 public void successEmpty() {
50 Maybe.just(1)
51 .flattenStreamAsFlowable(v -> Stream.of())
52 .test()
53 .assertResult();
54 }
55
56 @Test
57 public void successMany() {
58 Maybe.just(1)
59 .flattenStreamAsFlowable(v -> Stream.of(2, 3, 4, 5, 6))
60 .test()
61 .assertResult(2, 3, 4, 5, 6);
62 }
63
64 @Test
65 public void successManyTake() {
66 Maybe.just(1)
67 .flattenStreamAsFlowable(v -> Stream.of(2, 3, 4, 5, 6))
68 .take(3)
69 .test()
70 .assertResult(2, 3, 4);
71 }
72
73 @Test
74 public void empty() throws Throwable {
75 @SuppressWarnings("unchecked")
76 Function<? super Integer, Stream<? extends Integer>> f = mock(Function.class);
77
78 Maybe.<Integer>empty()
79 .flattenStreamAsFlowable(f)
80 .test()
81 .assertResult();
82
83 verify(f, never()).apply(any());
84 }
85
86 @Test
87 public void error() throws Throwable {
88 @SuppressWarnings("unchecked")
89 Function<? super Integer, Stream<? extends Integer>> f = mock(Function.class);
90
91 Maybe.<Integer>error(new TestException())
92 .flattenStreamAsFlowable(f)
93 .test()
94 .assertFailure(TestException.class);
95
96 verify(f, never()).apply(any());
97 }
98
99 @Test
100 public void mapperCrash() {
101 Maybe.just(1)
102 .flattenStreamAsFlowable(v -> { throw new TestException(); })
103 .test()
104 .assertFailure(TestException.class);
105 }
106
107 @Test
108 public void dispose() {
109 TestHelper.checkDisposed(Maybe.never().flattenStreamAsFlowable(Stream::of));
110 }
111
112 @Test
113 public void doubleOnSubscribe() {
114 TestHelper.checkDoubleOnSubscribeMaybeToFlowable(m -> m.flattenStreamAsFlowable(Stream::of));
115 }
116
117 @Test
118 public void badRequest() {
119 TestHelper.assertBadRequestReported(MaybeSubject.create().flattenStreamAsFlowable(Stream::of));
120 }
121
122 @Test
123 public void fusedEmpty() {
124 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
125 ts.setInitialFusionMode(QueueFuseable.ANY);
126
127 Maybe.just(1)
128 .flattenStreamAsFlowable(v -> Stream.<Integer>of())
129 .subscribe(ts);
130
131 ts.assertFuseable()
132 .assertFusionMode(QueueFuseable.ASYNC)
133 .assertResult();
134 }
135
136 @Test
137 public void fusedJust() {
138 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
139 ts.setInitialFusionMode(QueueFuseable.ANY);
140
141 Maybe.just(1)
142 .flattenStreamAsFlowable(v -> Stream.<Integer>of(v))
143 .subscribe(ts);
144
145 ts.assertFuseable()
146 .assertFusionMode(QueueFuseable.ASYNC)
147 .assertResult(1);
148 }
149
150 @Test
151 public void fusedMany() {
152 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
153 ts.setInitialFusionMode(QueueFuseable.ANY);
154
155 Maybe.just(1)
156 .flattenStreamAsFlowable(v -> Stream.<Integer>of(v, v + 1, v + 2))
157 .subscribe(ts);
158
159 ts.assertFuseable()
160 .assertFusionMode(QueueFuseable.ASYNC)
161 .assertResult(1, 2, 3);
162 }
163
164 @Test
165 public void fusedManyRejected() {
166 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
167 ts.setInitialFusionMode(QueueFuseable.SYNC);
168
169 Maybe.just(1)
170 .flattenStreamAsFlowable(v -> Stream.<Integer>of(v, v + 1, v + 2))
171 .subscribe(ts);
172
173 ts.assertFuseable()
174 .assertFusionMode(QueueFuseable.NONE)
175 .assertResult(1, 2, 3);
176 }
177
178 @Test
179 public void manyBackpressured() {
180 Maybe.just(1)
181 .flattenStreamAsFlowable(v -> IntStream.rangeClosed(1, 5).boxed())
182 .test(0L)
183 .assertEmpty()
184 .requestMore(2)
185 .assertValuesOnly(1, 2)
186 .requestMore(2)
187 .assertValuesOnly(1, 2, 3, 4)
188 .requestMore(1)
189 .assertResult(1, 2, 3, 4, 5);
190 }
191
192 @Test
193 public void manyBackpressured2() {
194 Maybe.just(1)
195 .flattenStreamAsFlowable(v -> IntStream.rangeClosed(1, 5).boxed())
196 .rebatchRequests(1)
197 .test(0L)
198 .assertEmpty()
199 .requestMore(2)
200 .assertValuesOnly(1, 2)
201 .requestMore(2)
202 .assertValuesOnly(1, 2, 3, 4)
203 .requestMore(1)
204 .assertResult(1, 2, 3, 4, 5);
205 }
206
207 @Test
208 public void fusedStreamAvailableLater() {
209 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
210 ts.setInitialFusionMode(QueueFuseable.ANY);
211
212 MaybeSubject<Integer> ms = MaybeSubject.create();
213
214 ms
215 .flattenStreamAsFlowable(v -> Stream.<Integer>of(v, v + 1, v + 2))
216 .subscribe(ts);
217
218 ts.assertFuseable()
219 .assertFusionMode(QueueFuseable.ASYNC)
220 .assertEmpty();
221
222 ms.onSuccess(1);
223
224 ts
225 .assertResult(1, 2, 3);
226 }
227
228 @Test
229 public void fused() throws Throwable {
230 AtomicReference<QueueSubscription<Integer>> qsr = new AtomicReference<>();
231
232 MaybeSubject<Integer> ms = MaybeSubject.create();
233
234 ms
235 .flattenStreamAsFlowable(Stream::of)
236 .subscribe(new FlowableSubscriber<Integer>() {
237
238 @Override
239 public void onNext(Integer t) {
240 }
241
242 @Override
243 public void onError(Throwable t) {
244 }
245
246 @Override
247 public void onComplete() {
248 }
249
250 @Override
251 @SuppressWarnings("unchecked")
252 public void onSubscribe(@NonNull Subscription s) {
253 qsr.set((QueueSubscription<Integer>)s);
254 }
255 });
256
257 QueueSubscription<Integer> qs = qsr.get();
258
259 assertEquals(QueueFuseable.ASYNC, qs.requestFusion(QueueFuseable.ASYNC));
260
261 assertTrue(qs.isEmpty());
262 assertNull(qs.poll());
263
264 ms.onSuccess(1);
265
266 assertFalse(qs.isEmpty());
267 assertEquals(1, qs.poll().intValue());
268
269 assertTrue(qs.isEmpty());
270 assertNull(qs.poll());
271
272 qs.cancel();
273
274 assertTrue(qs.isEmpty());
275 assertNull(qs.poll());
276 }
277
278 @Test
279 public void requestOneByOne() {
280 TestSubscriber<Integer> ts = new TestSubscriber<>();
281
282 Maybe.just(1)
283 .flattenStreamAsFlowable(v -> Stream.of(1, 2, 3, 4, 5))
284 .subscribe(new FlowableSubscriber<Integer>() {
285
286 Subscription upstream;
287
288 @Override
289 public void onSubscribe(@NonNull Subscription s) {
290 ts.onSubscribe(new BooleanSubscription());
291 upstream = s;
292 s.request(1);
293 }
294
295 @Override
296 public void onNext(Integer t) {
297 ts.onNext(t);
298 upstream.request(1);
299 }
300
301 @Override
302 public void onError(Throwable t) {
303 ts.onError(t);
304 }
305
306 @Override
307 public void onComplete() {
308 ts.onComplete();
309 }
310 });
311
312 ts.assertResult(1, 2, 3, 4, 5);
313 }
314
315 @Test
316 public void streamCloseCrash() throws Throwable {
317 TestHelper.withErrorTracking(errors -> {
318 Maybe.just(1)
319 .flattenStreamAsFlowable(v -> Stream.of(v).onClose(() -> { throw new TestException(); }))
320 .test()
321 .assertResult(1);
322
323 TestHelper.assertUndeliverable(errors, 0, TestException.class);
324 });
325 }
326
327 @Test
328 public void hasNextThrowsInDrain() {
329 @SuppressWarnings("unchecked")
330 Stream<Integer> stream = mock(Stream.class);
331 when(stream.iterator()).thenReturn(new Iterator<Integer>() {
332
333 int count;
334
335 @Override
336 public boolean hasNext() {
337 if (count++ > 0) {
338 throw new TestException();
339 }
340 return true;
341 }
342
343 @Override
344 public Integer next() {
345 return 1;
346 }
347 });
348
349 Maybe.just(1)
350 .flattenStreamAsFlowable(v -> stream)
351 .test()
352 .assertFailure(TestException.class, 1);
353 }
354
355 @Test
356 public void nextThrowsInDrain() {
357 @SuppressWarnings("unchecked")
358 Stream<Integer> stream = mock(Stream.class);
359 when(stream.iterator()).thenReturn(new Iterator<Integer>() {
360
361 @Override
362 public boolean hasNext() {
363 return true;
364 }
365
366 @Override
367 public Integer next() {
368 throw new TestException();
369 }
370 });
371
372 Maybe.just(1)
373 .flattenStreamAsFlowable(v -> stream)
374 .test()
375 .assertFailure(TestException.class);
376 }
377
378 @Test
379 public void cancelAfterHasNextInDrain() {
380 @SuppressWarnings("unchecked")
381 Stream<Integer> stream = mock(Stream.class);
382
383 TestSubscriber<Integer> ts = new TestSubscriber<>();
384
385 when(stream.iterator()).thenReturn(new Iterator<Integer>() {
386
387 int count;
388
389 @Override
390 public boolean hasNext() {
391 if (count++ > 0) {
392 ts.cancel();
393 }
394 return true;
395 }
396
397 @Override
398 public Integer next() {
399 return 1;
400 }
401 });
402
403 Maybe.just(1)
404 .flattenStreamAsFlowable(v -> stream)
405 .subscribeWith(ts)
406 .assertValuesOnly(1);
407 }
408
409 @Test
410 public void cancelAfterNextInDrain() {
411 @SuppressWarnings("unchecked")
412 Stream<Integer> stream = mock(Stream.class);
413
414 TestSubscriber<Integer> ts = new TestSubscriber<>();
415
416 when(stream.iterator()).thenReturn(new Iterator<Integer>() {
417
418 @Override
419 public boolean hasNext() {
420 return true;
421 }
422
423 @Override
424 public Integer next() {
425 ts.cancel();
426 return 1;
427 }
428 });
429
430 Maybe.just(1)
431 .flattenStreamAsFlowable(v -> stream)
432 .subscribeWith(ts)
433 .assertEmpty();
434 }
435
436 @Test
437 public void requestSuccessRace() {
438 for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) {
439 MaybeSubject<Integer> ms = MaybeSubject.create();
440
441 TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
442
443 ms.flattenStreamAsFlowable(Stream::of)
444 .subscribe(ts);
445
446 Runnable r1 = () -> ms.onSuccess(1);
447 Runnable r2 = () -> ts.request(1);
448
449 TestHelper.race(r1, r2);
450
451 ts.assertResult(1);
452 }
453 }
454 }