|
18 | 18 | import static org.mockito.Matchers.*;
|
19 | 19 | import static org.mockito.Mockito.*;
|
20 | 20 |
|
| 21 | +import java.io.IOException; |
21 | 22 | import java.util.concurrent.CountDownLatch;
|
22 | 23 | import java.util.concurrent.TimeUnit;
|
23 | 24 | import java.util.concurrent.TimeoutException;
|
@@ -266,4 +267,89 @@ public void call(Subscriber<? super String> subscriber) {
|
266 | 267 |
|
267 | 268 | exit.countDown(); // exit the thread
|
268 | 269 | }
|
| 270 | + |
| 271 | + @Test |
| 272 | + public void shouldUnsubscribeFromUnderlyingSubscriptionOnTimeout() throws InterruptedException { |
| 273 | + // From https://github.com/Netflix/RxJava/pull/951 |
| 274 | + final Subscription s = mock(Subscription.class); |
| 275 | + |
| 276 | + Observable<String> never = Observable.create(new OnSubscribe<String>() { |
| 277 | + public void call(Subscriber<? super String> subscriber) { |
| 278 | + subscriber.add(s); |
| 279 | + } |
| 280 | + }); |
| 281 | + |
| 282 | + TestScheduler testScheduler = new TestScheduler(); |
| 283 | + Observable<String> observableWithTimeout = never.timeout(1000, TimeUnit.MILLISECONDS, testScheduler); |
| 284 | + |
| 285 | + @SuppressWarnings("unchecked") |
| 286 | + Observer<String> observer = mock(Observer.class); |
| 287 | + observableWithTimeout.subscribe(observer); |
| 288 | + |
| 289 | + testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS); |
| 290 | + |
| 291 | + InOrder inOrder = inOrder(observer); |
| 292 | + inOrder.verify(observer).onError(isA(TimeoutException.class)); |
| 293 | + inOrder.verifyNoMoreInteractions(); |
| 294 | + |
| 295 | + verify(s, times(1)).unsubscribe(); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + public void shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyComplete() { |
| 300 | + // From https://github.com/Netflix/RxJava/pull/951 |
| 301 | + final Subscription s = mock(Subscription.class); |
| 302 | + |
| 303 | + Observable<String> immediatelyComplete = Observable.create(new OnSubscribe<String>() { |
| 304 | + public void call(Subscriber<? super String> subscriber) { |
| 305 | + subscriber.add(s); |
| 306 | + subscriber.onCompleted(); |
| 307 | + } |
| 308 | + }); |
| 309 | + |
| 310 | + TestScheduler testScheduler = new TestScheduler(); |
| 311 | + Observable<String> observableWithTimeout = immediatelyComplete.timeout(1000, TimeUnit.MILLISECONDS, |
| 312 | + testScheduler); |
| 313 | + |
| 314 | + @SuppressWarnings("unchecked") |
| 315 | + Observer<String> observer = mock(Observer.class); |
| 316 | + observableWithTimeout.subscribe(observer); |
| 317 | + |
| 318 | + testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS); |
| 319 | + |
| 320 | + InOrder inOrder = inOrder(observer); |
| 321 | + inOrder.verify(observer).onCompleted(); |
| 322 | + inOrder.verifyNoMoreInteractions(); |
| 323 | + |
| 324 | + verify(s, times(1)).unsubscribe(); |
| 325 | + } |
| 326 | + |
| 327 | + @Test |
| 328 | + public void shouldUnsubscribeFromUnderlyingSubscriptionOnImmediatelyErrored() throws InterruptedException { |
| 329 | + // From https://github.com/Netflix/RxJava/pull/951 |
| 330 | + final Subscription s = mock(Subscription.class); |
| 331 | + |
| 332 | + Observable<String> immediatelyError = Observable.create(new OnSubscribe<String>() { |
| 333 | + public void call(Subscriber<? super String> subscriber) { |
| 334 | + subscriber.add(s); |
| 335 | + subscriber.onError(new IOException("Error")); |
| 336 | + } |
| 337 | + }); |
| 338 | + |
| 339 | + TestScheduler testScheduler = new TestScheduler(); |
| 340 | + Observable<String> observableWithTimeout = immediatelyError.timeout(1000, TimeUnit.MILLISECONDS, |
| 341 | + testScheduler); |
| 342 | + |
| 343 | + @SuppressWarnings("unchecked") |
| 344 | + Observer<String> observer = mock(Observer.class); |
| 345 | + observableWithTimeout.subscribe(observer); |
| 346 | + |
| 347 | + testScheduler.advanceTimeBy(2000, TimeUnit.MILLISECONDS); |
| 348 | + |
| 349 | + InOrder inOrder = inOrder(observer); |
| 350 | + inOrder.verify(observer).onError(isA(IOException.class)); |
| 351 | + inOrder.verifyNoMoreInteractions(); |
| 352 | + |
| 353 | + verify(s, times(1)).unsubscribe(); |
| 354 | + } |
269 | 355 | }
|
0 commit comments