Skip to content

Commit

Permalink
Test methods in subscribe proxies (#160)
Browse files Browse the repository at this point in the history
* Add test helpers to subscribeProxies

* Replace TestObservers & TestSubscribers with calls to test() in proxies.

* Fix formatting

* Fix checkstyle violations in completableScoper

* Change the javadocs to mirror the other methods in the proxies

* Fix checkstyle violations
  • Loading branch information
VisheshVadhera authored and ZacSweers committed Jan 18, 2018
1 parent 4eb6478 commit 61098a2
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.TestObserver;

/**
* Entry point for auto-disposing {@link Completable}s.
Expand Down Expand Up @@ -80,6 +81,21 @@ public CompletableScoper(Maybe<?> lifecycle) {
@Override public <E extends CompletableObserver> E subscribeWith(E observer) {
return new AutoDisposeCompletable(maybeSource, scope()).subscribeWith(observer);
}

@Override public TestObserver<Void> test() {
TestObserver<Void> observer = new TestObserver<>();
subscribe(observer);
return observer;
}

@Override public TestObserver<Void> test(boolean cancel) {
TestObserver<Void> observer = new TestObserver<>();
if (cancel) {
observer.cancel();
}
subscribe(observer);
return observer;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.TestObserver;

/**
* Subscribe proxy that matches {@link Completable}'s subscribe overloads.
Expand Down Expand Up @@ -60,4 +61,18 @@ public interface CompletableSubscribeProxy {
* @return a {@link CompletableObserver}
*/
@CheckReturnValue <E extends CompletableObserver> E subscribeWith(E observer);

/**
* Proxy for {@link Completable#test()}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<Void> test();

/**
* Proxy for {@link Completable#test(boolean)}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<Void> test(boolean cancel);
}
23 changes: 23 additions & 0 deletions autodispose/src/main/java/com/uber/autodispose/FlowableScoper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.subscribers.TestSubscriber;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
Expand Down Expand Up @@ -97,6 +99,27 @@ public Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwab
@Override public <E extends Subscriber<? super T>> E subscribeWith(E observer) {
return new AutoDisposeFlowable<>(source, scope()).subscribeWith(observer);
}

@Override public TestSubscriber<T> test() {
TestSubscriber<T> subscriber = new TestSubscriber<>();
subscribe(subscriber);
return subscriber;
}

@Override public TestSubscriber<T> test(long initialRequest) {
TestSubscriber<T> subscriber = new TestSubscriber<>(initialRequest);
subscribe(subscriber);
return subscriber;
}

@Override public TestSubscriber<T> test(long initialRequest, boolean cancel) {
TestSubscriber<T> subscriber = new TestSubscriber<>(initialRequest);
if (cancel) {
subscriber.cancel();
}
subscribe(subscriber);
return subscriber;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.subscribers.TestSubscriber;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

Expand Down Expand Up @@ -77,4 +79,25 @@ Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onE
* @return an {@link Subscriber}
*/
@CheckReturnValue <E extends Subscriber<? super T>> E subscribeWith(E observer);

/**
* Proxy for {@link Flowable#test()}.
*
* @return a {@link TestSubscriber}
*/
@CheckReturnValue TestSubscriber<T> test();

/**
* Proxy for {@link Flowable#test(long)}.
*
* @return a {@link TestSubscriber}
*/
@CheckReturnValue TestSubscriber<T> test(long initialRequest);

/**
* Proxy for {@link Flowable#test(long, boolean)}.
*
* @return a {@link TestSubscriber}
*/
@CheckReturnValue TestSubscriber<T> test(long initialRequest, boolean cancel);
}
17 changes: 17 additions & 0 deletions autodispose/src/main/java/com/uber/autodispose/MaybeScoper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.TestObserver;

/**
* Entry point for auto-disposing {@link Maybe}s.
Expand Down Expand Up @@ -88,6 +89,22 @@ public Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwab
@Override public <E extends MaybeObserver<? super T>> E subscribeWith(E observer) {
return new AutoDisposeMaybe<>(maybeSource, scope()).subscribeWith(observer);
}

@Override public TestObserver<T> test() {
TestObserver<T> observer = new TestObserver<>();
subscribe(observer);
return observer;
}

@Override public TestObserver<T> test(boolean cancel) {
TestObserver<T> observer = new TestObserver<>();

if (cancel) {
observer.cancel();
}
subscribe(observer);
return observer;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.TestObserver;

/**
* Subscribe proxy that matches {@link Maybe}'s subscribe overloads.
Expand Down Expand Up @@ -68,4 +69,18 @@ Disposable subscribe(Consumer<? super T> onSuccess, Consumer<? super Throwable>
* @return a {@link MaybeObserver}
*/
@CheckReturnValue <E extends MaybeObserver<? super T>> E subscribeWith(E observer);

/**
* Proxy for {@link Maybe#test()}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<T> test();

/**
* Proxy for {@link Maybe#test(boolean)}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<T> test(boolean cancel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.TestObserver;

/**
* Entry point for auto-disposing {@link Observable}s.
Expand Down Expand Up @@ -97,6 +98,21 @@ public Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwab
@Override public <E extends Observer<? super T>> E subscribeWith(E observer) {
return new AutoDisposeObservable<>(observableSource, scope()).subscribeWith(observer);
}

@Override public TestObserver<T> test() {
TestObserver<T> observer = new TestObserver<>();
subscribe(observer);
return observer;
}

@Override public TestObserver<T> test(boolean dispose) {
TestObserver<T> observer = new TestObserver<>();
if (dispose) {
observer.dispose();
}
subscribe(observer);
return observer;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.TestObserver;

/**
* Subscribe proxy that matches {@link Observable}'s subscribe overloads.
Expand Down Expand Up @@ -77,4 +78,18 @@ Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwable> onE
* @return an {@link Observer}
*/
@CheckReturnValue <E extends Observer<? super T>> E subscribeWith(E observer);

/**
* Proxy for {@link Observable#test()}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<T> test();

/**
* Proxy for {@link Observable#test(boolean)}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<T> test(boolean dispose);
}
17 changes: 17 additions & 0 deletions autodispose/src/main/java/com/uber/autodispose/SingleScoper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.reactivex.functions.BiConsumer;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.TestObserver;

/**
* Entry point for auto-disposing {@link Single}s.
Expand Down Expand Up @@ -87,6 +88,22 @@ public Disposable subscribe(Consumer<? super T> onNext, Consumer<? super Throwab
@Override public <E extends SingleObserver<? super T>> E subscribeWith(E observer) {
return new AutoDisposeSingle<>(singleSource, scope()).subscribeWith(observer);
}

@Override public TestObserver<T> test() {
TestObserver<T> observer = new TestObserver<>();
subscribe(observer);
return observer;
}

@Override public TestObserver<T> test(boolean cancel) {
TestObserver<T> observer = new TestObserver<>();

if (cancel) {
observer.cancel();
}
subscribe(observer);
return observer;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiConsumer;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.TestObserver;

/**
* Subscribe proxy that matches {@link Single}'s subscribe overloads.
Expand Down Expand Up @@ -67,4 +68,18 @@ public interface SingleSubscribeProxy<T> {
* @return a {@link SingleObserver}
*/
@CheckReturnValue <E extends SingleObserver<? super T>> E subscribeWith(E observer);

/**
* Proxy for {@link Single#test()}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<T> test();

/**
* Proxy for {@link Single#test(boolean)}.
*
* @return a {@link TestObserver}
*/
@CheckReturnValue TestObserver<T> test(boolean cancel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ public class AutoDisposeCompletableObserverTest {
@Override public void accept(OutsideLifecycleException e) { }
});
BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
TestObserver<Integer> o = new TestObserver<>();
LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
CompletableSubject source = CompletableSubject.create();
source.as(autoDisposable(provider))
.subscribe(o);
TestObserver<Void> o = source
.as(autoDisposable(provider))
.test();

assertThat(source.hasObservers()).isFalse();
assertThat(lifecycle.hasObservers()).isFalse();
Expand All @@ -248,11 +248,11 @@ public class AutoDisposeCompletableObserverTest {
lifecycle.onNext(1);
lifecycle.onNext(2);
lifecycle.onNext(3);
TestObserver<Integer> o = new TestObserver<>();
LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
CompletableSubject source = CompletableSubject.create();
source.as(autoDisposable(provider))
.subscribe(o);
TestObserver<Void> o = source
.as(autoDisposable(provider))
.test();

assertThat(source.hasObservers()).isFalse();
assertThat(lifecycle.hasObservers()).isFalse();
Expand All @@ -269,11 +269,10 @@ public class AutoDisposeCompletableObserverTest {
}
});
BehaviorSubject<Integer> lifecycle = BehaviorSubject.create();
TestObserver<Integer> o = new TestObserver<>();
LifecycleScopeProvider<Integer> provider = TestUtil.makeLifecycleProvider(lifecycle);
CompletableSubject source = CompletableSubject.create();
source.as(autoDisposable(provider))
.subscribe(o);
TestObserver<Void> o = CompletableSubject.create()
.as(autoDisposable(provider))
.test();

o.assertNoValues();
o.assertError(new Predicate<Throwable>() {
Expand Down Expand Up @@ -347,23 +346,21 @@ public CompletableObserver apply(Completable source, CompletableObserver observe
}

@Test public void autoDispose_withScopeProviderCompleted_shouldNotReportDoubleSubscriptions() {
TestObserver<Object> o = new TestObserver<>();
CompletableSubject.create()
.as(autoDisposable(ScopeProvider.UNBOUND))
.subscribe(o);
TestObserver<Void> o = CompletableSubject.create()
.as(autoDisposable(ScopeProvider.UNBOUND))
.test();
o.assertNoValues();
o.assertNoErrors();

rule.assertNoErrors();
}

@Test public void unbound_shouldStillPassValues() {
TestObserver<Object> o = new TestObserver<>();
CompletableSubject s = CompletableSubject.create();
s.as(autoDisposable(ScopeProvider.UNBOUND))
.subscribe(o);
TestObserver<Void> o = CompletableSubject.create()
.as(autoDisposable(ScopeProvider.UNBOUND))
.test();

s.onComplete();
o.onComplete();
o.assertComplete();
}
}
Loading

0 comments on commit 61098a2

Please sign in to comment.