Debian Codegraph @

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