@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 autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithExp() { 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); SingleSubject<Integer> source = SingleSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); o.assertNoValues(); o.assertError(throwable -> throwable instanceof IllegalStateException && throwable.getCause() instanceof OutsideScopeException); } }
@Test public void unbound_shouldStillPassValues() { SingleSubject<Integer> s = SingleSubject.create(); TestObserver<Integer> o = s.as(autoDisposable(ScopeProvider.UNBOUND)) .test(); s.onSuccess(1); o.assertValue(1); }
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.create(); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); SingleSubject<Integer> source = SingleSubject.create(); TestObserver<Integer> 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<Object> o = SingleSubject.create() .as(autoDisposable(ScopeProvider.UNBOUND)) .test(); o.assertNoValues(); o.assertNoErrors(); rule.assertNoErrors(); }
@Test public void autoDispose_withMaybe_normal() { 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(); // Got the event source.onSuccess(1); assertThat(o.takeSuccess()).isEqualTo(1); // Nothing more, scope disposed too o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@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(); }
@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 autoDispose_withProvider() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); SingleSubject<Integer> source = SingleSubject.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.onSuccess(3); o.takeSuccess(); // All cleaned up o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@Test public void autoDispose_withProvider_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); SingleSubject<Integer> source = SingleSubject.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(); // Lifecycle ends scope.onComplete(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); // No one is listening even if upstream finally does emit source.onSuccess(3); o.assertNoMoreEvents(); }
@Test public void autoDispose_withLifecycleProvider() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); SingleSubject<Integer> source = SingleSubject.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.onSuccess(3); o.takeSuccess(); // All cleaned up o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); }
@Test public void autoDispose_withLifecycleProvider_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); SingleSubject<Integer> source = SingleSubject.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 ends lifecycle.onNext(3); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); // No one is listening even if upstream finally does emit source.onSuccess(3); 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); SingleSubject<Integer> source = SingleSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Test public void disposed() { TestHelper.checkDisposed(SingleSubject.<Integer>create() .as(Singles.flatMapObservable( new Function<Integer, Observable<Integer>>() { @Override public Observable<Integer> apply(Integer e) throws Exception { return Observable.just(2); } }, new Function<Throwable, Observable<Integer>>() { @Override public Observable<Integer> apply(Throwable e) throws Exception { return Observable.error(e); } } )) ); }
@Test public void disposed() { TestHelper.checkDisposed(SingleSubject.<Integer>create() .as(Singles.flatMapCompletable( new Function<Integer, Completable>() { @Override public Completable apply(Integer e) throws Exception { return Completable.complete(); } }, new Function<Throwable, Completable>() { @Override public Completable apply(Throwable e) throws Exception { return Completable.error(e); } } )) ); }
@Test public void disposed() { TestHelper.checkDisposed(SingleSubject.<Integer>create() .as(Singles.flatMapFlowable( new Function<Integer, Flowable<Integer>>() { @Override public Flowable<Integer> apply(Integer e) throws Exception { return Flowable.just(2); } }, new Function<Throwable, Flowable<Integer>>() { @Override public Flowable<Integer> apply(Throwable e) throws Exception { return Flowable.error(e); } } )) ); }
@Test public void disposed() { TestHelper.checkDisposed(SingleSubject.<Integer>create() .as(Singles.flatMapMaybe( new Function<Integer, Maybe<Integer>>() { @Override public Maybe<Integer> apply(Integer e) throws Exception { return Maybe.just(2); } }, new Function<Throwable, Maybe<Integer>>() { @Override public Maybe<Integer> apply(Throwable e) throws Exception { return Maybe.error(e); } } )) ); }