Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRangeLongTest.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.operators.flowable;
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.*;
21 import java.util.concurrent.atomic.*;
22
23 import org.junit.Test;
24 import org.reactivestreams.Subscriber;
25
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.operators.QueueFuseable;
30 import io.reactivex.rxjava3.subscribers.*;
31 import io.reactivex.rxjava3.testsupport.*;
32
33 public class FlowableRangeLongTest extends RxJavaTest {
34
35 @Test
36 public void rangeStartAt2Count3() {
37 Subscriber<Long> subscriber = TestHelper.mockSubscriber();
38
39 Flowable.rangeLong(2, 3).subscribe(subscriber);
40
41 verify(subscriber, times(1)).onNext(2L);
42 verify(subscriber, times(1)).onNext(3L);
43 verify(subscriber, times(1)).onNext(4L);
44 verify(subscriber, never()).onNext(5L);
45 verify(subscriber, never()).onError(any(Throwable.class));
46 verify(subscriber, times(1)).onComplete();
47 }
48
49 @Test
50 public void rangeUnsubscribe() {
51 Subscriber<Long> subscriber = TestHelper.mockSubscriber();
52
53 final AtomicInteger count = new AtomicInteger();
54
55 Flowable.rangeLong(1, 1000).doOnNext(new Consumer<Long>() {
56 @Override
57 public void accept(Long t1) {
58 count.incrementAndGet();
59 }
60 })
61 .take(3).subscribe(subscriber);
62
63 verify(subscriber, times(1)).onNext(1L);
64 verify(subscriber, times(1)).onNext(2L);
65 verify(subscriber, times(1)).onNext(3L);
66 verify(subscriber, never()).onNext(4L);
67 verify(subscriber, never()).onError(any(Throwable.class));
68 verify(subscriber, times(1)).onComplete();
69 assertEquals(3, count.get());
70 }
71
72 @Test
73 public void rangeWithZero() {
74 Flowable.rangeLong(1, 0);
75 }
76
77 @Test
78 public void rangeWithOverflow2() {
79 Flowable.rangeLong(Long.MAX_VALUE, 0);
80 }
81
82 @Test
83 public void rangeWithOverflow3() {
84 Flowable.rangeLong(1, Long.MAX_VALUE);
85 }
86
87 @Test(expected = IllegalArgumentException.class)
88 public void rangeWithOverflow4() {
89 Flowable.rangeLong(2, Long.MAX_VALUE);
90 }
91
92 @Test
93 public void rangeWithOverflow5() {
94 assertFalse(Flowable.rangeLong(Long.MIN_VALUE, 0).blockingIterable().iterator().hasNext());
95 }
96
97 @Test
98 public void backpressureViaRequest() {
99 Flowable<Long> f = Flowable.rangeLong(1, Flowable.bufferSize());
100
101 TestSubscriberEx<Long> ts = new TestSubscriberEx<>(0L);
102
103 ts.assertNoValues();
104 ts.request(1);
105
106 f.subscribe(ts);
107
108 ts.assertValue(1L);
109
110 ts.request(2);
111 ts.assertValues(1L, 2L, 3L);
112
113 ts.request(3);
114 ts.assertValues(1L, 2L, 3L, 4L, 5L, 6L);
115
116 ts.request(Flowable.bufferSize());
117 ts.assertTerminated();
118 }
119
120 @Test
121 public void noBackpressure() {
122 ArrayList<Long> list = new ArrayList<>(Flowable.bufferSize() * 2);
123 for (long i = 1; i <= Flowable.bufferSize() * 2 + 1; i++) {
124 list.add(i);
125 }
126
127 Flowable<Long> f = Flowable.rangeLong(1, list.size());
128
129 TestSubscriberEx<Long> ts = new TestSubscriberEx<>(0L);
130
131 ts.assertNoValues();
132 ts.request(Long.MAX_VALUE); // infinite
133
134 f.subscribe(ts);
135
136 ts.assertValueSequence(list);
137 ts.assertTerminated();
138 }
139 void withBackpressureOneByOne(long start) {
140 Flowable<Long> source = Flowable.rangeLong(start, 100);
141
142 TestSubscriberEx<Long> ts = new TestSubscriberEx<>(0L);
143 ts.request(1);
144 source.subscribe(ts);
145
146 List<Long> list = new ArrayList<>(100);
147 for (long i = 0; i < 100; i++) {
148 list.add(i + start);
149 ts.request(1);
150 }
151 ts.assertValueSequence(list);
152 ts.assertTerminated();
153 }
154 void withBackpressureAllAtOnce(long start) {
155 Flowable<Long> source = Flowable.rangeLong(start, 100);
156
157 TestSubscriberEx<Long> ts = new TestSubscriberEx<>(0L);
158 ts.request(100);
159 source.subscribe(ts);
160
161 List<Long> list = new ArrayList<>(100);
162 for (long i = 0; i < 100; i++) {
163 list.add(i + start);
164 }
165 ts.assertValueSequence(list);
166 ts.assertTerminated();
167 }
168
169 @Test
170 public void withBackpressure1() {
171 for (long i = 0; i < 100; i++) {
172 withBackpressureOneByOne(i);
173 }
174 }
175
176 @Test
177 public void withBackpressureAllAtOnce() {
178 for (long i = 0; i < 100; i++) {
179 withBackpressureAllAtOnce(i);
180 }
181 }
182
183 @Test
184 public void withBackpressureRequestWayMore() {
185 Flowable<Long> source = Flowable.rangeLong(50, 100);
186
187 TestSubscriberEx<Long> ts = new TestSubscriberEx<>(0L);
188 ts.request(150);
189 source.subscribe(ts);
190
191 List<Long> list = new ArrayList<>(100);
192 for (long i = 0; i < 100; i++) {
193 list.add(i + 50);
194 }
195
196 ts.request(50); // and then some
197
198 ts.assertValueSequence(list);
199 ts.assertTerminated();
200 }
201
202 @Test
203 public void requestOverflow() {
204 final AtomicInteger count = new AtomicInteger();
205 int n = 10;
206 Flowable.rangeLong(1, n).subscribe(new DefaultSubscriber<Long>() {
207
208 @Override
209 public void onStart() {
210 request(2);
211 }
212
213 @Override
214 public void onComplete() {
215 //do nothing
216 }
217
218 @Override
219 public void onError(Throwable e) {
220 throw new RuntimeException(e);
221 }
222
223 @Override
224 public void onNext(Long t) {
225 count.incrementAndGet();
226 request(Long.MAX_VALUE - 1);
227 }});
228 assertEquals(n, count.get());
229 }
230
231 @Test
232 public void emptyRangeSendsOnCompleteEagerlyWithRequestZero() {
233 final AtomicBoolean completed = new AtomicBoolean(false);
234 Flowable.rangeLong(1, 0).subscribe(new DefaultSubscriber<Long>() {
235
236 @Override
237 public void onStart() {
238 // request(0);
239 }
240
241 @Override
242 public void onComplete() {
243 completed.set(true);
244 }
245
246 @Override
247 public void onError(Throwable e) {
248
249 }
250
251 @Override
252 public void onNext(Long t) {
253
254 }});
255 assertTrue(completed.get());
256 }
257
258 @Test
259 public void nearMaxValueWithoutBackpressure() {
260 TestSubscriber<Long> ts = new TestSubscriber<>();
261 Flowable.rangeLong(Long.MAX_VALUE - 1L, 2L).subscribe(ts);
262
263 ts.assertComplete();
264 ts.assertNoErrors();
265 ts.assertValues(Long.MAX_VALUE - 1L, Long.MAX_VALUE);
266 }
267
268 @Test
269 public void nearMaxValueWithBackpressure() {
270 TestSubscriber<Long> ts = new TestSubscriber<>(3L);
271 Flowable.rangeLong(Long.MAX_VALUE - 1L, 2L).subscribe(ts);
272
273 ts.assertComplete();
274 ts.assertNoErrors();
275 ts.assertValues(Long.MAX_VALUE - 1L, Long.MAX_VALUE);
276 }
277
278 @Test
279 public void negativeCount() {
280 try {
281 Flowable.rangeLong(1L, -1L);
282 fail("Should have thrown IllegalArgumentException");
283 } catch (IllegalArgumentException ex) {
284 assertEquals("count >= 0 required but it was -1", ex.getMessage());
285 }
286 }
287
288 @Test
289 public void countOne() {
290 Flowable.rangeLong(5495454L, 1L)
291 .test()
292 .assertResult(5495454L);
293 }
294
295 @Test
296 public void fused() {
297 TestSubscriberEx<Long> ts = new TestSubscriberEx<Long>().setInitialFusionMode(QueueFuseable.ANY);
298
299 Flowable.rangeLong(1, 2).subscribe(ts);
300
301 ts.assertFusionMode(QueueFuseable.SYNC)
302 .assertResult(1L, 2L);
303 }
304
305 @Test
306 public void fusedReject() {
307 TestSubscriberEx<Long> ts = new TestSubscriberEx<Long>().setInitialFusionMode(QueueFuseable.ASYNC);
308
309 Flowable.rangeLong(1, 2).subscribe(ts);
310
311 ts.assertFusionMode(QueueFuseable.NONE)
312 .assertResult(1L, 2L);
313 }
314
315 @Test
316 public void disposed() {
317 TestHelper.checkDisposed(Flowable.rangeLong(1, 2));
318 }
319
320 @Test
321 public void fusedClearIsEmpty() {
322 TestHelper.checkFusedIsEmptyClear(Flowable.rangeLong(1, 2));
323 }
324
325 @Test
326 public void noOverflow() {
327 Flowable.rangeLong(Long.MAX_VALUE - 1, 2);
328 Flowable.rangeLong(Long.MIN_VALUE, 2);
329 Flowable.rangeLong(Long.MIN_VALUE, Long.MAX_VALUE);
330 }
331
332 @Test
333 public void conditionalNormal() {
334 Flowable.rangeLong(1L, 5L)
335 .filter(Functions.alwaysTrue())
336 .test()
337 .assertResult(1L, 2L, 3L, 4L, 5L);
338 }
339
340 @Test
341 public void badRequest() {
342 TestHelper.assertBadRequestReported(Flowable.rangeLong(1L, 5L));
343
344 TestHelper.assertBadRequestReported(Flowable.rangeLong(1L, 5L).filter(Functions.alwaysTrue()));
345 }
346
347 @Test
348 public void conditionalNormalSlowpath() {
349 Flowable.rangeLong(1L, 5L)
350 .filter(Functions.alwaysTrue())
351 .test(5)
352 .assertResult(1L, 2L, 3L, 4L, 5L);
353 }
354
355 @Test
356 public void conditionalSlowPathTakeExact() {
357 Flowable.rangeLong(1L, 5L)
358 .filter(Functions.alwaysTrue())
359 .take(5)
360 .test()
361 .assertResult(1L, 2L, 3L, 4L, 5L);
362 }
363
364 @Test
365 public void slowPathTakeExact() {
366 Flowable.rangeLong(1L, 5L)
367 .filter(Functions.alwaysTrue())
368 .take(5)
369 .test()
370 .assertResult(1L, 2L, 3L, 4L, 5L);
371 }
372
373 @Test
374 public void conditionalSlowPathRebatch() {
375 Flowable.rangeLong(1L, 5L)
376 .filter(Functions.alwaysTrue())
377 .rebatchRequests(1)
378 .test()
379 .assertResult(1L, 2L, 3L, 4L, 5L);
380 }
381
382 @Test
383 public void slowPathRebatch() {
384 Flowable.rangeLong(1L, 5L)
385 .rebatchRequests(1)
386 .test()
387 .assertResult(1L, 2L, 3L, 4L, 5L);
388 }
389
390 @Test
391 public void slowPathCancel() {
392 TestSubscriber<Long> ts = new TestSubscriber<Long>(2L) {
393 @Override
394 public void onNext(Long t) {
395 super.onNext(t);
396 cancel();
397 onComplete();
398 }
399 };
400
401 Flowable.rangeLong(1L, 5L)
402 .subscribe(ts);
403
404 ts.assertResult(1L);
405 }
406
407 @Test
408 public void fastPathCancel() {
409 TestSubscriber<Long> ts = new TestSubscriber<Long>() {
410 @Override
411 public void onNext(Long t) {
412 super.onNext(t);
413 cancel();
414 onComplete();
415 }
416 };
417
418 Flowable.rangeLong(1L, 5L)
419 .subscribe(ts);
420
421 ts.assertResult(1L);
422 }
423
424 @Test
425 public void conditionalSlowPathCancel() {
426 TestSubscriber<Long> ts = new TestSubscriber<Long>(1L) {
427 @Override
428 public void onNext(Long t) {
429 super.onNext(t);
430 cancel();
431 onComplete();
432 }
433 };
434
435 Flowable.rangeLong(1L, 5L)
436 .filter(Functions.alwaysTrue())
437 .subscribe(ts);
438
439 ts.assertResult(1L);
440 }
441
442 @Test
443 public void conditionalFastPathCancel() {
444 TestSubscriber<Long> ts = new TestSubscriber<Long>() {
445 @Override
446 public void onNext(Long t) {
447 super.onNext(t);
448 cancel();
449 onComplete();
450 }
451 };
452
453 Flowable.rangeLong(1L, 5L)
454 .filter(Functions.alwaysTrue())
455 .subscribe(ts);
456
457 ts.assertResult(1L);
458 }
459
460 @Test
461 public void conditionalRequestOneByOne() {
462 TestSubscriber<Long> ts = new TestSubscriber<Long>(1L) {
463 @Override
464 public void onNext(Long t) {
465 super.onNext(t);
466 request(1);
467 }
468 };
469
470 Flowable.rangeLong(1L, 5L)
471 .filter(new Predicate<Long>() {
472 @Override
473 public boolean test(Long v) throws Exception {
474 return v % 2 == 0;
475 }
476 })
477 .subscribe(ts);
478
479 ts.assertResult(2L, 4L);
480 }
481
482 @Test
483 public void conditionalRequestOneByOne2() {
484 TestSubscriber<Long> ts = new TestSubscriber<Long>(1L) {
485 @Override
486 public void onNext(Long t) {
487 super.onNext(t);
488 request(1);
489 }
490 };
491
492 Flowable.rangeLong(1L, 5L)
493 .filter(Functions.alwaysTrue())
494 .subscribe(ts);
495
496 ts.assertResult(1L, 2L, 3L, 4L, 5L);
497 }
498
499 @Test
500 public void fastPathCancelExact() {
501 TestSubscriber<Long> ts = new TestSubscriber<Long>() {
502 @Override
503 public void onNext(Long t) {
504 super.onNext(t);
505 if (t == 5L) {
506 cancel();
507 onComplete();
508 }
509 }
510 };
511
512 Flowable.rangeLong(1L, 5L)
513 .subscribe(ts);
514
515 ts.assertResult(1L, 2L, 3L, 4L, 5L);
516 }
517
518 @Test
519 public void conditionalFastPathCancelExact() {
520 TestSubscriber<Long> ts = new TestSubscriber<Long>() {
521 @Override
522 public void onNext(Long t) {
523 super.onNext(t);
524 if (t == 5L) {
525 cancel();
526 onComplete();
527 }
528 }
529 };
530
531 Flowable.rangeLong(1L, 5L)
532 .filter(new Predicate<Long>() {
533 @Override
534 public boolean test(Long v) throws Exception {
535 return v % 2 == 0;
536 }
537 })
538 .subscribe(ts);
539
540 ts.assertResult(2L, 4L);
541 }
542
543 @Test
544 public void slowPathCancelBeforeComplete() {
545 Flowable.rangeLong(1, 2)
546 .take(2)
547 .test()
548 .assertResult(1L, 2L);
549 }
550
551 @Test
552 public void conditionalFastPathCancelBeforeComplete() {
553 TestSubscriber<Long> ts = new TestSubscriber<>();
554
555 Flowable.rangeLong(1, 2)
556 .compose(TestHelper.conditional())
557 .doOnNext(v -> {
558 if (v == 2L) {
559 ts.cancel();
560 }
561 })
562 .subscribe(ts);
563
564 ts.assertValuesOnly(1L, 2L);
565 }
566
567 @Test
568 public void conditionalSlowPathTake() {
569 TestSubscriber<Long> ts = new TestSubscriber<>(4);
570
571 Flowable.rangeLong(1, 3)
572 .compose(TestHelper.conditional())
573 .doOnNext(v -> {
574 if (v == 2L) {
575 ts.cancel();
576 }
577 })
578 .subscribe(ts);
579
580 ts.assertValuesOnly(1L, 2L);
581 }
582 }