From 1945713fd50d0e6740df94fb63358a0939105ab5 Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Wed, 6 Nov 2013 20:12:00 -0800 Subject: [PATCH 1/2] Fix unit test after last() changed behavior --- .../src/test/java/rx/operators/OperationTakeWhileTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rxjava-core/src/test/java/rx/operators/OperationTakeWhileTest.java b/rxjava-core/src/test/java/rx/operators/OperationTakeWhileTest.java index 08c6d8e83f..830dd59b33 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationTakeWhileTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperationTakeWhileTest.java @@ -119,7 +119,7 @@ public Subscription onSubscribe(Observer observer) { public Boolean call(String s) { return false; } - })).toBlockingObservable().last(); + })).toBlockingObservable().lastOrDefault(""); } @Test From 115b6d327784fbf3300dab4bfd076318432038fd Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Wed, 6 Nov 2013 20:12:12 -0800 Subject: [PATCH 2/2] Add and clarify unit tests in map --- .../java/rx/operators/OperationMapTest.java | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/rxjava-core/src/test/java/rx/operators/OperationMapTest.java b/rxjava-core/src/test/java/rx/operators/OperationMapTest.java index 6d639d5235..773534a7e8 100644 --- a/rxjava-core/src/test/java/rx/operators/OperationMapTest.java +++ b/rxjava-core/src/test/java/rx/operators/OperationMapTest.java @@ -212,8 +212,12 @@ public String call(String s) { verify(stringObserver, times(1)).onError(any(Throwable.class)); } + /** + * This is testing how unsubscribe behavior is handled when an error occurs in a user provided function + * and the source is unsubscribed from ... but ignores or can't receive the unsubscribe as it is synchronous. + */ @Test - public void testMapWithSynchronousObservableContainingError() { + public void testMapContainingErrorWithSequenceThatDoesntUnsubscribe() { Observable w = Observable.from("one", "fail", "two", "three", "fail"); final AtomicInteger c1 = new AtomicInteger(); final AtomicInteger c2 = new AtomicInteger(); @@ -243,7 +247,9 @@ public String call(String s) { verify(stringObserver, never()).onCompleted(); verify(stringObserver, times(1)).onError(any(Throwable.class)); - // we should have only returned 1 value: "one" + // We should have only returned 1 value: "one" + // Since the unsubscribe doesn't propagate, we will actually be sent all events and need + // to ignore all after the first failure. assertEquals(1, c1.get()); assertEquals(1, c2.get()); } @@ -282,6 +288,52 @@ public String call(String arg0) { inorder.verify(stringObserver, times(1)).onError(any(IllegalArgumentException.class)); inorder.verifyNoMoreInteractions(); } + + /** + * While mapping over range(1,1).last() we expect IllegalArgumentException since the sequence is empty. + */ + @Test(expected = IllegalArgumentException.class) + public void testErrorPassesThruMap() { + Observable.range(1,0).last().map(new Func1() { + + @Override + public Integer call(Integer i) { + return i; + } + + }).toBlockingObservable().single(); + } + + /** + * We expect IllegalStateException to pass thru map. + */ + @Test(expected = IllegalStateException.class) + public void testErrorPassesThruMap2() { + Observable.error(new IllegalStateException()).map(new Func1() { + + @Override + public Object call(Object i) { + return i; + } + + }).toBlockingObservable().single(); + } + + /** + * We expect an ArithmeticException exception here because last() emits a single value + * but then we divide by 0. + */ + @Test(expected = ArithmeticException.class) + public void testMapWithErrorInFunc() { + Observable.range(1,1).last().map(new Func1() { + + @Override + public Integer call(Integer i) { + return i/0; + } + + }).toBlockingObservable().single(); + } private static Map getMap(String prefix) { Map m = new HashMap();