@Test public void complete() { CompletableSubject cs = CompletableSubject.create(); assertFalse(cs.hasComplete()); assertFalse(cs.hasThrowable()); assertNull(cs.getThrowable()); assertFalse(cs.hasObservers()); assertEquals(0, cs.observerCount()); TestObserver<Void> to = cs.test(); to.assertEmpty(); assertTrue(cs.hasObservers()); assertEquals(1, cs.observerCount()); cs.onComplete(); assertTrue(cs.hasComplete()); assertFalse(cs.hasThrowable()); assertNull(cs.getThrowable()); assertFalse(cs.hasObservers()); assertEquals(0, cs.observerCount()); to.assertResult(); cs.test().assertResult(); assertTrue(cs.hasComplete()); assertFalse(cs.hasThrowable()); assertNull(cs.getThrowable()); assertFalse(cs.hasObservers()); assertEquals(0, cs.observerCount()); }
@Test public void error() { CompletableSubject cs = CompletableSubject.create(); assertFalse(cs.hasComplete()); assertFalse(cs.hasThrowable()); assertNull(cs.getThrowable()); assertFalse(cs.hasObservers()); assertEquals(0, cs.observerCount()); TestObserver<Void> to = cs.test(); to.assertEmpty(); assertTrue(cs.hasObservers()); assertEquals(1, cs.observerCount()); cs.onError(new IOException()); assertFalse(cs.hasComplete()); assertTrue(cs.hasThrowable()); assertTrue(cs.getThrowable().toString(), cs.getThrowable() instanceof IOException); assertFalse(cs.hasObservers()); assertEquals(0, cs.observerCount()); to.assertFailure(IOException.class); cs.test().assertFailure(IOException.class); assertFalse(cs.hasComplete()); assertTrue(cs.hasThrowable()); assertTrue(cs.getThrowable().toString(), cs.getThrowable() instanceof IOException); assertFalse(cs.hasObservers()); assertEquals(0, cs.observerCount()); }
/** * Creates a fresh CompletableSubject. * @return the new CompletableSubject instance */ @CheckReturnValue @NonNull public static CompletableSubject create() { return new CompletableSubject(); }
@Test public void untilCompletableOtherOnComplete() { CompletableSubject main = CompletableSubject.create(); CompletableSubject other = CompletableSubject.create(); TestObserver<Void> to = main.ambWith(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); other.onComplete(); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertResult(); }
@Test public void untilCompletableOtherError() { CompletableSubject main = CompletableSubject.create(); CompletableSubject other = CompletableSubject.create(); TestObserver<Void> to = main.ambWith(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); other.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertFailure(TestException.class); }
@Test public void disposed() { TestHelper.checkDisposed(CompletableSubject.create().doOnComplete(Functions.EMPTY_ACTION)); } }
@Test public void scalarSource() { CompletableSubject cs = CompletableSubject.create(); TestObserver<Void> to = Observable.just(1) .switchMapCompletable(Functions.justFunction(cs)) .test(); assertTrue(cs.hasObservers()); to.assertEmpty(); cs.onComplete(); to.assertResult(); } }
@Test public void autoDispose_withMaybe_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); CompletableSubject source = CompletableSubject.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.onComplete(); o.assertNoMoreEvents(); }
@Test public void cancelOther() { CompletableSubject other = CompletableSubject.create(); TestObserver<Object> to = Observable.empty() .concatWith(other) .test(); assertTrue(other.hasObservers()); to.cancel(); assertFalse(other.hasObservers()); }
@Test public void untilCompletableOtherError() { SingleSubject<Integer> main = SingleSubject.create(); CompletableSubject other = CompletableSubject.create(); TestObserver<Integer> to = main.takeUntil(other).test(); assertTrue("Main no observers?", main.hasObservers()); assertTrue("Other no observers?", other.hasObservers()); other.onError(new TestException()); assertFalse("Main has observers?", main.hasObservers()); assertFalse("Other has observers?", other.hasObservers()); to.assertFailure(TestException.class); }
@Test public void cancelOther() { CompletableSubject cs = CompletableSubject.create(); PublishSubject<Integer> ps = PublishSubject.create(); TestObserver<Integer> to = cs.andThen(ps) .test(); assertTrue(cs.hasObservers()); assertFalse(ps.hasObservers()); cs.onComplete(); assertFalse(cs.hasObservers()); assertTrue(ps.hasObservers()); to.cancel(); assertFalse(cs.hasObservers()); assertFalse(ps.hasObservers()); }
@Test public void endError() { PublishSubject<Integer> ps = PublishSubject.create(); final CompletableSubject cs = CompletableSubject.create(); final CompletableSubject cs2 = CompletableSubject.create(); assertFalse(cs.hasObservers()); assertTrue(cs.hasObservers()); cs.onError(new TestException()); cs2.onComplete();
@Test public void asyncFused() { final PublishSubject<Integer> ps = PublishSubject.create(); final CompletableSubject cs = CompletableSubject.create(); final TestObserver<Void> to = ps.observeOn(ImmediateThinScheduler.INSTANCE) .concatMapCompletable( Functions.justFunction(cs) ) .test(); ps.onNext(1); ps.onComplete(); cs.onComplete(); to.assertResult(); }
@Test public void otherCompletes() { CompletableSubject cs1 = CompletableSubject.create(); CompletableSubject cs2 = CompletableSubject.create(); TestObserver<Void> to = cs1.takeUntil(cs2).test(); to.assertEmpty(); assertTrue(cs1.hasObservers()); assertTrue(cs2.hasObservers()); cs2.onComplete(); assertFalse(cs1.hasObservers()); assertFalse(cs2.hasObservers()); to.assertResult(); }
@Test public void once() { CompletableSubject cs = CompletableSubject.create(); TestObserver<Void> to = cs.test(); cs.onComplete(); List<Throwable> errors = TestHelper.trackPluginErrors(); try { cs.onError(new IOException()); TestHelper.assertUndeliverable(errors, 0, IOException.class); } finally { RxJavaPlugins.reset(); } cs.onComplete(); to.assertResult(); }
@Test public void autoDispose_outsideScope_withProviderAndNoOpPlugin_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { }); ScopeProvider provider = outsideScopeProvider(); CompletableSubject source = CompletableSubject.create(); TestObserver<Void> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Test public void mainErrors() { CompletableSubject cs1 = CompletableSubject.create(); CompletableSubject cs2 = CompletableSubject.create(); TestObserver<Void> to = cs1.takeUntil(cs2).test(); to.assertEmpty(); assertTrue(cs1.hasObservers()); assertTrue(cs2.hasObservers()); cs1.onError(new TestException()); assertFalse(cs1.hasObservers()); assertFalse(cs2.hasObservers()); to.assertFailure(TestException.class); }
@Test public void errorMain() { CompletableSubject cs = CompletableSubject.create(); PublishSubject<Integer> ps = PublishSubject.create(); TestObserver<Integer> to = cs.andThen(ps) .test(); assertTrue(cs.hasObservers()); assertFalse(ps.hasObservers()); cs.onError(new TestException()); assertFalse(cs.hasObservers()); assertFalse(ps.hasObservers()); to.assertFailure(TestException.class); }