Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableSequenceEqualTest.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.mockito.ArgumentMatchers.isA;
17 import static org.mockito.Mockito.*;
18
19 import java.util.List;
20 import java.util.concurrent.TimeUnit;
21
22 import org.junit.Test;
23 import org.mockito.InOrder;
24 import org.reactivestreams.Subscriber;
25
26 import io.reactivex.rxjava3.core.*;
27 import io.reactivex.rxjava3.exceptions.*;
28 import io.reactivex.rxjava3.functions.*;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.observers.TestObserver;
31 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
32 import io.reactivex.rxjava3.processors.PublishProcessor;
33 import io.reactivex.rxjava3.schedulers.Schedulers;
34 import io.reactivex.rxjava3.subscribers.TestSubscriber;
35 import io.reactivex.rxjava3.testsupport.TestHelper;
36
37 public class FlowableSequenceEqualTest extends RxJavaTest {
38
39 @Test
40 public void flowable1() {
41 Flowable<Boolean> flowable = Flowable.sequenceEqual(
42 Flowable.just("one", "two", "three"),
43 Flowable.just("one", "two", "three")).toFlowable();
44 verifyResult(flowable, true);
45 }
46
47 @Test
48 public void flowable2() {
49 Flowable<Boolean> flowable = Flowable.sequenceEqual(
50 Flowable.just("one", "two", "three"),
51 Flowable.just("one", "two", "three", "four")).toFlowable();
52 verifyResult(flowable, false);
53 }
54
55 @Test
56 public void flowable3() {
57 Flowable<Boolean> flowable = Flowable.sequenceEqual(
58 Flowable.just("one", "two", "three", "four"),
59 Flowable.just("one", "two", "three")).toFlowable();
60 verifyResult(flowable, false);
61 }
62
63 @Test
64 public void withError1Flowable() {
65 Flowable<Boolean> flowable = Flowable.sequenceEqual(
66 Flowable.concat(Flowable.just("one"),
67 Flowable.<String> error(new TestException())),
68 Flowable.just("one", "two", "three")).toFlowable();
69 verifyError(flowable);
70 }
71
72 @Test
73 public void withError2Flowable() {
74 Flowable<Boolean> flowable = Flowable.sequenceEqual(
75 Flowable.just("one", "two", "three"),
76 Flowable.concat(Flowable.just("one"),
77 Flowable.<String> error(new TestException()))).toFlowable();
78 verifyError(flowable);
79 }
80
81 @Test
82 public void withError3Flowable() {
83 Flowable<Boolean> flowable = Flowable.sequenceEqual(
84 Flowable.concat(Flowable.just("one"),
85 Flowable.<String> error(new TestException())),
86 Flowable.concat(Flowable.just("one"),
87 Flowable.<String> error(new TestException()))).toFlowable();
88 verifyError(flowable);
89 }
90
91 @Test
92 public void withEmpty1Flowable() {
93 Flowable<Boolean> flowable = Flowable.sequenceEqual(
94 Flowable.<String> empty(),
95 Flowable.just("one", "two", "three")).toFlowable();
96 verifyResult(flowable, false);
97 }
98
99 @Test
100 public void withEmpty2Flowable() {
101 Flowable<Boolean> flowable = Flowable.sequenceEqual(
102 Flowable.just("one", "two", "three"),
103 Flowable.<String> empty()).toFlowable();
104 verifyResult(flowable, false);
105 }
106
107 @Test
108 public void withEmpty3Flowable() {
109 Flowable<Boolean> flowable = Flowable.sequenceEqual(
110 Flowable.<String> empty(), Flowable.<String> empty()).toFlowable();
111 verifyResult(flowable, true);
112 }
113
114 @Test
115 public void withEqualityErrorFlowable() {
116 Flowable<Boolean> flowable = Flowable.sequenceEqual(
117 Flowable.just("one"), Flowable.just("one"),
118 new BiPredicate<String, String>() {
119 @Override
120 public boolean test(String t1, String t2) {
121 throw new TestException();
122 }
123 }).toFlowable();
124 verifyError(flowable);
125 }
126
127 @Test
128 public void one() {
129 Single<Boolean> single = Flowable.sequenceEqual(
130 Flowable.just("one", "two", "three"),
131 Flowable.just("one", "two", "three"));
132 verifyResult(single, true);
133 }
134
135 @Test
136 public void two() {
137 Single<Boolean> single = Flowable.sequenceEqual(
138 Flowable.just("one", "two", "three"),
139 Flowable.just("one", "two", "three", "four"));
140 verifyResult(single, false);
141 }
142
143 @Test
144 public void three() {
145 Single<Boolean> single = Flowable.sequenceEqual(
146 Flowable.just("one", "two", "three", "four"),
147 Flowable.just("one", "two", "three"));
148 verifyResult(single, false);
149 }
150
151 @Test
152 public void withError1() {
153 Single<Boolean> single = Flowable.sequenceEqual(
154 Flowable.concat(Flowable.just("one"),
155 Flowable.<String> error(new TestException())),
156 Flowable.just("one", "two", "three"));
157 verifyError(single);
158 }
159
160 @Test
161 public void withError2() {
162 Single<Boolean> single = Flowable.sequenceEqual(
163 Flowable.just("one", "two", "three"),
164 Flowable.concat(Flowable.just("one"),
165 Flowable.<String> error(new TestException())));
166 verifyError(single);
167 }
168
169 @Test
170 public void withError3() {
171 Single<Boolean> single = Flowable.sequenceEqual(
172 Flowable.concat(Flowable.just("one"),
173 Flowable.<String> error(new TestException())),
174 Flowable.concat(Flowable.just("one"),
175 Flowable.<String> error(new TestException())));
176 verifyError(single);
177 }
178
179 @Test
180 public void withEmpty1() {
181 Single<Boolean> single = Flowable.sequenceEqual(
182 Flowable.<String> empty(),
183 Flowable.just("one", "two", "three"));
184 verifyResult(single, false);
185 }
186
187 @Test
188 public void withEmpty2() {
189 Single<Boolean> single = Flowable.sequenceEqual(
190 Flowable.just("one", "two", "three"),
191 Flowable.<String> empty());
192 verifyResult(single, false);
193 }
194
195 @Test
196 public void withEmpty3() {
197 Single<Boolean> single = Flowable.sequenceEqual(
198 Flowable.<String> empty(), Flowable.<String> empty());
199 verifyResult(single, true);
200 }
201
202 @Test
203 public void withEqualityError() {
204 Single<Boolean> single = Flowable.sequenceEqual(
205 Flowable.just("one"), Flowable.just("one"),
206 new BiPredicate<String, String>() {
207 @Override
208 public boolean test(String t1, String t2) {
209 throw new TestException();
210 }
211 });
212 verifyError(single);
213 }
214
215 private void verifyResult(Flowable<Boolean> flowable, boolean result) {
216 Subscriber<Boolean> subscriber = TestHelper.mockSubscriber();
217
218 flowable.subscribe(subscriber);
219
220 InOrder inOrder = inOrder(subscriber);
221 inOrder.verify(subscriber, times(1)).onNext(result);
222 inOrder.verify(subscriber).onComplete();
223 inOrder.verifyNoMoreInteractions();
224 }
225
226 private void verifyResult(Single<Boolean> single, boolean result) {
227 SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
228
229 single.subscribe(observer);
230
231 InOrder inOrder = inOrder(observer);
232 inOrder.verify(observer, times(1)).onSuccess(result);
233 inOrder.verifyNoMoreInteractions();
234 }
235
236 private void verifyError(Flowable<Boolean> flowable) {
237 Subscriber<Boolean> subscriber = TestHelper.mockSubscriber();
238 flowable.subscribe(subscriber);
239
240 InOrder inOrder = inOrder(subscriber);
241 inOrder.verify(subscriber, times(1)).onError(isA(TestException.class));
242 inOrder.verifyNoMoreInteractions();
243 }
244
245 private void verifyError(Single<Boolean> single) {
246 SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
247 single.subscribe(observer);
248
249 InOrder inOrder = inOrder(observer);
250 inOrder.verify(observer, times(1)).onError(isA(TestException.class));
251 inOrder.verifyNoMoreInteractions();
252 }
253
254 @Test
255 public void prefetch() {
256
257 Flowable.sequenceEqual(Flowable.range(1, 20), Flowable.range(1, 20), 2)
258 .test()
259 .assertResult(true);
260 }
261
262 @Test
263 public void disposed() {
264 TestHelper.checkDisposed(Flowable.sequenceEqual(Flowable.just(1), Flowable.just(2)));
265 }
266
267 @Test
268 public void simpleInequal() {
269 Flowable.sequenceEqual(Flowable.just(1), Flowable.just(2))
270 .test()
271 .assertResult(false);
272 }
273
274 @Test
275 public void simpleInequalObservable() {
276 Flowable.sequenceEqual(Flowable.just(1), Flowable.just(2))
277 .toFlowable()
278 .test()
279 .assertResult(false);
280 }
281
282 @Test
283 public void onNextCancelRace() {
284 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
285 final PublishProcessor<Integer> pp = PublishProcessor.create();
286
287 final TestObserver<Boolean> to = Flowable.sequenceEqual(Flowable.never(), pp).test();
288
289 Runnable r1 = new Runnable() {
290 @Override
291 public void run() {
292 to.dispose();
293 }
294 };
295
296 Runnable r2 = new Runnable() {
297 @Override
298 public void run() {
299 pp.onNext(1);
300 }
301 };
302
303 TestHelper.race(r1, r2);
304
305 to.assertEmpty();
306 }
307 }
308
309 @Test
310 public void onNextCancelRaceObservable() {
311 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
312 final PublishProcessor<Integer> pp = PublishProcessor.create();
313
314 final TestSubscriber<Boolean> ts = Flowable.sequenceEqual(Flowable.never(), pp).toFlowable().test();
315
316 Runnable r1 = new Runnable() {
317 @Override
318 public void run() {
319 ts.cancel();
320 }
321 };
322
323 Runnable r2 = new Runnable() {
324 @Override
325 public void run() {
326 pp.onNext(1);
327 }
328 };
329
330 TestHelper.race(r1, r2);
331
332 ts.assertEmpty();
333 }
334 }
335
336 @Test
337 public void disposedFlowable() {
338 TestHelper.checkDisposed(Flowable.sequenceEqual(Flowable.just(1), Flowable.just(2)).toFlowable());
339 }
340
341 @Test
342 public void prefetchFlowable() {
343 Flowable.sequenceEqual(Flowable.range(1, 20), Flowable.range(1, 20), 2)
344 .toFlowable()
345 .test()
346 .assertResult(true);
347 }
348
349 @Test
350 public void longSequenceEqualsFlowable() {
351 Flowable<Integer> source = Flowable.range(1, Flowable.bufferSize() * 4).subscribeOn(Schedulers.computation());
352
353 Flowable.sequenceEqual(source, source)
354 .toFlowable()
355 .test()
356 .awaitDone(5, TimeUnit.SECONDS)
357 .assertResult(true);
358 }
359
360 @Test
361 public void syncFusedCrashFlowable() {
362 Flowable<Integer> source = Flowable.range(1, 10).map(new Function<Integer, Integer>() {
363 @Override
364 public Integer apply(Integer v) throws Exception { throw new TestException(); }
365 });
366
367 Flowable.sequenceEqual(source, Flowable.range(1, 10).hide())
368 .toFlowable()
369 .test()
370 .assertFailure(TestException.class);
371
372 Flowable.sequenceEqual(Flowable.range(1, 10).hide(), source)
373 .toFlowable()
374 .test()
375 .assertFailure(TestException.class);
376 }
377
378 @Test
379 public void cancelAndDrainRaceFlowable() {
380 Flowable<Object> neverNever = new Flowable<Object>() {
381 @Override
382 protected void subscribeActual(Subscriber<? super Object> s) {
383 }
384 };
385
386 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
387 final TestSubscriber<Boolean> ts = new TestSubscriber<>();
388
389 final PublishProcessor<Integer> pp = PublishProcessor.create();
390
391 boolean swap = (i & 1) == 0;
392
393 Flowable.sequenceEqual(swap ? pp : neverNever, swap ? neverNever : pp)
394 .toFlowable()
395 .subscribe(ts);
396
397 Runnable r1 = new Runnable() {
398 @Override
399 public void run() {
400 pp.onNext(1);
401 }
402 };
403
404 Runnable r2 = new Runnable() {
405 @Override
406 public void run() {
407 ts.cancel();
408 }
409 };
410
411 TestHelper.race(r1, r2);
412
413 ts.assertEmpty();
414 }
415 }
416
417 @Test
418 public void sourceOverflowsFlowable() {
419 Flowable.sequenceEqual(Flowable.never(), new Flowable<Object>() {
420 @Override
421 protected void subscribeActual(Subscriber<? super Object> s) {
422 s.onSubscribe(new BooleanSubscription());
423 for (int i = 0; i < 10; i++) {
424 s.onNext(i);
425 }
426 }
427 }, 8)
428 .toFlowable()
429 .test()
430 .assertFailure(MissingBackpressureException.class);
431 }
432
433 @Test
434 public void doubleErrorFlowable() {
435 List<Throwable> errors = TestHelper.trackPluginErrors();
436 try {
437 Flowable.sequenceEqual(Flowable.never(), new Flowable<Object>() {
438 @Override
439 protected void subscribeActual(Subscriber<? super Object> s) {
440 s.onSubscribe(new BooleanSubscription());
441 s.onError(new TestException("First"));
442 s.onError(new TestException("Second"));
443 }
444 }, 8)
445 .toFlowable()
446 .to(TestHelper.<Boolean>testConsumer())
447 .assertFailureAndMessage(TestException.class, "First");
448
449 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
450 } finally {
451 RxJavaPlugins.reset();
452 }
453 }
454
455 @Test
456 public void longSequenceEquals() {
457 Flowable<Integer> source = Flowable.range(1, Flowable.bufferSize() * 4).subscribeOn(Schedulers.computation());
458
459 Flowable.sequenceEqual(source, source)
460 .test()
461 .awaitDone(5, TimeUnit.SECONDS)
462 .assertResult(true);
463 }
464
465 @Test
466 public void syncFusedCrash() {
467 Flowable<Integer> source = Flowable.range(1, 10).map(new Function<Integer, Integer>() {
468 @Override
469 public Integer apply(Integer v) throws Exception { throw new TestException(); }
470 });
471
472 Flowable.sequenceEqual(source, Flowable.range(1, 10).hide())
473 .test()
474 .assertFailure(TestException.class);
475
476 Flowable.sequenceEqual(Flowable.range(1, 10).hide(), source)
477 .test()
478 .assertFailure(TestException.class);
479 }
480
481 @Test
482 public void cancelAndDrainRace() {
483 Flowable<Object> neverNever = new Flowable<Object>() {
484 @Override
485 protected void subscribeActual(Subscriber<? super Object> s) {
486 }
487 };
488
489 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
490 final TestObserver<Boolean> to = new TestObserver<>();
491
492 final PublishProcessor<Integer> pp = PublishProcessor.create();
493
494 boolean swap = (i & 1) == 0;
495
496 Flowable.sequenceEqual(swap ? pp : neverNever, swap ? neverNever : pp)
497 .subscribe(to);
498
499 Runnable r1 = new Runnable() {
500 @Override
501 public void run() {
502 pp.onNext(1);
503 }
504 };
505
506 Runnable r2 = new Runnable() {
507 @Override
508 public void run() {
509 to.dispose();
510 }
511 };
512
513 TestHelper.race(r1, r2);
514
515 to.assertEmpty();
516 }
517 }
518
519 @Test
520 public void sourceOverflows() {
521 Flowable.sequenceEqual(Flowable.never(), new Flowable<Object>() {
522 @Override
523 protected void subscribeActual(Subscriber<? super Object> s) {
524 s.onSubscribe(new BooleanSubscription());
525 for (int i = 0; i < 10; i++) {
526 s.onNext(i);
527 }
528 }
529 }, 8)
530 .test()
531 .assertFailure(MissingBackpressureException.class);
532 }
533
534 @Test
535 public void doubleError() {
536 List<Throwable> errors = TestHelper.trackPluginErrors();
537 try {
538 Flowable.sequenceEqual(Flowable.never(), new Flowable<Object>() {
539 @Override
540 protected void subscribeActual(Subscriber<? super Object> s) {
541 s.onSubscribe(new BooleanSubscription());
542 s.onError(new TestException("First"));
543 s.onError(new TestException("Second"));
544 }
545 }, 8)
546 .to(TestHelper.<Boolean>testConsumer())
547 .assertFailureAndMessage(TestException.class, "First");
548
549 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
550 } finally {
551 RxJavaPlugins.reset();
552 }
553 }
554
555 @Test
556 public void undeliverableUponCancel() {
557 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Single<Boolean>>() {
558 @Override
559 public Single<Boolean> apply(Flowable<Integer> upstream) {
560 return Flowable.sequenceEqual(Flowable.just(1).hide(), upstream);
561 }
562 });
563 }
564
565 @Test
566 public void undeliverableUponCancelAsFlowable() {
567 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Boolean>>() {
568 @Override
569 public Flowable<Boolean> apply(Flowable<Integer> upstream) {
570 return Flowable.sequenceEqual(Flowable.just(1).hide(), upstream).toFlowable();
571 }
572 });
573 }
574
575 @Test
576 public void undeliverableUponCancel2() {
577 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Single<Boolean>>() {
578 @Override
579 public Single<Boolean> apply(Flowable<Integer> upstream) {
580 return Flowable.sequenceEqual(upstream, Flowable.just(1).hide());
581 }
582 });
583 }
584
585 @Test
586 public void undeliverableUponCancelAsFlowable2() {
587 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Boolean>>() {
588 @Override
589 public Flowable<Boolean> apply(Flowable<Integer> upstream) {
590 return Flowable.sequenceEqual(upstream, Flowable.just(1).hide()).toFlowable();
591 }
592 });
593 }
594
595 @Test
596 public void fusionRejected() {
597 Flowable.sequenceEqual(TestHelper.rejectFlowableFusion(), Flowable.never())
598 .test()
599 .assertEmpty();
600 }
601
602 @Test
603 public void fusionRejectedFlowable() {
604 Flowable.sequenceEqual(TestHelper.rejectFlowableFusion(), Flowable.never())
605 .toFlowable()
606 .test()
607 .assertEmpty();
608 }
609
610 @Test
611 public void asyncSourceCompare() {
612 Flowable.sequenceEqual(Flowable.fromCallable(() -> 1), Flowable.just(1))
613 .test()
614 .assertResult(true);
615 }
616 }