Skip to content

Commit 2e2ab16

Browse files
Merge pull request #471 from benjchristensen/error-handling
Unit Test Tweaks
2 parents 2a737ec + 115b6d3 commit 2e2ab16

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

rxjava-core/src/test/java/rx/operators/OperationMapTest.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,12 @@ public String call(String s) {
212212
verify(stringObserver, times(1)).onError(any(Throwable.class));
213213
}
214214

215+
/**
216+
* This is testing how unsubscribe behavior is handled when an error occurs in a user provided function
217+
* and the source is unsubscribed from ... but ignores or can't receive the unsubscribe as it is synchronous.
218+
*/
215219
@Test
216-
public void testMapWithSynchronousObservableContainingError() {
220+
public void testMapContainingErrorWithSequenceThatDoesntUnsubscribe() {
217221
Observable<String> w = Observable.from("one", "fail", "two", "three", "fail");
218222
final AtomicInteger c1 = new AtomicInteger();
219223
final AtomicInteger c2 = new AtomicInteger();
@@ -243,7 +247,9 @@ public String call(String s) {
243247
verify(stringObserver, never()).onCompleted();
244248
verify(stringObserver, times(1)).onError(any(Throwable.class));
245249

246-
// we should have only returned 1 value: "one"
250+
// We should have only returned 1 value: "one"
251+
// Since the unsubscribe doesn't propagate, we will actually be sent all events and need
252+
// to ignore all after the first failure.
247253
assertEquals(1, c1.get());
248254
assertEquals(1, c2.get());
249255
}
@@ -282,6 +288,52 @@ public String call(String arg0) {
282288
inorder.verify(stringObserver, times(1)).onError(any(IllegalArgumentException.class));
283289
inorder.verifyNoMoreInteractions();
284290
}
291+
292+
/**
293+
* While mapping over range(1,1).last() we expect IllegalArgumentException since the sequence is empty.
294+
*/
295+
@Test(expected = IllegalArgumentException.class)
296+
public void testErrorPassesThruMap() {
297+
Observable.range(1,0).last().map(new Func1<Integer, Integer>() {
298+
299+
@Override
300+
public Integer call(Integer i) {
301+
return i;
302+
}
303+
304+
}).toBlockingObservable().single();
305+
}
306+
307+
/**
308+
* We expect IllegalStateException to pass thru map.
309+
*/
310+
@Test(expected = IllegalStateException.class)
311+
public void testErrorPassesThruMap2() {
312+
Observable.error(new IllegalStateException()).map(new Func1<Object, Object>() {
313+
314+
@Override
315+
public Object call(Object i) {
316+
return i;
317+
}
318+
319+
}).toBlockingObservable().single();
320+
}
321+
322+
/**
323+
* We expect an ArithmeticException exception here because last() emits a single value
324+
* but then we divide by 0.
325+
*/
326+
@Test(expected = ArithmeticException.class)
327+
public void testMapWithErrorInFunc() {
328+
Observable.range(1,1).last().map(new Func1<Integer, Integer>() {
329+
330+
@Override
331+
public Integer call(Integer i) {
332+
return i/0;
333+
}
334+
335+
}).toBlockingObservable().single();
336+
}
285337

286338
private static Map<String, String> getMap(String prefix) {
287339
Map<String, String> m = new HashMap<String, String>();

rxjava-core/src/test/java/rx/operators/OperationTakeWhileTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Subscription onSubscribe(Observer<? super String> observer) {
119119
public Boolean call(String s) {
120120
return false;
121121
}
122-
})).toBlockingObservable().last();
122+
})).toBlockingObservable().lastOrDefault("");
123123
}
124124

125125
@Test

0 commit comments

Comments
 (0)