|
15 | 15 | */
|
16 | 16 | package rx.operators;
|
17 | 17 |
|
18 |
| -import static org.junit.Assert.*; |
19 |
| -import static rx.operators.OperationToFuture.*; |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | +import static rx.operators.OperationToFuture.toFuture; |
20 | 22 |
|
21 | 23 | import java.util.List;
|
| 24 | +import java.util.concurrent.CancellationException; |
22 | 25 | import java.util.concurrent.ExecutionException;
|
23 | 26 | import java.util.concurrent.Future;
|
| 27 | +import java.util.concurrent.TimeUnit; |
24 | 28 |
|
25 | 29 | import org.junit.Test;
|
26 | 30 |
|
27 | 31 | import rx.Observable;
|
28 | 32 | import rx.Observable.OnSubscribeFunc;
|
29 | 33 | import rx.Observer;
|
| 34 | +import rx.Subscriber; |
30 | 35 | import rx.Subscription;
|
31 | 36 | import rx.subscriptions.Subscriptions;
|
32 | 37 |
|
@@ -77,6 +82,34 @@ public Subscription onSubscribe(Observer<? super String> observer) {
|
77 | 82 | }
|
78 | 83 | }
|
79 | 84 |
|
| 85 | + @Test(expected=CancellationException.class) |
| 86 | + public void testGetAfterCancel() throws Exception { |
| 87 | + Observable<String> obs = Observable.create(new OperationNeverComplete<String>()); |
| 88 | + Future<String> f = toFuture(obs); |
| 89 | + boolean cancelled = f.cancel(true); |
| 90 | + assertTrue(cancelled); // because OperationNeverComplete never does |
| 91 | + f.get(); // Future.get() docs require this to throw |
| 92 | + } |
| 93 | + |
| 94 | + @Test(expected=CancellationException.class) |
| 95 | + public void testGetWithTimeoutAfterCancel() throws Exception { |
| 96 | + Observable<String> obs = Observable.create(new OperationNeverComplete<String>()); |
| 97 | + Future<String> f = toFuture(obs); |
| 98 | + boolean cancelled = f.cancel(true); |
| 99 | + assertTrue(cancelled); // because OperationNeverComplete never does |
| 100 | + f.get(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // Future.get() docs require this to throw |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Emits no observations. Used to simulate a long-running asynchronous operation. |
| 105 | + */ |
| 106 | + private static class OperationNeverComplete<T> implements Observable.OnSubscribe<T> { |
| 107 | + @Override |
| 108 | + public void call(Subscriber<? super T> unused) { |
| 109 | + // do nothing |
| 110 | + } |
| 111 | + } |
| 112 | + |
80 | 113 | private static class TestException extends RuntimeException {
|
81 | 114 | private static final long serialVersionUID = 1L;
|
82 | 115 | }
|
|
0 commit comments