@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<Void> o = CompletableSubject.create() .as(autoDisposable(provider)) .test(); o.assertNoValues(); o.assertError(throwable -> throwable instanceof IllegalStateException && throwable.getCause() instanceof OutsideScopeException); } }
@Test public void autoDispose_withProviderAndPlugin_withoutStarting_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); }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.create(); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); TestObserver<Void> o = CompletableSubject.create() .as(autoDisposable(provider)) .test(); o.assertNoValues(); o.assertError(throwable -> throwable instanceof IllegalStateException && throwable.getCause() instanceof OutsideScopeException); } }
@Test public void unbound_shouldStillPassValues() { TestObserver<Void> o = CompletableSubject.create() .as(autoDisposable(ScopeProvider.UNBOUND)) .test(); o.onComplete(); o.assertComplete(); }
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.create(); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); CompletableSubject source = CompletableSubject.create(); TestObserver<Void> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Test public void autoDispose_withScopeProviderCompleted_shouldNotReportDoubleSubscriptions() { TestObserver<Void> o = CompletableSubject.create() .as(autoDisposable(ScopeProvider.UNBOUND)) .test(); o.assertNoValues(); o.assertNoErrors(); rule.assertNoErrors(); }
@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 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 autoDispose_withMaybe_normal() { 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(); // Got the event source.onComplete(); o.assertOnComplete(); // Nothing more, scope disposed too o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@Test public void autoDispose_withProvider_completion() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); CompletableSubject source = CompletableSubject.create(); CompletableSubject scope = CompletableSubject.create(); ScopeProvider provider = makeProvider(scope); source.as(autoDisposable(provider)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); source.onComplete(); o.assertOnComplete(); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@Test public void autoDispose_withLifecycleProvider_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); CompletableSubject source = CompletableSubject.create(); BehaviorSubject<Integer> lifecycle = BehaviorSubject.createDefault(0); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); source.as(autoDisposable(provider)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(lifecycle.hasObservers()).isTrue(); lifecycle.onNext(1); assertThat(source.hasObservers()).isTrue(); assertThat(lifecycle.hasObservers()).isTrue(); lifecycle.onNext(3); // All disposed assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); // No one is listening source.onComplete(); o.assertNoMoreEvents(); }
@Test public void autoDispose_withLifecycleProvider_completion() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); CompletableSubject source = CompletableSubject.create(); BehaviorSubject<Integer> lifecycle = BehaviorSubject.createDefault(0); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); source.as(autoDisposable(provider)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(lifecycle.hasObservers()).isTrue(); lifecycle.onNext(1); assertThat(source.hasObservers()).isTrue(); assertThat(lifecycle.hasObservers()).isTrue(); source.onComplete(); o.assertOnComplete(); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); }
@Test public void autoDispose_withProvider_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); CompletableSubject source = CompletableSubject.create(); CompletableSubject scope = CompletableSubject.create(); ScopeProvider provider = makeProvider(scope); source.as(autoDisposable(provider)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); scope.onComplete(); // All disposed assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); // No one is listening source.onComplete(); o.assertNoMoreEvents(); }
@Test public void autoDispose_withProviderAndNoOpPlugin_afterEnding_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { // Noop }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.createDefault(0); lifecycle.onNext(1); lifecycle.onNext(2); lifecycle.onNext(3); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); CompletableSubject source = CompletableSubject.create(); TestObserver<Void> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Test public void disposed() { TestHelper.checkDisposed(CompletableSubject.create() .as(Completables.flatMapMaybe( new Callable<Maybe<Integer>>() { @Override public Maybe<Integer> call() throws Exception { return Maybe.empty(); } }, new Function<Throwable, Maybe<Integer>>() { @Override public Maybe<Integer> apply(Throwable e) throws Exception { return Maybe.error(e); } } )) ); }
@Test public void disposed() { TestHelper.checkDisposed(CompletableSubject.create() .as(Completables.flatMapObservable( new Callable<Observable<Integer>>() { @Override public Observable<Integer> call() throws Exception { return Observable.empty(); } }, new Function<Throwable, Observable<Integer>>() { @Override public Observable<Integer> apply(Throwable e) throws Exception { return Observable.error(e); } } )) ); }
@Test public void disposed() { TestHelper.checkDisposed(CompletableSubject.create() .as(Completables.flatMapFlowable( new Callable<Flowable<Integer>>() { @Override public Flowable<Integer> call() throws Exception { return Flowable.empty(); } }, new Function<Throwable, Flowable<Integer>>() { @Override public Flowable<Integer> apply(Throwable e) throws Exception { return Flowable.error(e); } } )) ); }
@Test public void disposed() { TestHelper.checkDisposed(CompletableSubject.create() .as(Completables.flatMapSingle( new Callable<Single<Integer>>() { @Override public Single<Integer> call() throws Exception { return Single.just(1); } }, new Function<Throwable, Single<Integer>>() { @Override public Single<Integer> apply(Throwable e) throws Exception { return Single.error(e); } } )) ); }