@Test(expected = NullPointerException.class) public void ambArrayNull() { Single.ambArray((Single<Integer>[])null); }
/** * Signals the event of this or the other SingleSource whichever signals first. * <p> * <img width="640" height="463" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.ambWith.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 the other SingleSource to race for the first emission of success or error * @return the new Single instance. A subscription to this provided source will occur after subscribing * to the current source. * @since 2.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public final Single<T> ambWith(SingleSource<? extends T> other) { ObjectHelper.requireNonNull(other, "other is null"); return ambArray(this, other); }
/** * Signals the event of this or the other SingleSource whichever signals first. * <dl> * <dt><b>Scheduler:</b></dt> * <dd>{@code ambWith} does not operate by default on a particular {@link Scheduler}.</dd> * </dl> * @param other the other SingleSource to race for the first emission of success or error * @return the new Single instance. A subscription to this provided source will occur after subscribing * to the current source. * @since 2.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") public final Single<T> ambWith(SingleSource<? extends T> other) { ObjectHelper.requireNonNull(other, "other is null"); return ambArray(this, other); }
@SuppressWarnings("unchecked") @Test public void ambArrayOneIsNull() { Single.ambArray(null, just1) .test() .assertError(NullPointerException.class); }
@SuppressWarnings("unchecked") @Test public void ambArrayEmpty() { Single.ambArray() .test() .assertFailure(NoSuchElementException.class); }
@SuppressWarnings("unchecked") @Test public void ambArrayOrder() { Single<Integer> error = Single.error(new RuntimeException()); Single.ambArray(Single.just(1), error).test().assertValue(1); }
@SuppressWarnings("unchecked") @Test public void ambSingleSource() { assertSame(Single.never(), Single.ambArray(Single.never())); }
@SuppressWarnings("unchecked") @Test public void noWinnerSuccessDispose() throws Exception { for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { final AtomicBoolean interrupted = new AtomicBoolean(); final CountDownLatch cdl = new CountDownLatch(1); Single.ambArray( Single.just(1) .subscribeOn(Schedulers.single()) .observeOn(Schedulers.computation()), Single.never() ) .subscribe(new BiConsumer<Object, Throwable>() { @Override public void accept(Object v, Throwable e) throws Exception { assertNotNull(v); assertNull(e); interrupted.set(Thread.currentThread().isInterrupted()); cdl.countDown(); } }); assertTrue(cdl.await(500, TimeUnit.SECONDS)); assertFalse("Interrupted!", interrupted.get()); } }
@SuppressWarnings("unchecked") @Test public void noWinnerErrorDispose() throws Exception { final TestException ex = new TestException(); for (int i = 0; i < TestHelper.RACE_LONG_LOOPS; i++) { final AtomicBoolean interrupted = new AtomicBoolean(); final CountDownLatch cdl = new CountDownLatch(1); Single.ambArray( Single.error(ex) .subscribeOn(Schedulers.single()) .observeOn(Schedulers.computation()), Single.never() ) .subscribe(new BiConsumer<Object, Throwable>() { @Override public void accept(Object v, Throwable e) throws Exception { assertNull(v); assertNotNull(e); interrupted.set(Thread.currentThread().isInterrupted()); cdl.countDown(); } }); assertTrue(cdl.await(500, TimeUnit.SECONDS)); assertFalse("Interrupted!", interrupted.get()); } } }
@SuppressWarnings("unchecked") @Test public void error() { Single.ambArray(Single.error(new TestException()), Single.just(1)) .test() .assertFailure(TestException.class); }
@Test public void nullSourceSuccessRace() { for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { List<Throwable> errors = TestHelper.trackPluginErrors(); try { final Subject<Integer> ps = ReplaySubject.create(); ps.onNext(1); @SuppressWarnings("unchecked") final Single<Integer> source = Single.ambArray(ps.singleOrError(), Single.<Integer>never(), Single.<Integer>never(), null); Runnable r1 = new Runnable() { @Override public void run() { source.test(); } }; Runnable r2 = new Runnable() { @Override public void run() { ps.onComplete(); } }; TestHelper.race(r1, r2); if (!errors.isEmpty()) { TestHelper.assertError(errors, 0, NullPointerException.class); } } finally { RxJavaPlugins.reset(); } } }
final Subject<Integer> ps2 = PublishSubject.create(); Single.ambArray(ps1.singleOrError(), ps2.singleOrError()).test();
final Subject<Integer> ps2 = PublishSubject.create(); Single.ambArray(ps1.singleOrError(), ps2.singleOrError()).test();