1515 */
1616package rx ;
1717
18- import static org .mockito .Matchers .*;
19- import static org .mockito .Mockito .*;
18+ import static org .mockito .Matchers .any ;
19+ import static org .mockito .Mockito .inOrder ;
20+ import static org .mockito .Mockito .mock ;
21+ import static org .mockito .Mockito .never ;
22+ import static org .mockito .Mockito .times ;
23+ import static org .mockito .Mockito .verify ;
2024
2125import java .util .concurrent .TimeUnit ;
2226import java .util .concurrent .TimeoutException ;
2327
2428import org .junit .Before ;
2529import org .junit .Test ;
30+ import org .mockito .InOrder ;
2631import org .mockito .MockitoAnnotations ;
2732
2833import rx .concurrency .TestScheduler ;
@@ -46,6 +51,7 @@ public void setUp() {
4651
4752 @ Test
4853 public void shouldNotTimeoutIfOnNextWithinTimeout () {
54+ @ SuppressWarnings ("unchecked" )
4955 Observer <String > observer = mock (Observer .class );
5056 Subscription subscription = withTimeout .subscribe (observer );
5157 testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -58,6 +64,7 @@ public void shouldNotTimeoutIfOnNextWithinTimeout() {
5864
5965 @ Test
6066 public void shouldNotTimeoutIfSecondOnNextWithinTimeout () {
67+ @ SuppressWarnings ("unchecked" )
6168 Observer <String > observer = mock (Observer .class );
6269 Subscription subscription = withTimeout .subscribe (observer );
6370 testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -72,6 +79,7 @@ public void shouldNotTimeoutIfSecondOnNextWithinTimeout() {
7279
7380 @ Test
7481 public void shouldTimeoutIfOnNextNotWithinTimeout () {
82+ @ SuppressWarnings ("unchecked" )
7583 Observer <String > observer = mock (Observer .class );
7684 Subscription subscription = withTimeout .subscribe (observer );
7785 testScheduler .advanceTimeBy (TIMEOUT + 1 , TimeUnit .SECONDS );
@@ -81,6 +89,7 @@ public void shouldTimeoutIfOnNextNotWithinTimeout() {
8189
8290 @ Test
8391 public void shouldTimeoutIfSecondOnNextNotWithinTimeout () {
92+ @ SuppressWarnings ("unchecked" )
8493 Observer <String > observer = mock (Observer .class );
8594 Subscription subscription = withTimeout .subscribe (observer );
8695 testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -93,6 +102,7 @@ public void shouldTimeoutIfSecondOnNextNotWithinTimeout() {
93102
94103 @ Test
95104 public void shouldCompleteIfUnderlyingComletes () {
105+ @ SuppressWarnings ("unchecked" )
96106 Observer <String > observer = mock (Observer .class );
97107 Subscription subscription = withTimeout .subscribe (observer );
98108 testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -105,6 +115,7 @@ public void shouldCompleteIfUnderlyingComletes() {
105115
106116 @ Test
107117 public void shouldErrorIfUnderlyingErrors () {
118+ @ SuppressWarnings ("unchecked" )
108119 Observer <String > observer = mock (Observer .class );
109120 Subscription subscription = withTimeout .subscribe (observer );
110121 testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -113,4 +124,103 @@ public void shouldErrorIfUnderlyingErrors() {
113124 verify (observer ).onError (any (UnsupportedOperationException .class ));
114125 subscription .unsubscribe ();
115126 }
127+
128+ @ Test
129+ public void shouldSwitchToOtherIfOnNextNotWithinTimeout () {
130+ Observable <String > other = Observable .from ("a" , "b" , "c" );
131+ Observable <String > source = underlyingSubject .timeout (TIMEOUT , TIME_UNIT , other , testScheduler );
132+
133+ @ SuppressWarnings ("unchecked" )
134+ Observer <String > observer = mock (Observer .class );
135+ Subscription subscription = source .subscribe (observer );
136+
137+ testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
138+ underlyingSubject .onNext ("One" );
139+ testScheduler .advanceTimeBy (4 , TimeUnit .SECONDS );
140+ underlyingSubject .onNext ("Two" );
141+ InOrder inOrder = inOrder (observer );
142+ inOrder .verify (observer , times (1 )).onNext ("One" );
143+ inOrder .verify (observer , times (1 )).onNext ("a" );
144+ inOrder .verify (observer , times (1 )).onNext ("b" );
145+ inOrder .verify (observer , times (1 )).onNext ("c" );
146+ inOrder .verify (observer , times (1 )).onCompleted ();
147+ inOrder .verifyNoMoreInteractions ();
148+ subscription .unsubscribe ();
149+ }
150+
151+ @ Test
152+ public void shouldSwitchToOtherIfOnErrorNotWithinTimeout () {
153+ Observable <String > other = Observable .from ("a" , "b" , "c" );
154+ Observable <String > source = underlyingSubject .timeout (TIMEOUT , TIME_UNIT , other , testScheduler );
155+
156+ @ SuppressWarnings ("unchecked" )
157+ Observer <String > observer = mock (Observer .class );
158+ Subscription subscription = source .subscribe (observer );
159+
160+ testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
161+ underlyingSubject .onNext ("One" );
162+ testScheduler .advanceTimeBy (4 , TimeUnit .SECONDS );
163+ underlyingSubject .onError (new UnsupportedOperationException ());
164+ InOrder inOrder = inOrder (observer );
165+ inOrder .verify (observer , times (1 )).onNext ("One" );
166+ inOrder .verify (observer , times (1 )).onNext ("a" );
167+ inOrder .verify (observer , times (1 )).onNext ("b" );
168+ inOrder .verify (observer , times (1 )).onNext ("c" );
169+ inOrder .verify (observer , times (1 )).onCompleted ();
170+ inOrder .verifyNoMoreInteractions ();
171+ subscription .unsubscribe ();
172+ }
173+
174+ @ Test
175+ public void shouldSwitchToOtherIfOnCompletedNotWithinTimeout () {
176+ Observable <String > other = Observable .from ("a" , "b" , "c" );
177+ Observable <String > source = underlyingSubject .timeout (TIMEOUT , TIME_UNIT , other , testScheduler );
178+
179+ @ SuppressWarnings ("unchecked" )
180+ Observer <String > observer = mock (Observer .class );
181+ Subscription subscription = source .subscribe (observer );
182+
183+ testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
184+ underlyingSubject .onNext ("One" );
185+ testScheduler .advanceTimeBy (4 , TimeUnit .SECONDS );
186+ underlyingSubject .onCompleted ();
187+ InOrder inOrder = inOrder (observer );
188+ inOrder .verify (observer , times (1 )).onNext ("One" );
189+ inOrder .verify (observer , times (1 )).onNext ("a" );
190+ inOrder .verify (observer , times (1 )).onNext ("b" );
191+ inOrder .verify (observer , times (1 )).onNext ("c" );
192+ inOrder .verify (observer , times (1 )).onCompleted ();
193+ inOrder .verifyNoMoreInteractions ();
194+ subscription .unsubscribe ();
195+ }
196+
197+ @ Test
198+ public void shouldSwitchToOtherAndCanBeUnsubscribedIfOnNextNotWithinTimeout () {
199+ PublishSubject <String > other = PublishSubject .create ();
200+ Observable <String > source = underlyingSubject .timeout (TIMEOUT , TIME_UNIT , other , testScheduler );
201+
202+ @ SuppressWarnings ("unchecked" )
203+ Observer <String > observer = mock (Observer .class );
204+ Subscription subscription = source .subscribe (observer );
205+
206+ testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
207+ underlyingSubject .onNext ("One" );
208+ testScheduler .advanceTimeBy (4 , TimeUnit .SECONDS );
209+ underlyingSubject .onNext ("Two" );
210+
211+ other .onNext ("a" );
212+ other .onNext ("b" );
213+ subscription .unsubscribe ();
214+
215+ // The following messages should not be delivered.
216+ other .onNext ("c" );
217+ other .onNext ("d" );
218+ other .onCompleted ();
219+
220+ InOrder inOrder = inOrder (observer );
221+ inOrder .verify (observer , times (1 )).onNext ("One" );
222+ inOrder .verify (observer , times (1 )).onNext ("a" );
223+ inOrder .verify (observer , times (1 )).onNext ("b" );
224+ inOrder .verifyNoMoreInteractions ();
225+ }
116226}
0 commit comments