15
15
*/
16
16
package rx ;
17
17
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 ;
20
24
21
25
import java .util .concurrent .TimeUnit ;
22
26
import java .util .concurrent .TimeoutException ;
23
27
24
28
import org .junit .Before ;
25
29
import org .junit .Test ;
30
+ import org .mockito .InOrder ;
26
31
import org .mockito .MockitoAnnotations ;
27
32
28
33
import rx .concurrency .TestScheduler ;
@@ -46,6 +51,7 @@ public void setUp() {
46
51
47
52
@ Test
48
53
public void shouldNotTimeoutIfOnNextWithinTimeout () {
54
+ @ SuppressWarnings ("unchecked" )
49
55
Observer <String > observer = mock (Observer .class );
50
56
Subscription subscription = withTimeout .subscribe (observer );
51
57
testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -58,6 +64,7 @@ public void shouldNotTimeoutIfOnNextWithinTimeout() {
58
64
59
65
@ Test
60
66
public void shouldNotTimeoutIfSecondOnNextWithinTimeout () {
67
+ @ SuppressWarnings ("unchecked" )
61
68
Observer <String > observer = mock (Observer .class );
62
69
Subscription subscription = withTimeout .subscribe (observer );
63
70
testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -72,6 +79,7 @@ public void shouldNotTimeoutIfSecondOnNextWithinTimeout() {
72
79
73
80
@ Test
74
81
public void shouldTimeoutIfOnNextNotWithinTimeout () {
82
+ @ SuppressWarnings ("unchecked" )
75
83
Observer <String > observer = mock (Observer .class );
76
84
Subscription subscription = withTimeout .subscribe (observer );
77
85
testScheduler .advanceTimeBy (TIMEOUT + 1 , TimeUnit .SECONDS );
@@ -81,6 +89,7 @@ public void shouldTimeoutIfOnNextNotWithinTimeout() {
81
89
82
90
@ Test
83
91
public void shouldTimeoutIfSecondOnNextNotWithinTimeout () {
92
+ @ SuppressWarnings ("unchecked" )
84
93
Observer <String > observer = mock (Observer .class );
85
94
Subscription subscription = withTimeout .subscribe (observer );
86
95
testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -93,6 +102,7 @@ public void shouldTimeoutIfSecondOnNextNotWithinTimeout() {
93
102
94
103
@ Test
95
104
public void shouldCompleteIfUnderlyingComletes () {
105
+ @ SuppressWarnings ("unchecked" )
96
106
Observer <String > observer = mock (Observer .class );
97
107
Subscription subscription = withTimeout .subscribe (observer );
98
108
testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -105,6 +115,7 @@ public void shouldCompleteIfUnderlyingComletes() {
105
115
106
116
@ Test
107
117
public void shouldErrorIfUnderlyingErrors () {
118
+ @ SuppressWarnings ("unchecked" )
108
119
Observer <String > observer = mock (Observer .class );
109
120
Subscription subscription = withTimeout .subscribe (observer );
110
121
testScheduler .advanceTimeBy (2 , TimeUnit .SECONDS );
@@ -113,4 +124,103 @@ public void shouldErrorIfUnderlyingErrors() {
113
124
verify (observer ).onError (any (UnsupportedOperationException .class ));
114
125
subscription .unsubscribe ();
115
126
}
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
+ }
116
226
}
0 commit comments