|
20 | 20 | import static org.mockito.ArgumentMatchers.any;
|
21 | 21 | import static org.mockito.Mockito.*;
|
22 | 22 |
|
| 23 | +import java.util.List; |
23 | 24 | import java.util.concurrent.*;
|
24 | 25 |
|
| 26 | +import io.reactivex.exceptions.TestException; |
| 27 | +import io.reactivex.plugins.RxJavaPlugins; |
25 | 28 | import org.junit.Test;
|
26 | 29 | import org.mockito.invocation.InvocationOnMock;
|
27 | 30 | import org.mockito.stubbing.Answer;
|
@@ -239,4 +242,75 @@ public Object call() throws Exception {
|
239 | 242 | .test()
|
240 | 243 | .assertFailure(NullPointerException.class);
|
241 | 244 | }
|
| 245 | + |
| 246 | + @Test |
| 247 | + public void disposedOnArrival() { |
| 248 | + final int[] count = { 0 }; |
| 249 | + Observable.fromCallable(new Callable<Object>() { |
| 250 | + @Override |
| 251 | + public Object call() throws Exception { |
| 252 | + count[0]++; |
| 253 | + return 1; |
| 254 | + } |
| 255 | + }) |
| 256 | + .test(true) |
| 257 | + .assertEmpty(); |
| 258 | + |
| 259 | + assertEquals(0, count[0]); |
| 260 | + } |
| 261 | + |
| 262 | + @Test |
| 263 | + public void disposedOnCall() { |
| 264 | + final TestObserver<Integer> to = new TestObserver<Integer>(); |
| 265 | + |
| 266 | + Observable.fromCallable(new Callable<Integer>() { |
| 267 | + @Override |
| 268 | + public Integer call() throws Exception { |
| 269 | + to.cancel(); |
| 270 | + return 1; |
| 271 | + } |
| 272 | + }) |
| 273 | + .subscribe(to); |
| 274 | + |
| 275 | + to.assertEmpty(); |
| 276 | + } |
| 277 | + |
| 278 | + @Test |
| 279 | + public void disposedOnCallThrows() { |
| 280 | + List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 281 | + try { |
| 282 | + final TestObserver<Integer> to = new TestObserver<Integer>(); |
| 283 | + |
| 284 | + Observable.fromCallable(new Callable<Integer>() { |
| 285 | + @Override |
| 286 | + public Integer call() throws Exception { |
| 287 | + to.cancel(); |
| 288 | + throw new TestException(); |
| 289 | + } |
| 290 | + }) |
| 291 | + .subscribe(to); |
| 292 | + |
| 293 | + to.assertEmpty(); |
| 294 | + |
| 295 | + TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 296 | + } finally { |
| 297 | + RxJavaPlugins.reset(); |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + @Test |
| 302 | + public void take() { |
| 303 | + Observable.fromCallable(new Callable<Object>() { |
| 304 | + @Override |
| 305 | + public Object call() throws Exception { |
| 306 | + return 1; |
| 307 | + } |
| 308 | + }) |
| 309 | + .take(1) |
| 310 | + .test() |
| 311 | + .assertResult(1); |
| 312 | + } |
| 313 | + |
| 314 | + |
| 315 | + |
242 | 316 | }
|
0 commit comments