1+ package rx .internal .operators ;
2+
3+ import org .junit .Test ;
4+ import org .mockito .invocation .InvocationOnMock ;
5+ import org .mockito .stubbing .Answer ;
6+ import rx .Observable ;
7+ import rx .Observer ;
8+ import rx .Subscription ;
9+
10+ import java .util .concurrent .Callable ;
11+ import java .util .concurrent .CountDownLatch ;
12+
13+ import static org .mockito .Mockito .*;
14+ import static rx .schedulers .Schedulers .computation ;
15+
16+ public class OnSubscribeFromCallableTest {
17+
18+ @ SuppressWarnings ("unchecked" )
19+ @ Test
20+ public void shouldNotInvokeFuncUntilSubscription () throws Exception {
21+ Callable <Object > func = mock (Callable .class );
22+
23+ when (func .call ()).thenReturn (new Object ());
24+
25+ Observable <Object > fromCallableObservable = Observable .fromCallable (func );
26+
27+ verifyZeroInteractions (func );
28+
29+ fromCallableObservable .subscribe ();
30+
31+ verify (func ).call ();
32+ }
33+
34+ @ SuppressWarnings ("unchecked" )
35+ @ Test
36+ public void shouldCallOnNextAndOnCompleted () throws Exception {
37+ Callable <String > func = mock (Callable .class );
38+
39+ when (func .call ()).thenReturn ("test_value" );
40+
41+ Observable <String > fromCallableObservable = Observable .fromCallable (func );
42+
43+ Observer <String > observer = mock (Observer .class );
44+
45+ fromCallableObservable .subscribe (observer );
46+
47+ verify (observer ).onNext ("test_value" );
48+ verify (observer ).onCompleted ();
49+ verify (observer , never ()).onError (any (Throwable .class ));
50+ }
51+
52+ @ SuppressWarnings ("unchecked" )
53+ @ Test
54+ public void shouldCallOnError () throws Exception {
55+ Callable <Object > func = mock (Callable .class );
56+
57+ Throwable throwable = new IllegalStateException ("Test exception" );
58+ when (func .call ()).thenThrow (throwable );
59+
60+ Observable <Object > fromCallableObservable = Observable .fromCallable (func );
61+
62+ Observer <Object > observer = mock (Observer .class );
63+
64+ fromCallableObservable .subscribe (observer );
65+
66+ verify (observer , never ()).onNext (anyObject ());
67+ verify (observer , never ()).onCompleted ();
68+ verify (observer ).onError (throwable );
69+ }
70+
71+ @ SuppressWarnings ("unchecked" )
72+ @ Test
73+ public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission () throws Exception {
74+ Callable <String > func = mock (Callable .class );
75+
76+ final CountDownLatch funcLatch = new CountDownLatch (1 );
77+ final CountDownLatch observerLatch = new CountDownLatch (1 );
78+
79+ when (func .call ()).thenAnswer (new Answer <String >() {
80+ @ Override
81+ public String answer (InvocationOnMock invocation ) throws Throwable {
82+ observerLatch .countDown ();
83+
84+ try {
85+ funcLatch .await ();
86+ } catch (InterruptedException e ) {
87+ // It's okay, unsubscription causes Thread interruption
88+
89+ // Restoring interruption status of the Thread
90+ Thread .currentThread ().interrupt ();
91+ }
92+
93+ return "should_not_be_delivered" ;
94+ }
95+ });
96+
97+ Observable <String > fromCallableObservable = Observable .fromCallable (func );
98+
99+ Observer <String > observer = mock (Observer .class );
100+
101+ Subscription subscription = fromCallableObservable
102+ .subscribeOn (computation ())
103+ .subscribe (observer );
104+
105+ // Wait until func will be invoked
106+ observerLatch .await ();
107+
108+ // Unsubscribing before emission
109+ subscription .unsubscribe ();
110+
111+ // Emitting result
112+ funcLatch .countDown ();
113+
114+ // func must be invoked
115+ verify (func ).call ();
116+
117+ // Observer must not be notified at all
118+ verifyZeroInteractions (observer );
119+ }
120+
121+ @ SuppressWarnings ("unchecked" )
122+ @ Test
123+ public void shouldAllowToThrowCheckedException () {
124+ final Exception checkedException = new Exception ("test exception" );
125+
126+ Observable <Object > fromCallableObservable = Observable .fromCallable (new Callable <Object >() {
127+ @ Override
128+ public Object call () throws Exception {
129+ throw checkedException ;
130+ }
131+ });
132+
133+ Observer <Object > observer = mock (Observer .class );
134+
135+ fromCallableObservable .subscribe (observer );
136+
137+ verify (observer ).onError (checkedException );
138+ verifyNoMoreInteractions (observer );
139+ }
140+ }
0 commit comments