Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/jdk8/FlowableFromStreamTest.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
18 import java.util.*;
19 import java.util.concurrent.*;
20 import java.util.concurrent.atomic.*;
21 import java.util.stream.*;
22
23 import org.junit.Test;
24 import org.reactivestreams.Subscription;
25
26 import io.reactivex.rxjava3.annotations.NonNull;
27 import io.reactivex.rxjava3.core.*;
28 import io.reactivex.rxjava3.exceptions.TestException;
29 import io.reactivex.rxjava3.operators.QueueFuseable;
30 import io.reactivex.rxjava3.operators.QueueSubscription;
31 import io.reactivex.rxjava3.operators.SimpleQueue;
32 import io.reactivex.rxjava3.schedulers.Schedulers;
33 import io.reactivex.rxjava3.testsupport.*;
34
35 public class FlowableFromStreamTest extends RxJavaTest {
36
37 @Test
38 public void empty() {
39 Flowable.fromStream(Stream.<Integer>of())
40 .test()
41 .assertResult();
42 }
43
44 @Test
45 public void just() {
46 Flowable.fromStream(Stream.<Integer>of(1))
47 .test()
48 .assertResult(1);
49 }
50
51 @Test
52 public void many() {
53 Flowable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5))
54 .test()
55 .assertResult(1, 2, 3, 4, 5);
56 }
57
58 @Test
59 public void manyBackpressured() {
60 Flowable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5))
61 .test(0L)
62 .assertEmpty()
63 .requestMore(1)
64 .assertValuesOnly(1)
65 .requestMore(2)
66 .assertValuesOnly(1, 2, 3)
67 .requestMore(2)
68 .assertResult(1, 2, 3, 4, 5);
69 }
70
71 @Test
72 public void noReuse() {
73 Flowable<Integer> source = Flowable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5));
74
75 source
76 .test()
77 .assertResult(1, 2, 3, 4, 5);
78
79 source
80 .test()
81 .assertFailure(IllegalStateException.class);
82 }
83
84 @Test
85 public void take() {
86 Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed())
87 .take(5)
88 .test()
89 .assertResult(1, 2, 3, 4, 5);
90 }
91
92 @Test
93 public void emptyConditional() {
94 Flowable.fromStream(Stream.<Integer>of())
95 .filter(v -> true)
96 .test()
97 .assertResult();
98 }
99
100 @Test
101 public void justConditional() {
102 Flowable.fromStream(Stream.<Integer>of(1))
103 .filter(v -> true)
104 .test()
105 .assertResult(1);
106 }
107
108 @Test
109 public void manyConditional() {
110 Flowable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5))
111 .filter(v -> true)
112 .test()
113 .assertResult(1, 2, 3, 4, 5);
114 }
115
116 @Test
117 public void manyBackpressuredConditional() {
118 Flowable.fromStream(Stream.<Integer>of(1, 2, 3, 4, 5))
119 .filter(v -> true)
120 .test(0L)
121 .assertEmpty()
122 .requestMore(1)
123 .assertValuesOnly(1)
124 .requestMore(2)
125 .assertValuesOnly(1, 2, 3)
126 .requestMore(2)
127 .assertResult(1, 2, 3, 4, 5);
128 }
129
130 @Test
131 public void manyConditionalSkip() {
132 Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed())
133 .filter(v -> v % 2 == 0)
134 .test()
135 .assertResult(2, 4, 6, 8, 10);
136 }
137
138 @Test
139 public void takeConditional() {
140 Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed())
141 .filter(v -> true)
142 .take(5)
143 .test()
144 .assertResult(1, 2, 3, 4, 5);
145 }
146
147 @Test
148 public void noOfferNoCrashAfterClear() throws Throwable {
149 AtomicReference<SimpleQueue<?>> queue = new AtomicReference<>();
150
151 Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed())
152 .subscribe(new FlowableSubscriber<Integer>() {
153 @Override
154 public void onSubscribe(@NonNull Subscription s) {
155 queue.set((SimpleQueue<?>)s);
156 }
157
158 @Override
159 public void onNext(Integer t) {
160 }
161
162 @Override
163 public void onError(Throwable t) {
164 }
165
166 @Override
167 public void onComplete() {
168 }
169 });
170
171 SimpleQueue<?> q = queue.get();
172 TestHelper.assertNoOffer(q);
173
174 assertFalse(q.isEmpty());
175
176 q.clear();
177
178 assertNull(q.poll());
179
180 assertTrue(q.isEmpty());
181
182 q.clear();
183
184 assertNull(q.poll());
185
186 assertTrue(q.isEmpty());
187 }
188
189 @Test
190 public void fusedPoll() throws Throwable {
191 AtomicReference<SimpleQueue<?>> queue = new AtomicReference<>();
192 AtomicInteger calls = new AtomicInteger();
193
194 Flowable.fromStream(Stream.of(1).onClose(() -> calls.getAndIncrement()))
195 .subscribe(new FlowableSubscriber<Integer>() {
196 @Override
197 public void onSubscribe(@NonNull Subscription s) {
198 queue.set((SimpleQueue<?>)s);
199 ((QueueSubscription<?>)s).requestFusion(QueueFuseable.ANY);
200 }
201
202 @Override
203 public void onNext(Integer t) {
204 }
205
206 @Override
207 public void onError(Throwable t) {
208 }
209
210 @Override
211 public void onComplete() {
212 }
213 });
214
215 SimpleQueue<?> q = queue.get();
216
217 assertFalse(q.isEmpty());
218
219 assertEquals(1, q.poll());
220
221 assertTrue(q.isEmpty());
222
223 assertEquals(1, calls.get());
224 }
225
226 @Test
227 public void streamOfNull() {
228 Flowable.fromStream(Stream.of((Integer)null))
229 .test()
230 .assertFailure(NullPointerException.class);
231 }
232
233 @Test
234 public void streamOfNullConditional() {
235 Flowable.fromStream(Stream.of((Integer)null))
236 .filter(v -> true)
237 .test()
238 .assertFailure(NullPointerException.class);
239 }
240
241 @Test
242 public void syncFusionSupport() {
243 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
244 ts.setInitialFusionMode(QueueFuseable.ANY);
245
246 Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed())
247 .subscribeWith(ts)
248 .assertFuseable()
249 .assertFusionMode(QueueFuseable.SYNC)
250 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
251 }
252
253 @Test
254 public void asyncFusionNotSupported() {
255 TestSubscriberEx<Integer> ts = new TestSubscriberEx<>();
256 ts.setInitialFusionMode(QueueFuseable.ASYNC);
257
258 Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed())
259 .subscribeWith(ts)
260 .assertFuseable()
261 .assertFusionMode(QueueFuseable.NONE)
262 .assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
263 }
264
265 @Test
266 public void fusedForParallel() {
267 Flowable.fromStream(IntStream.rangeClosed(1, 1000).boxed())
268 .parallel()
269 .runOn(Schedulers.computation(), 1)
270 .map(v -> v + 1)
271 .sequential()
272 .test()
273 .awaitDone(5, TimeUnit.SECONDS)
274 .assertValueCount(1000)
275 .assertNoErrors()
276 .assertComplete();
277 }
278
279 @Test
280 public void runToEndCloseCrash() throws Throwable {
281 TestHelper.withErrorTracking(errors -> {
282 Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5).onClose(() -> { throw new TestException(); });
283
284 Flowable.fromStream(stream)
285 .test()
286 .assertResult(1, 2, 3, 4, 5);
287
288 TestHelper.assertUndeliverable(errors, 0, TestException.class);
289 });
290 }
291
292 @Test
293 public void takeCloseCrash() throws Throwable {
294 TestHelper.withErrorTracking(errors -> {
295 Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5).onClose(() -> { throw new TestException(); });
296
297 Flowable.fromStream(stream)
298 .take(3)
299 .test()
300 .assertResult(1, 2, 3);
301
302 TestHelper.assertUndeliverable(errors, 0, TestException.class);
303 });
304 }
305
306 @Test
307 public void hasNextCrash() {
308 AtomicInteger v = new AtomicInteger();
309 Flowable.fromStream(Stream.<Integer>generate(() -> {
310 int value = v.getAndIncrement();
311 if (value == 1) {
312 throw new TestException();
313 }
314 return value;
315 }))
316 .test()
317 .assertFailure(TestException.class, 0);
318 }
319
320 @Test
321 public void hasNextCrashConditional() {
322 AtomicInteger counter = new AtomicInteger();
323 Flowable.fromStream(Stream.<Integer>generate(() -> {
324 int value = counter.getAndIncrement();
325 if (value == 1) {
326 throw new TestException();
327 }
328 return value;
329 }))
330 .filter(v -> true)
331 .test()
332 .assertFailure(TestException.class, 0);
333 }
334
335 void requestOneByOneBase(boolean conditional) {
336 List<Object> list = new ArrayList<>();
337
338 Flowable<Integer> source = Flowable.fromStream(IntStream.rangeClosed(1, 10).boxed());
339 if (conditional) {
340 source = source.filter(v -> true);
341 }
342
343 source.subscribe(new FlowableSubscriber<Integer>() {
344
345 @NonNull Subscription upstream;
346
347 @Override
348 public void onSubscribe(@NonNull Subscription s) {
349 this.upstream = s;
350 s.request(1);
351 }
352
353 @Override
354 public void onNext(Integer t) {
355 list.add(t);
356 upstream.request(1);
357 }
358
359 @Override
360 public void onError(Throwable t) {
361 list.add(t);
362 }
363
364 @Override
365 public void onComplete() {
366 list.add(100);
367 }
368 });
369
370 assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100), list);
371 }
372
373 @Test
374 public void requestOneByOne() {
375 requestOneByOneBase(false);
376 }
377
378 @Test
379 public void requestOneByOneConditional() {
380 requestOneByOneBase(true);
381 }
382
383 void requestRaceBase(boolean conditional) throws Exception {
384 ExecutorService exec = Executors.newCachedThreadPool();
385 try {
386 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
387
388 AtomicInteger counter = new AtomicInteger();
389
390 int max = 100;
391
392 Flowable<Integer> source = Flowable.fromStream(IntStream.rangeClosed(1, max).boxed());
393 if (conditional) {
394 source = source.filter(v -> true);
395 }
396
397 CountDownLatch cdl = new CountDownLatch(1);
398
399 source
400 .subscribe(new FlowableSubscriber<Integer>() {
401
402 @NonNull Subscription upstream;
403
404 @Override
405 public void onSubscribe(@NonNull Subscription s) {
406
407 this.upstream = s;
408 s.request(1);
409
410 }
411
412 @Override
413 public void onNext(Integer t) {
414 counter.getAndIncrement();
415
416 AtomicInteger sync = new AtomicInteger(2);
417 exec.submit(() -> {
418 if (sync.decrementAndGet() != 0) {
419 while (sync.get() != 0) { }
420 }
421 upstream.request(1);
422 });
423
424 if (sync.decrementAndGet() != 0) {
425 while (sync.get() != 0) { }
426 }
427 }
428
429 @Override
430 public void onError(Throwable t) {
431 t.printStackTrace();
432 cdl.countDown();
433 }
434
435 @Override
436 public void onComplete() {
437 counter.getAndIncrement();
438 cdl.countDown();
439 }
440 });
441
442 assertTrue(cdl.await(60, TimeUnit.SECONDS));
443
444 assertEquals(max + 1, counter.get());
445 }
446 } finally {
447 exec.shutdown();
448 }
449 }
450
451 @Test
452 public void requestRace() throws Exception {
453 requestRaceBase(false);
454 }
455
456 @Test
457 public void requestRaceConditional() throws Exception {
458 requestRaceBase(true);
459 }
460
461 @Test
462 public void closeCalledOnEmpty() {
463 AtomicInteger calls = new AtomicInteger();
464
465 Flowable.fromStream(Stream.of().onClose(() -> calls.getAndIncrement()))
466 .test()
467 .assertResult();
468
469 assertEquals(1, calls.get());
470 }
471
472 @Test
473 public void closeCalledAfterItems() {
474 AtomicInteger calls = new AtomicInteger();
475
476 Flowable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement()))
477 .test()
478 .assertResult(1, 2, 3, 4, 5);
479
480 assertEquals(1, calls.get());
481 }
482
483 @Test
484 public void closeCalledOnCancel() {
485 AtomicInteger calls = new AtomicInteger();
486
487 Flowable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement()))
488 .take(3)
489 .test()
490 .assertResult(1, 2, 3);
491
492 assertEquals(1, calls.get());
493 }
494
495 @Test
496 public void closeCalledOnItemCrash() {
497 AtomicInteger calls = new AtomicInteger();
498 AtomicInteger counter = new AtomicInteger();
499 Flowable.fromStream(Stream.<Integer>generate(() -> {
500 int value = counter.getAndIncrement();
501 if (value == 1) {
502 throw new TestException();
503 }
504 return value;
505 }).onClose(() -> calls.getAndIncrement()))
506 .test()
507 .assertFailure(TestException.class, 0);
508
509 assertEquals(1, calls.get());
510 }
511
512 @Test
513 public void closeCalledAfterItemsConditional() {
514 AtomicInteger calls = new AtomicInteger();
515
516 Flowable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement()))
517 .filter(v -> true)
518 .test()
519 .assertResult(1, 2, 3, 4, 5);
520
521 assertEquals(1, calls.get());
522 }
523
524 @Test
525 public void closeCalledOnCancelConditional() {
526 AtomicInteger calls = new AtomicInteger();
527
528 Flowable.fromStream(Stream.of(1, 2, 3, 4, 5).onClose(() -> calls.getAndIncrement()))
529 .filter(v -> true)
530 .take(3)
531 .test()
532 .assertResult(1, 2, 3);
533
534 assertEquals(1, calls.get());
535 }
536
537 @Test
538 public void closeCalledOnItemCrashConditional() {
539 AtomicInteger calls = new AtomicInteger();
540 AtomicInteger counter = new AtomicInteger();
541 Flowable.fromStream(Stream.<Integer>generate(() -> {
542 int value = counter.getAndIncrement();
543 if (value == 1) {
544 throw new TestException();
545 }
546 return value;
547 }).onClose(() -> calls.getAndIncrement()))
548 .filter(v -> true)
549 .test()
550 .assertFailure(TestException.class, 0);
551
552 assertEquals(1, calls.get());
553 }
554
555 @Test
556 public void badRequest() {
557 TestHelper.assertBadRequestReported(Flowable.fromStream(Stream.of(1)));
558 }
559 }