@Test public void untilSingleMainSuccess() { SingleSubject<Integer> main = SingleSubject.create(); SingleSubject<Integer> other = SingleSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); main.onSuccess(1); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertResult(1); }
@Test public void untilSingleMainError() { SingleSubject<Integer> main = SingleSubject.create(); SingleSubject<Integer> other = SingleSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); main.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertFailure(TestException.class); }
/** * Creates a fresh SingleSubject. * @param <T> the value type received and emitted * @return the new SingleSubject instance */ @CheckReturnValue @NonNull public static <T> SingleSubject<T> create() { return new SingleSubject<T>(); }
@Test public void autoDispose_withMaybe_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); SingleSubject<Integer> source = SingleSubject.create(); CompletableSubject scope = CompletableSubject.create(); source.as(autoDisposable(scope)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); // Lifecycle ends scope.onComplete(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); // Event if upstream emits, no one is listening source.onSuccess(2); o.assertNoMoreEvents(); }
@Test public void scalarSource() { SingleSubject<Integer> ss = SingleSubject.create(); TestObserver<Integer> to = Observable.just(1) .switchMapSingle(Functions.justFunction(ss)) .test(); assertTrue(ss.hasObservers()); to.assertEmpty(); ss.onSuccess(2); to.assertResult(2); } }
@Test public void untilSingleDispose() { SingleSubject<Integer> main = SingleSubject.create(); SingleSubject<Integer> other = SingleSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); to.dispose(); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertEmpty(); }
@Test public void onNextSlowPath() { final PublishProcessor<Integer> pp = PublishProcessor.create(); final SingleSubject<Integer> cs = SingleSubject.create(); TestSubscriber<Integer> ts = pp.mergeWith(cs).subscribeWith(new TestSubscriber<Integer>() { @Override public void onNext(Integer t) { super.onNext(t); if (t == 1) { pp.onNext(2); } } }); pp.onNext(1); cs.onSuccess(3); pp.onNext(4); pp.onComplete(); ts.assertResult(1, 2, 3, 4); }
@Test public void cancelOther() { SingleSubject<Object> other = SingleSubject.create(); TestSubscriber<Object> ts = Flowable.empty() .concatWith(other) .test(); assertTrue(other.hasObservers()); ts.cancel(); assertFalse(other.hasObservers()); }
@Test public void switchOver() { PublishProcessor<Integer> pp = PublishProcessor.create(); final SingleSubject<Integer> ms1 = SingleSubject.create(); final SingleSubject<Integer> ms2 = SingleSubject.create(); TestSubscriber<Integer> ts = pp.switchMapSingle(new Function<Integer, SingleSource<Integer>>() { @Override public SingleSource<Integer> apply(Integer v) throws Exception { if (v == 1) { return ms1; } return ms2; } }).test(); ts.assertEmpty(); pp.onNext(1); ts.assertEmpty(); assertTrue(ms1.hasObservers()); pp.onNext(2); assertFalse(ms1.hasObservers()); assertTrue(ms2.hasObservers()); ms2.onError(new TestException()); assertFalse(pp.hasSubscribers()); ts.assertFailure(TestException.class); }
@Test public void autoDispose_outsideScope_withProviderAndPlugin_shouldFailWithWrappedExp() { AutoDisposePlugins.setOutsideScopeHandler(e -> { // Wrap in an IllegalStateException so we can verify this is the exception we see on the // other side throw new IllegalStateException(e); }); ScopeProvider provider = outsideScopeProvider(); TestObserver<Integer> o = SingleSubject.<Integer>create().as(autoDisposable(provider)) .test(); o.assertNoValues(); o.assertError(throwable -> throwable instanceof IllegalStateException && throwable.getCause() instanceof OutsideScopeException); } }
@Test public void cancelOther() { SingleSubject<Integer> ss = SingleSubject.create(); PublishSubject<Integer> ps = PublishSubject.create(); TestObserver<Integer> to = ss.flatMapObservable(Functions.justFunction(ps)) .test(); assertTrue(ss.hasObservers()); assertFalse(ps.hasObservers()); ss.onSuccess(1); assertFalse(ss.hasObservers()); assertTrue(ps.hasObservers()); to.cancel(); assertFalse(ss.hasObservers()); assertFalse(ps.hasObservers()); }
@Test public void error() { SingleSubject<Integer> ss = SingleSubject.create(); assertFalse(ss.hasValue()); assertNull(ss.getValue()); assertFalse(ss.hasThrowable()); assertNull(ss.getThrowable()); assertFalse(ss.hasObservers()); assertEquals(0, ss.observerCount()); TestObserver<Integer> to = ss.test(); to.assertEmpty(); assertTrue(ss.hasObservers()); assertEquals(1, ss.observerCount()); ss.onError(new IOException()); assertFalse(ss.hasValue()); assertNull(ss.getValue()); assertTrue(ss.hasThrowable()); assertTrue(ss.getThrowable().toString(), ss.getThrowable() instanceof IOException); assertFalse(ss.hasObservers()); assertEquals(0, ss.observerCount()); to.assertFailure(IOException.class); ss.test().assertFailure(IOException.class); assertFalse(ss.hasValue()); assertNull(ss.getValue()); assertTrue(ss.hasThrowable()); assertTrue(ss.getThrowable().toString(), ss.getThrowable() instanceof IOException); assertFalse(ss.hasObservers()); assertEquals(0, ss.observerCount()); }
@Test public void once() { SingleSubject<Integer> ss = SingleSubject.create(); TestObserver<Integer> to = ss.test(); ss.onSuccess(1); ss.onSuccess(2); List<Throwable> errors = TestHelper.trackPluginErrors(); try { ss.onError(new IOException()); TestHelper.assertUndeliverable(errors, 0, IOException.class); } finally { RxJavaPlugins.reset(); } to.assertResult(1); }
@Test public void autoDispose_outsideScope_withProviderAndNoOpPlugin_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { }); ScopeProvider provider = outsideScopeProvider(); SingleSubject<Integer> source = SingleSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Override public void run() { ms.onSuccess(3); } };
@Test public void nullValue() { SingleSubject<Integer> ss = SingleSubject.create(); try { ss.onSuccess(null); fail("No NullPointerException thrown"); } catch (NullPointerException ex) { assertEquals("onSuccess called with null. Null values are generally not allowed in 2.x operators and sources.", ex.getMessage()); } ss.test().assertEmpty().cancel(); }
@Test public void onSubscribeDispose() { SingleSubject<Integer> ss = SingleSubject.create(); Disposable d = Disposables.empty(); ss.onSubscribe(d); assertFalse(d.isDisposed()); ss.onSuccess(1); d = Disposables.empty(); ss.onSubscribe(d); assertTrue(d.isDisposed()); }
@Test public void errorMain() { SingleSubject<Integer> ss = SingleSubject.create(); PublishSubject<Integer> ps = PublishSubject.create(); TestObserver<Integer> to = ss.flatMapObservable(Functions.justFunction(ps)) .test(); assertTrue(ss.hasObservers()); assertFalse(ps.hasObservers()); ss.onError(new TestException()); assertFalse(ss.hasObservers()); assertFalse(ps.hasObservers()); to.assertFailure(TestException.class); }