|
15 | 15 | */
|
16 | 16 | package rx.internal.operators;
|
17 | 17 |
|
| 18 | +import static org.junit.Assert.assertEquals; |
18 | 19 | import static org.mockito.Matchers.any;
|
19 | 20 | import static org.mockito.Mockito.*;
|
| 21 | +import static org.mockito.Mockito.verify; |
20 | 22 |
|
21 |
| -import java.util.*; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.LinkedHashMap; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.concurrent.CopyOnWriteArrayList; |
22 | 29 |
|
23 |
| -import org.junit.*; |
24 |
| -import org.mockito.*; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.MockitoAnnotations; |
25 | 34 |
|
26 | 35 | import rx.Observable;
|
| 36 | +import rx.Observable.OnSubscribe; |
27 | 37 | import rx.Observer;
|
| 38 | +import rx.Producer; |
| 39 | +import rx.Subscriber; |
28 | 40 | import rx.exceptions.TestException;
|
29 |
| -import rx.functions.*; |
| 41 | +import rx.functions.Action1; |
| 42 | +import rx.functions.Func0; |
| 43 | +import rx.functions.Func1; |
30 | 44 | import rx.internal.util.UtilityFunctions;
|
31 | 45 | import rx.observers.TestSubscriber;
|
| 46 | +import rx.plugins.RxJavaHooks; |
32 | 47 |
|
33 | 48 | public class OperatorToMapTest {
|
34 | 49 | @Mock
|
@@ -281,4 +296,111 @@ public Map<Integer, Integer> call() {
|
281 | 296 | ts.assertNoValues();
|
282 | 297 | ts.assertNotCompleted();
|
283 | 298 | }
|
| 299 | + |
| 300 | + @Test |
| 301 | + public void testFactoryFailureDoesNotAllowErrorAndCompletedEmissions() { |
| 302 | + TestSubscriber<Map<Integer, Integer>> ts = TestSubscriber.create(0); |
| 303 | + final RuntimeException e = new RuntimeException(); |
| 304 | + Observable.create(new OnSubscribe<Integer>() { |
| 305 | + |
| 306 | + @Override |
| 307 | + public void call(final Subscriber<? super Integer> sub) { |
| 308 | + sub.setProducer(new Producer() { |
| 309 | + |
| 310 | + @Override |
| 311 | + public void request(long n) { |
| 312 | + if (n > 1) { |
| 313 | + sub.onNext(1); |
| 314 | + sub.onCompleted(); |
| 315 | + } |
| 316 | + } |
| 317 | + }); |
| 318 | + } |
| 319 | + }).toMap(new Func1<Integer,Integer>() { |
| 320 | + |
| 321 | + @Override |
| 322 | + public Integer call(Integer t) { |
| 323 | + throw e; |
| 324 | + } |
| 325 | + }).unsafeSubscribe(ts); |
| 326 | + ts.assertNoValues(); |
| 327 | + ts.assertError(e); |
| 328 | + ts.assertNotCompleted(); |
| 329 | + } |
| 330 | + |
| 331 | + @Test |
| 332 | + public void testFactoryFailureDoesNotAllowTwoErrorEmissions() { |
| 333 | + try { |
| 334 | + final List<Throwable> list = new CopyOnWriteArrayList<Throwable>(); |
| 335 | + RxJavaHooks.setOnError(new Action1<Throwable>() { |
| 336 | + |
| 337 | + @Override |
| 338 | + public void call(Throwable t) { |
| 339 | + list.add(t); |
| 340 | + } |
| 341 | + }); |
| 342 | + TestSubscriber<Map<Integer, Integer>> ts = TestSubscriber.create(0); |
| 343 | + final RuntimeException e1 = new RuntimeException(); |
| 344 | + final RuntimeException e2 = new RuntimeException(); |
| 345 | + Observable.create(new OnSubscribe<Integer>() { |
| 346 | + |
| 347 | + @Override |
| 348 | + public void call(final Subscriber<? super Integer> sub) { |
| 349 | + sub.setProducer(new Producer() { |
| 350 | + |
| 351 | + @Override |
| 352 | + public void request(long n) { |
| 353 | + if (n > 1) { |
| 354 | + sub.onNext(1); |
| 355 | + sub.onError(e2); |
| 356 | + } |
| 357 | + } |
| 358 | + }); |
| 359 | + } |
| 360 | + }).toMap(new Func1<Integer, Integer>() { |
| 361 | + |
| 362 | + @Override |
| 363 | + public Integer call(Integer t) { |
| 364 | + throw e1; |
| 365 | + } |
| 366 | + }).unsafeSubscribe(ts); |
| 367 | + ts.assertNoValues(); |
| 368 | + assertEquals(Arrays.asList(e1), ts.getOnErrorEvents()); |
| 369 | + assertEquals(Arrays.asList(e2), list); |
| 370 | + ts.assertNotCompleted(); |
| 371 | + } finally { |
| 372 | + RxJavaHooks.setOnError(null); |
| 373 | + } |
| 374 | + } |
| 375 | + |
| 376 | + @Test |
| 377 | + public void testFactoryFailureDoesNotAllowErrorThenOnNextEmissions() { |
| 378 | + TestSubscriber<Map<Integer, Integer>> ts = TestSubscriber.create(0); |
| 379 | + final RuntimeException e = new RuntimeException(); |
| 380 | + Observable.create(new OnSubscribe<Integer>() { |
| 381 | + |
| 382 | + @Override |
| 383 | + public void call(final Subscriber<? super Integer> sub) { |
| 384 | + sub.setProducer(new Producer() { |
| 385 | + |
| 386 | + @Override |
| 387 | + public void request(long n) { |
| 388 | + if (n > 1) { |
| 389 | + sub.onNext(1); |
| 390 | + sub.onNext(2); |
| 391 | + } |
| 392 | + } |
| 393 | + }); |
| 394 | + } |
| 395 | + }).toMap(new Func1<Integer,Integer>() { |
| 396 | + |
| 397 | + @Override |
| 398 | + public Integer call(Integer t) { |
| 399 | + throw e; |
| 400 | + } |
| 401 | + }).unsafeSubscribe(ts); |
| 402 | + ts.assertNoValues(); |
| 403 | + ts.assertError(e); |
| 404 | + ts.assertNotCompleted(); |
| 405 | + } |
284 | 406 | }
|
0 commit comments