|
16 | 16 | package rx.observables;
|
17 | 17 |
|
18 | 18 | import static org.junit.Assert.assertEquals;
|
| 19 | +import static org.junit.Assert.assertTrue; |
19 | 20 | import static org.junit.Assert.fail;
|
20 | 21 |
|
21 | 22 | import java.util.Iterator;
|
22 | 23 | import java.util.NoSuchElementException;
|
| 24 | +import java.util.concurrent.CountDownLatch; |
| 25 | +import java.util.concurrent.TimeUnit; |
23 | 26 |
|
24 | 27 | import org.junit.Assert;
|
25 | 28 | import org.junit.Before;
|
|
28 | 31 | import org.mockito.MockitoAnnotations;
|
29 | 32 |
|
30 | 33 | import rx.Observable;
|
| 34 | +import rx.Observable.OnSubscribe; |
31 | 35 | import rx.Subscriber;
|
32 | 36 | import rx.exceptions.TestException;
|
| 37 | +import rx.functions.Action0; |
33 | 38 | import rx.functions.Action1;
|
34 | 39 | import rx.functions.Func1;
|
| 40 | +import rx.schedulers.Schedulers; |
| 41 | +import rx.subscriptions.Subscriptions; |
35 | 42 |
|
36 | 43 | public class BlockingObservableTest {
|
37 | 44 |
|
@@ -377,4 +384,30 @@ public Boolean call(String args) {
|
377 | 384 | });
|
378 | 385 | assertEquals("default", first);
|
379 | 386 | }
|
| 387 | + |
| 388 | + @Test |
| 389 | + public void testSingleOrDefaultUnsubscribe() throws InterruptedException { |
| 390 | + final CountDownLatch unsubscribe = new CountDownLatch(1); |
| 391 | + Observable<Integer> o = Observable.create(new OnSubscribe<Integer>() { |
| 392 | + @Override |
| 393 | + public void call(Subscriber<? super Integer> subscriber) { |
| 394 | + subscriber.add(Subscriptions.create(new Action0() { |
| 395 | + @Override |
| 396 | + public void call() { |
| 397 | + unsubscribe.countDown(); |
| 398 | + } |
| 399 | + })); |
| 400 | + subscriber.onNext(1); |
| 401 | + subscriber.onNext(2); |
| 402 | + // Don't call `onCompleted` to emulate an infinite stream |
| 403 | + } |
| 404 | + }).subscribeOn(Schedulers.newThread()); |
| 405 | + try { |
| 406 | + o.toBlocking().singleOrDefault(-1); |
| 407 | + fail("Expected IllegalArgumentException because there are 2 elements"); |
| 408 | + } catch (IllegalArgumentException e) { |
| 409 | + // Expected |
| 410 | + } |
| 411 | + assertTrue("Timeout means `unsubscribe` is not called", unsubscribe.await(30, TimeUnit.SECONDS)); |
| 412 | + } |
380 | 413 | }
|
0 commit comments