Skip to content

Commit cce8745

Browse files
committed
Beef up UnsubscribeTester
1 parent 12954a6 commit cce8745

File tree

2 files changed

+81
-44
lines changed

2 files changed

+81
-44
lines changed

rxjava-core/src/main/java/rx/subjects/RepeatSubject.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import org.junit.Test;
44
import org.mockito.Mockito;
5+
import rx.Observable;
56
import rx.Observer;
67
import rx.Subscription;
78
import rx.subscriptions.Subscriptions;
89
import rx.testing.UnsubscribeTester;
10+
import rx.util.functions.Action1;
11+
import rx.util.functions.Func0;
912
import rx.util.functions.Func1;
1013

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

268271
@Test
269-
public void testUnsubscribeFromOnNext() {
270-
RepeatSubject<Object> subject = RepeatSubject.create();
271-
272-
UnsubscribeTester test1 = UnsubscribeTester.createOnNext(subject);
273-
UnsubscribeTester test2 = UnsubscribeTester.createOnNext(subject);
274-
275-
subject.onNext("one");
276-
277-
test1.assertPassed();
278-
test2.assertPassed();
279-
}
280-
281-
@Test
282-
public void testUnsubscribeFromOnCompleted() {
283-
RepeatSubject<Object> subject = RepeatSubject.create();
284-
285-
UnsubscribeTester test1 = UnsubscribeTester.createOnCompleted(subject);
286-
UnsubscribeTester test2 = UnsubscribeTester.createOnCompleted(subject);
287-
288-
subject.onCompleted();
289-
290-
test1.assertPassed();
291-
test2.assertPassed();
292-
}
293-
294-
@Test
295-
public void testUnsubscribeFromOnError() {
296-
RepeatSubject<Object> subject = RepeatSubject.create();
297-
298-
UnsubscribeTester test1 = UnsubscribeTester.createOnError(subject);
299-
UnsubscribeTester test2 = UnsubscribeTester.createOnError(subject);
300-
301-
subject.onError(new Exception());
302-
303-
test1.assertPassed();
304-
test2.assertPassed();
272+
public void testUnsubscribe()
273+
{
274+
UnsubscribeTester.test(new Func0<RepeatSubject<Object>>()
275+
{
276+
@Override
277+
public RepeatSubject<Object> call()
278+
{
279+
return RepeatSubject.create();
280+
}
281+
}, new Action1<RepeatSubject<Object>>()
282+
{
283+
@Override
284+
public void call(RepeatSubject<Object> repeatSubject)
285+
{
286+
repeatSubject.onCompleted();
287+
}
288+
}, new Action1<RepeatSubject<Object>>()
289+
{
290+
@Override
291+
public void call(RepeatSubject<Object> repeatSubject)
292+
{
293+
repeatSubject.onError(new Exception());
294+
}
295+
}, new Action1<RepeatSubject<Object>>()
296+
{
297+
@Override
298+
public void call(RepeatSubject<Object> repeatSubject)
299+
{
300+
repeatSubject.onNext("one");
301+
}
302+
}
303+
);
305304
}
306-
307305
}
308306
}

rxjava-core/src/main/java/rx/testing/UnsubscribeTester.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import rx.Observable;
44
import rx.Observer;
55
import rx.Subscription;
6+
import rx.util.functions.Action1;
7+
import rx.util.functions.Func0;
68

79
import static org.junit.Assert.assertTrue;
810

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

1315
public UnsubscribeTester() {}
1416

15-
public static <T> UnsubscribeTester createOnNext(Observable<T> observable)
17+
/**
18+
* Tests the unsubscription semantics of an observable.
19+
*
20+
* @param provider Function that when called provides an instance of the observable being tested
21+
* @param generateOnCompleted Causes an observer generated by @param provider to generate an onCompleted event. Null to not test onCompleted.
22+
* @param generateOnError Causes an observer generated by @param provider to generate an onError event. Null to not test onError.
23+
* @param generateOnNext Causes an observer generated by @param provider to generate an onNext event. Null to not test onNext.
24+
* @param <T> The type of object passed by the Observable
25+
*/
26+
public static <T, O extends Observable<T>> void test(Func0<O> provider, Action1<? super O> generateOnCompleted, Action1<? super O> generateOnError, Action1<? super O> generateOnNext)
27+
{
28+
if (generateOnCompleted != null) {
29+
O observable = provider.call();
30+
UnsubscribeTester tester1 = createOnCompleted(observable);
31+
UnsubscribeTester tester2 = createOnCompleted(observable);
32+
generateOnCompleted.call(observable);
33+
tester1.assertPassed();
34+
tester2.assertPassed();
35+
}
36+
if (generateOnError != null) {
37+
O observable = provider.call();
38+
UnsubscribeTester tester1 = createOnError(observable);
39+
UnsubscribeTester tester2 = createOnError(observable);
40+
generateOnError.call(observable);
41+
tester1.assertPassed();
42+
tester2.assertPassed();
43+
}
44+
if (generateOnNext != null) {
45+
O observable = provider.call();
46+
UnsubscribeTester tester1 = createOnNext(observable);
47+
UnsubscribeTester tester2 = createOnNext(observable);
48+
generateOnNext.call(observable);
49+
tester1.assertPassed();
50+
tester2.assertPassed();
51+
}
52+
}
53+
54+
private static <T> UnsubscribeTester createOnCompleted(Observable<T> observable)
1655
{
1756
final UnsubscribeTester test = new UnsubscribeTester();
1857
test.setSubscription(observable.subscribe(new Observer<T>()
1958
{
2059
@Override
2160
public void onCompleted()
2261
{
62+
test.doUnsubscribe();
2363
}
2464

2565
@Override
@@ -30,26 +70,25 @@ public void onError(Exception e)
3070
@Override
3171
public void onNext(T args)
3272
{
33-
test.doUnsubscribe();
3473
}
3574
}));
3675
return test;
3776
}
3877

39-
public static <T> UnsubscribeTester createOnCompleted(Observable<T> observable)
78+
private static <T> UnsubscribeTester createOnError(Observable<T> observable)
4079
{
4180
final UnsubscribeTester test = new UnsubscribeTester();
4281
test.setSubscription(observable.subscribe(new Observer<T>()
4382
{
4483
@Override
4584
public void onCompleted()
4685
{
47-
test.doUnsubscribe();
4886
}
4987

5088
@Override
5189
public void onError(Exception e)
5290
{
91+
test.doUnsubscribe();
5392
}
5493

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

63-
public static <T> UnsubscribeTester createOnError(Observable<T> observable)
102+
private static <T> UnsubscribeTester createOnNext(Observable<T> observable)
64103
{
65104
final UnsubscribeTester test = new UnsubscribeTester();
66105
test.setSubscription(observable.subscribe(new Observer<T>()
@@ -73,12 +112,12 @@ public void onCompleted()
73112
@Override
74113
public void onError(Exception e)
75114
{
76-
test.doUnsubscribe();
77115
}
78116

79117
@Override
80118
public void onNext(T args)
81119
{
120+
test.doUnsubscribe();
82121
}
83122
}));
84123
return test;
@@ -96,7 +135,7 @@ private void doUnsubscribe()
96135
subscription.unsubscribe();
97136
}
98137

99-
public void assertPassed()
138+
private void assertPassed()
100139
{
101140
assertTrue("expected notification was received", subscription == null);
102141
}

0 commit comments

Comments
 (0)