Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test Tweaks #471

Merged
merged 2 commits into from
Nov 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions rxjava-core/src/test/java/rx/operators/OperationMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> w = Observable.from("one", "fail", "two", "three", "fail");
final AtomicInteger c1 = new AtomicInteger();
final AtomicInteger c2 = new AtomicInteger();
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<Integer, Integer>() {

@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<Object, Object>() {

@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<Integer, Integer>() {

@Override
public Integer call(Integer i) {
return i/0;
}

}).toBlockingObservable().single();
}

private static Map<String, String> getMap(String prefix) {
Map<String, String> m = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Subscription onSubscribe(Observer<? super String> observer) {
public Boolean call(String s) {
return false;
}
})).toBlockingObservable().last();
})).toBlockingObservable().lastOrDefault("");
}

@Test
Expand Down