@@ -212,8 +212,12 @@ public String call(String s) {
212
212
verify (stringObserver , times (1 )).onError (any (Throwable .class ));
213
213
}
214
214
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
+ */
215
219
@ Test
216
- public void testMapWithSynchronousObservableContainingError () {
220
+ public void testMapContainingErrorWithSequenceThatDoesntUnsubscribe () {
217
221
Observable <String > w = Observable .from ("one" , "fail" , "two" , "three" , "fail" );
218
222
final AtomicInteger c1 = new AtomicInteger ();
219
223
final AtomicInteger c2 = new AtomicInteger ();
@@ -243,7 +247,9 @@ public String call(String s) {
243
247
verify (stringObserver , never ()).onCompleted ();
244
248
verify (stringObserver , times (1 )).onError (any (Throwable .class ));
245
249
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.
247
253
assertEquals (1 , c1 .get ());
248
254
assertEquals (1 , c2 .get ());
249
255
}
@@ -282,6 +288,52 @@ public String call(String arg0) {
282
288
inorder .verify (stringObserver , times (1 )).onError (any (IllegalArgumentException .class ));
283
289
inorder .verifyNoMoreInteractions ();
284
290
}
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
+ }
285
337
286
338
private static Map <String , String > getMap (String prefix ) {
287
339
Map <String , String > m = new HashMap <String , String >();
0 commit comments