@Test(expected = NullPointerException.class) public void switchOnNextNull() { Flowable.switchOnNext(null); }
@Override public Publisher<Long> createPublisher(long elements) { return Flowable.switchOnNext(Flowable.just( Flowable.fromIterable(iterate(elements))) ) ; } }
@Test public void testSwitchWhenOuterCompleteBeforeInner() { Flowable<Flowable<String>> source = Flowable.unsafeCreate(new Publisher<Flowable<String>>() { @Override public void subscribe(Subscriber<? super Flowable<String>> subscriber) { subscriber.onSubscribe(new BooleanSubscription()); publishNext(subscriber, 50, Flowable.unsafeCreate(new Publisher<String>() { @Override public void subscribe(Subscriber<? super String> subscriber) { subscriber.onSubscribe(new BooleanSubscription()); publishNext(subscriber, 70, "one"); publishNext(subscriber, 100, "two"); publishCompleted(subscriber, 200); } })); publishCompleted(subscriber, 60); } }); Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber); InOrder inOrder = inOrder(subscriber); scheduler.advanceTimeTo(350, TimeUnit.MILLISECONDS); inOrder.verify(subscriber, times(2)).onNext(anyString()); inOrder.verify(subscriber, times(1)).onComplete(); }
@Test public void testUnsubscribe() { final AtomicBoolean isUnsubscribed = new AtomicBoolean(); Flowable.switchOnNext( Flowable.unsafeCreate(new Publisher<Flowable<Integer>>() { @Override public void subscribe(final Subscriber<? super Flowable<Integer>> subscriber) { BooleanSubscription bs = new BooleanSubscription(); subscriber.onSubscribe(bs); subscriber.onNext(Flowable.just(1)); isUnsubscribed.set(bs.isCancelled()); } }) ).take(1).subscribe(); assertTrue("Switch doesn't propagate 'unsubscribe'", isUnsubscribed.get()); } /** The upstream producer hijacked the switch producer stopping the requests aimed at the inner observables. */
Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber);
Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber);
Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber);
Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber);
Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber);
@Test public void switchOnNextPrefetch() { final List<Integer> list = new ArrayList<Integer>(); Flowable<Integer> source = Flowable.range(1, 10).hide().doOnNext(new Consumer<Integer>() { @Override public void accept(Integer v) throws Exception { list.add(v); } }); Flowable.switchOnNext(Flowable.just(source).hide(), 2) .test(1); assertEquals(Arrays.asList(1, 2, 3), list); }
Flowable<String> sampled = Flowable.switchOnNext(source); sampled.subscribe(subscriber);
@Test public void dispose() { TestHelper.checkDisposed(Flowable.switchOnNext( Flowable.just(Flowable.just(1)).hide())); }
@Test public void scalarMap() { Flowable.switchOnNext(Flowable.just(Flowable.just(1))) .test() .assertResult(1); }
@Test(timeout = 10000) @Ignore("Request pattern changed and I can't decide if this is okay or not") public void testSecondaryRequestsAdditivelyAreMoreThanLongMaxValueInducesMaxValueRequestFromUpstream() throws InterruptedException { final List<Long> requests = new CopyOnWriteArrayList<Long>(); TestSubscriber<Long> ts = new TestSubscriber<Long>(1L); Flowable.switchOnNext( Flowable.interval(100, TimeUnit.MILLISECONDS) .map(new Function<Long, Flowable<Long>>() { @Override public Flowable<Long> apply(Long t) { return Flowable.fromIterable(Arrays.asList(1L, 2L, 3L)) .doOnRequest(new LongConsumer() { @Override public void accept(long v) { requests.add(v); } }); } }).take(3)).subscribe(ts); // we will miss two of the first observables Thread.sleep(250); ts.request(Long.MAX_VALUE - 1); ts.request(Long.MAX_VALUE - 1); ts.awaitTerminalEvent(); assertTrue(ts.valueCount() > 0); System.out.println(requests); assertEquals(5, requests.size()); assertEquals(Long.MAX_VALUE, (long) requests.get(requests.size() - 1)); }
@Test(timeout = 10000) public void testSecondaryRequestsDontOverflow() throws InterruptedException { TestSubscriber<Long> ts = new TestSubscriber<Long>(0L); Flowable.switchOnNext( Flowable.interval(100, TimeUnit.MILLISECONDS) .map(new Function<Long, Flowable<Long>>() { @Override public Flowable<Long> apply(Long t) { return Flowable.fromIterable(Arrays.asList(1L, 2L, 3L)).hide(); } }).take(3)).subscribe(ts); ts.request(1); //we will miss two of the first observable Thread.sleep(250); ts.request(Long.MAX_VALUE - 1); ts.request(Long.MAX_VALUE - 1); ts.awaitTerminalEvent(); ts.assertValueCount(7); }
@Test(timeout = 10000) public void testInitialRequestsAreAdditive() { TestSubscriber<Long> ts = new TestSubscriber<Long>(0L); Flowable.switchOnNext( Flowable.interval(100, TimeUnit.MILLISECONDS) .map( new Function<Long, Flowable<Long>>() { @Override public Flowable<Long> apply(Long t) { return Flowable.just(1L, 2L, 3L); } } ).take(3)) .subscribe(ts); ts.request(Long.MAX_VALUE - 100); ts.request(1); ts.awaitTerminalEvent(); }
@Test(timeout = 10000) public void testInitialRequestsDontOverflow() { TestSubscriber<Long> ts = new TestSubscriber<Long>(0L); Flowable.switchOnNext( Flowable.interval(100, TimeUnit.MILLISECONDS) .map(new Function<Long, Flowable<Long>>() { @Override public Flowable<Long> apply(Long t) { return Flowable.fromIterable(Arrays.asList(1L, 2L, 3L)).hide(); } }).take(3)).subscribe(ts); ts.request(Long.MAX_VALUE - 1); ts.request(2); ts.awaitTerminalEvent(); assertTrue(ts.valueCount() > 0); }
Flowable.switchOnNext(o).subscribe(new DefaultSubscriber<String>() {