@Override public Publisher<Object> apply(Flowable<Object> f) throws Exception { return f.doFinally(FlowableDoFinallyTest.this).filter(Functions.alwaysTrue()); } });
@Test public void testTakeFirstWithPredicateOfSome() { Flowable<Integer> flowable = Flowable.just(1, 3, 5, 4, 6, 3); flowable.filter(IS_EVEN).take(1).subscribe(w); verify(w, times(1)).onNext(anyInt()); verify(w).onNext(4); verify(w, times(1)).onComplete(); verify(w, never()).onError(any(Throwable.class)); }
@Test public void testFirstOrElseWithPredicateOfSomeFlowable() { Flowable<String> src = Flowable.just("a", "b", "c", "d", "e", "f"); src.filter(IS_D).first("default").toFlowable().subscribe(w); verify(w, times(1)).onNext(anyString()); verify(w, times(1)).onNext("d"); verify(w, never()).onError(any(Throwable.class)); verify(w, times(1)).onComplete(); }
@Test public void testFirstWithPredicateOfNoneMatchingThePredicate() { Flowable<Integer> flowable = Flowable.just(1, 3, 5, 7, 9, 7, 5, 3, 1); flowable.filter(IS_EVEN).firstElement().subscribe(wm); verify(wm, never()).onSuccess(anyInt()); verify(wm, times(1)).onComplete(); verify(wm, never()).onError(isA(NoSuchElementException.class)); }
@Test public void nullConditional() { Flowable.fromIterable(Arrays.asList(1, null, 3, 4, 5)) .filter(Functions.alwaysTrue()) .test() .assertFailure(NullPointerException.class, 1); }
@Test public void conditionalOneIsNullSlowPath() { Flowable.fromArray(new Integer[] { null, 1 }) .filter(Functions.alwaysTrue()) .test(2L) .assertFailure(NullPointerException.class); }
@Test public void emptyConditional() { Flowable.<Integer>empty() .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribeWith(ts) .assertResult(); assertTrue(values.isEmpty()); }
@Test public void rangeConditional() { Flowable.range(1, 5) .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribeWith(ts) .assertResult(1, 2, 3, 4, 5); assertEquals(Arrays.asList(1, -1, 2, -2, 3, -3, 4, -4, 5, -5), values); }
@Test public void syncNoneFused2() { TestSubscriber<Integer> ts = SubscriberFusion.newTest(QueueFuseable.ANY); Flowable.range(1, 5) .filter(Functions.alwaysFalse()) .filter(Functions.alwaysFalse()) .subscribe(ts); ts.assertOf(SubscriberFusion.<Integer>assertFuseable()) .assertOf(SubscriberFusion.<Integer>assertFusionMode(QueueFuseable.SYNC)) .assertResult(); }
@Test public void conditionalOneByOne() { Flowable.fromArray(new Integer[] { 1, 2, 3, 4, 5 }) .filter(Functions.alwaysTrue()) .rebatchRequests(1) .test() .assertResult(1, 2, 3, 4, 5); }
@Test public void slowPathTakeExact() { Flowable.range(1, 5) .filter(Functions.alwaysTrue()) .take(5) .test() .assertResult(1, 2, 3, 4, 5); }
@Test public void conditionalSlowPathRebatch() { Flowable.rangeLong(1L, 5L) .filter(Functions.alwaysTrue()) .rebatchRequests(1) .test() .assertResult(1L, 2L, 3L, 4L, 5L); }
@Test public void asyncFusedRejectedConditional() { TestSubscriber<Integer> ts0 = SubscriberFusion.newTest(QueueFuseable.ASYNC); Flowable.range(1, 5) .doAfterNext(afterNext) .filter(Functions.alwaysTrue()) .subscribe(ts0); SubscriberFusion.assertFusion(ts0, QueueFuseable.NONE) .assertResult(1, 2, 3, 4, 5); assertEquals(Arrays.asList(-1, -2, -3, -4, -5), values); }
@Test public void request1Conditional() { Flowable.range(1, 10).hide() .observeOn(ImmediateThinScheduler.INSTANCE) .filter(Functions.alwaysTrue()) .test(1L) .assertValue(1); }
@Test public void normalEmptyConditional() { Flowable.empty() .doFinally(this) .filter(Functions.alwaysTrue()) .test() .assertResult(); assertEquals(1, calls); }
@Test public void workerNotDisposedPrematurelyNormalInNormalOutConditional() { DisposeTrackingScheduler s = new DisposeTrackingScheduler(); Flowable.concat( Flowable.just(1).hide().observeOn(s).filter(Functions.alwaysTrue()), Flowable.just(2) ) .test() .assertResult(1, 2); assertEquals(1, s.disposedCount.get()); }
@Test public void normalTakeConditional() { Flowable.range(1, 10) .doFinally(this) .filter(Functions.alwaysTrue()) .take(5) .test() .assertResult(1, 2, 3, 4, 5); assertEquals(1, calls); }
@Test public void backFusedErrorConditional() { TestSubscriber<Integer> ts = SubscriberFusion.newTest(QueueFuseable.ANY); Flowable.<Integer>error(new TestException()) .observeOn(ImmediateThinScheduler.INSTANCE) .filter(Functions.alwaysTrue()) .subscribe(ts); SubscriberFusion.assertFusion(ts, QueueFuseable.ASYNC) .assertFailure(TestException.class); }
@Test public void syncFusedBoundaryConditional() { TestSubscriber<Integer> ts = SubscriberFusion.newTest(QueueFuseable.SYNC | QueueFuseable.BOUNDARY); Flowable.range(1, 5) .doFinally(this) .filter(Functions.alwaysTrue()) .subscribe(ts); SubscriberFusion.assertFusion(ts, QueueFuseable.NONE) .assertResult(1, 2, 3, 4, 5); assertEquals(1, calls); }
@Test public void backFusedConditional() { TestSubscriber<Integer> ts = SubscriberFusion.newTest(QueueFuseable.ANY); Flowable.range(1, 100).hide() .observeOn(ImmediateThinScheduler.INSTANCE) .filter(Functions.alwaysTrue()) .subscribe(ts); SubscriberFusion.assertFusion(ts, QueueFuseable.ASYNC) .assertValueCount(100) .assertComplete() .assertNoErrors(); }