@Test(expected = NullPointerException.class) public void ambVarargsNull() { Observable.ambArray((Observable<Object>[])null); }
/** * Mirrors the ObservableSource (current or provided) that first either emits an item or sends a termination * notification. * <p> * <img width="640" height="385" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/amb.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code ambWith} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param other * an ObservableSource competing to react first. A subscription to this provided source will occur after * subscribing to the current source. * @return an Observable that emits the same sequence as whichever of the source ObservableSources first * emitted an item or sent a termination notification * @see <a href="http://reactivex.io/documentation/operators/amb.html">ReactiveX operators documentation: Amb</a> */ @SuppressWarnings("unchecked") @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable<T> ambWith(ObservableSource<? extends T> other) { ObjectHelper.requireNonNull(other, "other is null"); return ambArray(this, other); }
/** * Mirrors the ObservableSource (current or provided) that first either emits an item or sends a termination * notification. * <p> * <img width="640" height="385" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/amb.png" alt=""> * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code ambWith} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * * @param other * an ObservableSource competing to react first. A subscription to this provided source will occur after * subscribing to the current source. * @return an Observable that emits the same sequence as whichever of the source ObservableSources first * emitted an item or sent a termination notification * @see <a href="http://reactivex.io/documentation/operators/amb.html">ReactiveX operators documentation: Amb</a> */ @SuppressWarnings("unchecked") @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final Observable<T> ambWith(ObservableSource<? extends T> other) { ObjectHelper.requireNonNull(other, "other is null"); return ambArray(this, other); }
@SuppressWarnings("unchecked") @Test public void ambArrayEmpty() { assertSame(Observable.empty(), Observable.ambArray()); }
@SuppressWarnings("unchecked") @Test(expected = NullPointerException.class) public void ambVarargsOneIsNull() { Observable.ambArray(Observable.never(), null).blockingLast(); }
@Test public void testAmb2() { IOException expectedException = new IOException( "fake exception"); Observable<String> observable1 = createObservable(new String[] {}, 2000, new IOException("fake exception")); Observable<String> observable2 = createObservable(new String[] { "2", "22", "222", "2222" }, 1000, expectedException); Observable<String> observable3 = createObservable(new String[] {}, 3000, new IOException("fake exception")); @SuppressWarnings("unchecked") Observable<String> o = Observable.ambArray(observable1, observable2, observable3); Observer<String> observer = TestHelper.mockObserver(); o.subscribe(observer); scheduler.advanceTimeBy(100000, TimeUnit.MILLISECONDS); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext("2"); inOrder.verify(observer, times(1)).onNext("22"); inOrder.verify(observer, times(1)).onNext("222"); inOrder.verify(observer, times(1)).onNext("2222"); inOrder.verify(observer, times(1)).onError(expectedException); inOrder.verifyNoMoreInteractions(); }
@SuppressWarnings("unchecked") @Test public void disposed() { TestHelper.checkDisposed(Observable.ambArray(Observable.never(), Observable.never())); }
@SuppressWarnings("unchecked") @Test public void ambArraySingleElement() { assertSame(Observable.never(), Observable.ambArray(Observable.never())); }
@Test public void testAmb() { Observable<String> observable1 = createObservable(new String[] { "1", "11", "111", "1111" }, 2000, null); Observable<String> observable2 = createObservable(new String[] { "2", "22", "222", "2222" }, 1000, null); Observable<String> observable3 = createObservable(new String[] { "3", "33", "333", "3333" }, 3000, null); @SuppressWarnings("unchecked") Observable<String> o = Observable.ambArray(observable1, observable2, observable3); Observer<String> observer = TestHelper.mockObserver(); o.subscribe(observer); scheduler.advanceTimeBy(100000, TimeUnit.MILLISECONDS); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onNext("2"); inOrder.verify(observer, times(1)).onNext("22"); inOrder.verify(observer, times(1)).onNext("222"); inOrder.verify(observer, times(1)).onNext("2222"); inOrder.verify(observer, times(1)).onComplete(); inOrder.verifyNoMoreInteractions(); }
@SuppressWarnings("unchecked") @Test public void ambArrayOrder() { Observable<Integer> error = Observable.error(new RuntimeException()); Observable.ambArray(Observable.just(1), error).test().assertValue(1).assertComplete(); } }
@Test public void testAmb3() { Observable<String> observable1 = createObservable(new String[] { "1" }, 2000, null); Observable<String> observable2 = createObservable(new String[] {}, 1000, null); Observable<String> observable3 = createObservable(new String[] { "3" }, 3000, null); @SuppressWarnings("unchecked") Observable<String> o = Observable.ambArray(observable1, observable2, observable3); Observer<String> observer = TestHelper.mockObserver(); o.subscribe(observer); scheduler.advanceTimeBy(100000, TimeUnit.MILLISECONDS); InOrder inOrder = inOrder(observer); inOrder.verify(observer, times(1)).onComplete(); inOrder.verifyNoMoreInteractions(); }
@Test public void onCompleteRace() { for (int i = 0; i < 500; i++) { final PublishSubject<Integer> ps1 = PublishSubject.create(); final PublishSubject<Integer> ps2 = PublishSubject.create(); @SuppressWarnings("unchecked") TestObserver<Integer> to = Observable.ambArray(ps1, ps2).test(); Runnable r1 = new Runnable() { @Override public void run() { ps1.onComplete(); } }; Runnable r2 = new Runnable() { @Override public void run() { ps2.onComplete(); } }; TestCommonHelper.race(r1, r2, Schedulers.single()); to.assertResult(); } }
@SuppressWarnings("unchecked") @Test public void testSubscriptionOnlyHappensOnce() throws InterruptedException { final AtomicLong count = new AtomicLong(); Consumer<Disposable> incrementer = new Consumer<Disposable>() { @Override public void accept(Disposable s) { count.incrementAndGet(); } }; //this aync stream should emit first Observable<Integer> o1 = Observable.just(1).doOnSubscribe(incrementer) .delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); //this stream emits second Observable<Integer> o2 = Observable.just(1).doOnSubscribe(incrementer) .delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation()); TestObserver<Integer> ts = new TestObserver<Integer>(); Observable.ambArray(o1, o2).subscribe(ts); ts.awaitTerminalEvent(5, TimeUnit.SECONDS); ts.assertNoErrors(); assertEquals(2, count.get()); }
TestObserver<Integer> to = Observable.ambArray(ps1, ps2).test();
@Test public void onNextRace() { for (int i = 0; i < 500; i++) { final PublishSubject<Integer> ps1 = PublishSubject.create(); final PublishSubject<Integer> ps2 = PublishSubject.create(); @SuppressWarnings("unchecked") TestObserver<Integer> to = Observable.ambArray(ps1, ps2).test(); Runnable r1 = new Runnable() { @Override public void run() { ps1.onNext(1); } }; Runnable r2 = new Runnable() { @Override public void run() { ps2.onNext(1); } }; TestCommonHelper.race(r1, r2, Schedulers.single()); to.assertSubscribed().assertNoErrors() .assertNotComplete().assertValueCount(1); } }
@SuppressWarnings("unchecked") @Test public void testAmbCancelsOthers() { PublishSubject<Integer> source1 = PublishSubject.create(); PublishSubject<Integer> source2 = PublishSubject.create(); PublishSubject<Integer> source3 = PublishSubject.create(); TestObserver<Integer> ts = new TestObserver<Integer>(); Observable.ambArray(source1, source2, source3).subscribe(ts); assertTrue("Source 1 doesn't have subscribers!", source1.hasObservers()); assertTrue("Source 2 doesn't have subscribers!", source2.hasObservers()); assertTrue("Source 3 doesn't have subscribers!", source3.hasObservers()); source1.onNext(1); assertTrue("Source 1 doesn't have subscribers!", source1.hasObservers()); assertFalse("Source 2 still has subscribers!", source2.hasObservers()); assertFalse("Source 2 still has subscribers!", source3.hasObservers()); }