|
15 | 15 | */
|
16 | 16 | package rx.operators;
|
17 | 17 |
|
| 18 | +import static org.junit.Assert.*; |
18 | 19 | import static org.mockito.Matchers.*;
|
19 | 20 | import static org.mockito.Mockito.*;
|
20 | 21 |
|
21 | 22 | import java.util.HashMap;
|
22 | 23 | import java.util.Map;
|
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
23 | 25 |
|
24 | 26 | import org.junit.Before;
|
25 | 27 | import org.junit.Test;
|
@@ -119,11 +121,8 @@ public MapObserver(Observer<R> observer, Func1<T, R> func) {
|
119 | 121 | Func1<T, R> func;
|
120 | 122 |
|
121 | 123 | public void onNext(T value) {
|
122 |
| - try { |
123 |
| - observer.onNext(func.call(value)); |
124 |
| - } catch (Exception ex) { |
125 |
| - observer.onError(ex); |
126 |
| - } |
| 124 | + // let the exception be thrown if func fails as a SafeObserver wrapping this will handle it |
| 125 | + observer.onNext(func.call(value)); |
127 | 126 | }
|
128 | 127 |
|
129 | 128 | public void onError(Exception ex) {
|
@@ -251,6 +250,40 @@ public String call(Map<String, String> map) {
|
251 | 250 |
|
252 | 251 | }
|
253 | 252 |
|
| 253 | + @Test |
| 254 | + public void testMapWithSynchronousObservableContainingError() { |
| 255 | + Observable<String> w = Observable.from("one", "fail", "two", "three", "fail"); |
| 256 | + final AtomicInteger c1 = new AtomicInteger(); |
| 257 | + final AtomicInteger c2 = new AtomicInteger(); |
| 258 | + Observable<String> m = Observable.create(map(w, new Func1<String, String>() { |
| 259 | + public String call(String s) { |
| 260 | + if ("fail".equals(s)) |
| 261 | + throw new RuntimeException("Forced Failure"); |
| 262 | + System.out.println("BadMapper:" + s); |
| 263 | + c1.incrementAndGet(); |
| 264 | + return s; |
| 265 | + } |
| 266 | + })).map(new Func1<String, String>() { |
| 267 | + public String call(String s) { |
| 268 | + System.out.println("SecondMapper:" + s); |
| 269 | + c2.incrementAndGet(); |
| 270 | + return s; |
| 271 | + } |
| 272 | + }); |
| 273 | + |
| 274 | + m.subscribe(stringObserver); |
| 275 | + |
| 276 | + verify(stringObserver, times(1)).onNext("one"); |
| 277 | + verify(stringObserver, never()).onNext("two"); |
| 278 | + verify(stringObserver, never()).onNext("three"); |
| 279 | + verify(stringObserver, never()).onCompleted(); |
| 280 | + verify(stringObserver, times(1)).onError(any(Exception.class)); |
| 281 | + |
| 282 | + // we should have only returned 1 value: "one" |
| 283 | + assertEquals(1, c1.get()); |
| 284 | + assertEquals(1, c2.get()); |
| 285 | + } |
| 286 | + |
254 | 287 | private Map<String, String> getMap(String prefix) {
|
255 | 288 | Map<String, String> m = new HashMap<String, String>();
|
256 | 289 | m.put("firstName", prefix + "First");
|
|
0 commit comments