Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReplaySubject - Manual Merge of Pull 218 #241

Merged
Prev Previous commit
Next Next commit
Beef up UnsubscribeTester
johngmyers committed Mar 31, 2013
commit cce874510408305fed6e01ab5b70ab753f9ac693
72 changes: 35 additions & 37 deletions rxjava-core/src/main/java/rx/subjects/RepeatSubject.java
Original file line number Diff line number Diff line change
@@ -2,10 +2,13 @@

import org.junit.Test;
import org.mockito.Mockito;
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
import rx.testing.UnsubscribeTester;
import rx.util.functions.Action1;
import rx.util.functions.Func0;
import rx.util.functions.Func1;

import java.util.ArrayList;
@@ -266,43 +269,38 @@ private void assertObservedUntilTwo(Observer<String> aObserver)
}

@Test
public void testUnsubscribeFromOnNext() {
RepeatSubject<Object> subject = RepeatSubject.create();

UnsubscribeTester test1 = UnsubscribeTester.createOnNext(subject);
UnsubscribeTester test2 = UnsubscribeTester.createOnNext(subject);

subject.onNext("one");

test1.assertPassed();
test2.assertPassed();
}

@Test
public void testUnsubscribeFromOnCompleted() {
RepeatSubject<Object> subject = RepeatSubject.create();

UnsubscribeTester test1 = UnsubscribeTester.createOnCompleted(subject);
UnsubscribeTester test2 = UnsubscribeTester.createOnCompleted(subject);

subject.onCompleted();

test1.assertPassed();
test2.assertPassed();
}

@Test
public void testUnsubscribeFromOnError() {
RepeatSubject<Object> subject = RepeatSubject.create();

UnsubscribeTester test1 = UnsubscribeTester.createOnError(subject);
UnsubscribeTester test2 = UnsubscribeTester.createOnError(subject);

subject.onError(new Exception());

test1.assertPassed();
test2.assertPassed();
public void testUnsubscribe()
{
UnsubscribeTester.test(new Func0<RepeatSubject<Object>>()
{
@Override
public RepeatSubject<Object> call()
{
return RepeatSubject.create();
}
}, new Action1<RepeatSubject<Object>>()
{
@Override
public void call(RepeatSubject<Object> repeatSubject)
{
repeatSubject.onCompleted();
}
}, new Action1<RepeatSubject<Object>>()
{
@Override
public void call(RepeatSubject<Object> repeatSubject)
{
repeatSubject.onError(new Exception());
}
}, new Action1<RepeatSubject<Object>>()
{
@Override
public void call(RepeatSubject<Object> repeatSubject)
{
repeatSubject.onNext("one");
}
}
);
}

}
}
53 changes: 46 additions & 7 deletions rxjava-core/src/main/java/rx/testing/UnsubscribeTester.java
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.util.functions.Action1;
import rx.util.functions.Func0;

import static org.junit.Assert.assertTrue;

@@ -12,14 +14,52 @@ public class UnsubscribeTester

public UnsubscribeTester() {}

public static <T> UnsubscribeTester createOnNext(Observable<T> observable)
/**
* Tests the unsubscription semantics of an observable.
*
* @param provider Function that when called provides an instance of the observable being tested
* @param generateOnCompleted Causes an observer generated by @param provider to generate an onCompleted event. Null to not test onCompleted.
* @param generateOnError Causes an observer generated by @param provider to generate an onError event. Null to not test onError.
* @param generateOnNext Causes an observer generated by @param provider to generate an onNext event. Null to not test onNext.
* @param <T> The type of object passed by the Observable
*/
public static <T, O extends Observable<T>> void test(Func0<O> provider, Action1<? super O> generateOnCompleted, Action1<? super O> generateOnError, Action1<? super O> generateOnNext)
{
if (generateOnCompleted != null) {
O observable = provider.call();
UnsubscribeTester tester1 = createOnCompleted(observable);
UnsubscribeTester tester2 = createOnCompleted(observable);
generateOnCompleted.call(observable);
tester1.assertPassed();
tester2.assertPassed();
}
if (generateOnError != null) {
O observable = provider.call();
UnsubscribeTester tester1 = createOnError(observable);
UnsubscribeTester tester2 = createOnError(observable);
generateOnError.call(observable);
tester1.assertPassed();
tester2.assertPassed();
}
if (generateOnNext != null) {
O observable = provider.call();
UnsubscribeTester tester1 = createOnNext(observable);
UnsubscribeTester tester2 = createOnNext(observable);
generateOnNext.call(observable);
tester1.assertPassed();
tester2.assertPassed();
}
}

private static <T> UnsubscribeTester createOnCompleted(Observable<T> observable)
{
final UnsubscribeTester test = new UnsubscribeTester();
test.setSubscription(observable.subscribe(new Observer<T>()
{
@Override
public void onCompleted()
{
test.doUnsubscribe();
}

@Override
@@ -30,26 +70,25 @@ public void onError(Exception e)
@Override
public void onNext(T args)
{
test.doUnsubscribe();
}
}));
return test;
}

public static <T> UnsubscribeTester createOnCompleted(Observable<T> observable)
private static <T> UnsubscribeTester createOnError(Observable<T> observable)
{
final UnsubscribeTester test = new UnsubscribeTester();
test.setSubscription(observable.subscribe(new Observer<T>()
{
@Override
public void onCompleted()
{
test.doUnsubscribe();
}

@Override
public void onError(Exception e)
{
test.doUnsubscribe();
}

@Override
@@ -60,7 +99,7 @@ public void onNext(T args)
return test;
}

public static <T> UnsubscribeTester createOnError(Observable<T> observable)
private static <T> UnsubscribeTester createOnNext(Observable<T> observable)
{
final UnsubscribeTester test = new UnsubscribeTester();
test.setSubscription(observable.subscribe(new Observer<T>()
@@ -73,12 +112,12 @@ public void onCompleted()
@Override
public void onError(Exception e)
{
test.doUnsubscribe();
}

@Override
public void onNext(T args)
{
test.doUnsubscribe();
}
}));
return test;
@@ -96,7 +135,7 @@ private void doUnsubscribe()
subscription.unsubscribe();
}

public void assertPassed()
private void assertPassed()
{
assertTrue("expected notification was received", subscription == null);
}