|
28 | 28 | import rx.Single.OnSubscribe; |
29 | 29 | import rx.exceptions.*; |
30 | 30 | import rx.functions.*; |
| 31 | +import rx.internal.util.RxThreadFactory; |
31 | 32 | import rx.observers.*; |
32 | 33 | import rx.plugins.RxJavaHooks; |
33 | 34 | import rx.schedulers.*; |
@@ -2230,4 +2231,65 @@ public void call(Throwable t) { |
2230 | 2231 |
|
2231 | 2232 | assertEquals(1, calls[0]); |
2232 | 2233 | } |
| 2234 | + |
| 2235 | + @Test |
| 2236 | + public void unsubscribeOnSuccess() throws InterruptedException { |
| 2237 | + final AtomicReference<String> name = new AtomicReference<String>(); |
| 2238 | + |
| 2239 | + final CountDownLatch cdl = new CountDownLatch(1); |
| 2240 | + |
| 2241 | + TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 2242 | + |
| 2243 | + Single.fromCallable(new Callable<Integer>() { |
| 2244 | + @Override |
| 2245 | + public Integer call() throws Exception { |
| 2246 | + return 1; |
| 2247 | + } |
| 2248 | + }) |
| 2249 | + .doOnUnsubscribe(new Action0() { |
| 2250 | + @Override |
| 2251 | + public void call() { |
| 2252 | + name.set(Thread.currentThread().getName()); |
| 2253 | + cdl.countDown(); |
| 2254 | + } |
| 2255 | + }) |
| 2256 | + .subscribeOn(Schedulers.io()) |
| 2257 | + .unsubscribeOn(Schedulers.computation()) |
| 2258 | + .subscribe(ts); |
| 2259 | + |
| 2260 | + cdl.await(); |
| 2261 | + |
| 2262 | + ts.awaitTerminalEvent(); |
| 2263 | + ts.assertReceivedOnNext(Arrays.asList(1)); |
| 2264 | + |
| 2265 | + assertTrue(name.get().startsWith("RxComputation")); |
| 2266 | + } |
| 2267 | + |
| 2268 | + @Test |
| 2269 | + public void unsubscribeOnError() throws InterruptedException { |
| 2270 | + final AtomicReference<String> name = new AtomicReference<String>(); |
| 2271 | + |
| 2272 | + final CountDownLatch cdl = new CountDownLatch(1); |
| 2273 | + |
| 2274 | + TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 2275 | + |
| 2276 | + Single.<Integer>error(new RuntimeException()) |
| 2277 | + .doOnUnsubscribe(new Action0() { |
| 2278 | + @Override |
| 2279 | + public void call() { |
| 2280 | + name.set(Thread.currentThread().getName()); |
| 2281 | + cdl.countDown(); |
| 2282 | + } |
| 2283 | + }) |
| 2284 | + .subscribeOn(Schedulers.io()) |
| 2285 | + .unsubscribeOn(Schedulers.computation()) |
| 2286 | + .subscribe(ts); |
| 2287 | + |
| 2288 | + cdl.await(); |
| 2289 | + |
| 2290 | + ts.awaitTerminalEvent(); |
| 2291 | + ts.assertError(RuntimeException.class); |
| 2292 | + |
| 2293 | + assertTrue(name.get().startsWith("RxComputation")); |
| 2294 | + } |
2233 | 2295 | } |
0 commit comments