forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathObservableTest.java
1225 lines (1037 loc) · 40.1 KB
/
ObservableTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.observable;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import org.junit.*;
import org.mockito.InOrder;
import io.reactivex.*;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.*;
import io.reactivex.functions.*;
import io.reactivex.observables.ConnectableObservable;
import io.reactivex.observers.*;
import io.reactivex.schedulers.*;
import io.reactivex.subjects.*;
public class ObservableTest {
Observer<Number> w;
SingleObserver<Number> wo;
MaybeObserver<Number> wm;
private static final Predicate<Integer> IS_EVEN = new Predicate<Integer>() {
@Override
public boolean test(Integer v) {
return v % 2 == 0;
}
};
@Before
public void before() {
w = TestHelper.mockObserver();
wo = TestHelper.mockSingleObserver();
wm = TestHelper.mockMaybeObserver();
}
@Test
public void fromArray() {
String[] items = new String[] { "one", "two", "three" };
assertEquals((Long)3L, Observable.fromArray(items).count().blockingGet());
assertEquals("two", Observable.fromArray(items).skip(1).take(1).blockingSingle());
assertEquals("three", Observable.fromArray(items).takeLast(1).blockingSingle());
}
@Test
public void fromIterable() {
ArrayList<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");
assertEquals((Long)3L, Observable.fromIterable(items).count().blockingGet());
assertEquals("two", Observable.fromIterable(items).skip(1).take(1).blockingSingle());
assertEquals("three", Observable.fromIterable(items).takeLast(1).blockingSingle());
}
@Test
public void fromArityArgs3() {
Observable<String> items = Observable.just("one", "two", "three");
assertEquals((Long)3L, items.count().blockingGet());
assertEquals("two", items.skip(1).take(1).blockingSingle());
assertEquals("three", items.takeLast(1).blockingSingle());
}
@Test
public void fromArityArgs1() {
Observable<String> items = Observable.just("one");
assertEquals((Long)1L, items.count().blockingGet());
assertEquals("one", items.takeLast(1).blockingSingle());
}
@Test
public void testCreate() {
Observable<String> o = Observable.just("one", "two", "three");
Observer<String> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, times(1)).onNext("one");
verify(observer, times(1)).onNext("two");
verify(observer, times(1)).onNext("three");
verify(observer, never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testCountAFewItemsObservable() {
Observable<String> o = Observable.just("a", "b", "c", "d");
o.count().toObservable().subscribe(w);
// we should be called only once
verify(w, times(1)).onNext(anyLong());
verify(w).onNext(4L);
verify(w, never()).onError(any(Throwable.class));
verify(w, times(1)).onComplete();
}
@Test
public void testCountZeroItemsObservable() {
Observable<String> o = Observable.empty();
o.count().toObservable().subscribe(w);
// we should be called only once
verify(w, times(1)).onNext(anyLong());
verify(w).onNext(0L);
verify(w, never()).onError(any(Throwable.class));
verify(w, times(1)).onComplete();
}
@Test
public void testCountErrorObservable() {
Observable<String> o = Observable.error(new Callable<Throwable>() {
@Override
public Throwable call() {
return new RuntimeException();
}
});
o.count().toObservable().subscribe(w);
verify(w, never()).onNext(anyInt());
verify(w, never()).onComplete();
verify(w, times(1)).onError(any(RuntimeException.class));
}
@Test
public void testCountAFewItems() {
Observable<String> o = Observable.just("a", "b", "c", "d");
o.count().subscribe(wo);
// we should be called only once
verify(wo, times(1)).onSuccess(anyLong());
verify(wo).onSuccess(4L);
verify(wo, never()).onError(any(Throwable.class));
}
@Test
public void testCountZeroItems() {
Observable<String> o = Observable.empty();
o.count().subscribe(wo);
// we should be called only once
verify(wo, times(1)).onSuccess(anyLong());
verify(wo).onSuccess(0L);
verify(wo, never()).onError(any(Throwable.class));
}
@Test
public void testCountError() {
Observable<String> o = Observable.error(new Callable<Throwable>() {
@Override
public Throwable call() {
return new RuntimeException();
}
});
o.count().subscribe(wo);
verify(wo, never()).onSuccess(anyInt());
verify(wo, times(1)).onError(any(RuntimeException.class));
}
@Test
public void testTakeFirstWithPredicateOfSome() {
Observable<Integer> o = Observable.just(1, 3, 5, 4, 6, 3);
o.filter(IS_EVEN).take(1).subscribe(w);
verify(w, times(1)).onNext(anyInt());
verify(w).onNext(4);
verify(w, times(1)).onComplete();
verify(w, never()).onError(any(Throwable.class));
}
@Test
public void testTakeFirstWithPredicateOfNoneMatchingThePredicate() {
Observable<Integer> o = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
o.filter(IS_EVEN).take(1).subscribe(w);
verify(w, never()).onNext(anyInt());
verify(w, times(1)).onComplete();
verify(w, never()).onError(any(Throwable.class));
}
@Test
public void testTakeFirstOfSome() {
Observable<Integer> o = Observable.just(1, 2, 3);
o.take(1).subscribe(w);
verify(w, times(1)).onNext(anyInt());
verify(w).onNext(1);
verify(w, times(1)).onComplete();
verify(w, never()).onError(any(Throwable.class));
}
@Test
public void testTakeFirstOfNone() {
Observable<Integer> o = Observable.empty();
o.take(1).subscribe(w);
verify(w, never()).onNext(anyInt());
verify(w, times(1)).onComplete();
verify(w, never()).onError(any(Throwable.class));
}
@Test
public void testFirstOfNone() {
Observable<Integer> o = Observable.empty();
o.firstElement().subscribe(wm);
verify(wm, never()).onSuccess(anyInt());
verify(wm).onComplete();
verify(wm, never()).onError(any(Throwable.class));
}
@Test
public void testFirstWithPredicateOfNoneMatchingThePredicate() {
Observable<Integer> o = Observable.just(1, 3, 5, 7, 9, 7, 5, 3, 1);
o.filter(IS_EVEN).firstElement().subscribe(wm);
verify(wm, never()).onSuccess(anyInt());
verify(wm).onComplete();
verify(wm, never()).onError(any(Throwable.class));
}
@Test
public void testReduce() {
Observable<Integer> o = Observable.just(1, 2, 3, 4);
o.reduce(new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
})
.subscribe(wm);
// we should be called only once
verify(wm, times(1)).onSuccess(anyInt());
verify(wm).onSuccess(10);
verify(wm, never()).onError(any(Throwable.class));
verify(wm, never()).onComplete();
}
@Test
public void testReduceObservable() {
Observable<Integer> o = Observable.just(1, 2, 3, 4);
o.reduce(new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
})
.toObservable()
.subscribe(w);
// we should be called only once
verify(w, times(1)).onNext(anyInt());
verify(w).onNext(10);
verify(w, never()).onError(any(Throwable.class));
verify(w).onComplete();
}
@Test
public void testReduceWithEmptyObservable() {
Observable<Integer> o = Observable.range(1, 0);
o.reduce(new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
})
.toObservable()
.test()
.assertResult();
}
/**
* A reduce on an empty Observable and a seed should just pass the seed through.
*
* This is confirmed at https://github.com/ReactiveX/RxJava/issues/423#issuecomment-27642456
*/
@Test
public void testReduceWithEmptyObservableAndSeed() {
Observable<Integer> o = Observable.range(1, 0);
int value = o.reduce(1, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
})
.blockingGet();
assertEquals(1, value);
}
@Test
public void testReduceWithInitialValue() {
Observable<Integer> o = Observable.just(1, 2, 3, 4);
o.reduce(50, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
})
.subscribe(wo);
// we should be called only once
verify(wo, times(1)).onSuccess(anyInt());
verify(wo).onSuccess(60);
verify(wo, never()).onError(any(Throwable.class));
}
@Test
public void testReduceWithInitialValueObservable() {
Observable<Integer> o = Observable.just(1, 2, 3, 4);
o.reduce(50, new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer t1, Integer t2) {
return t1 + t2;
}
})
.toObservable()
.subscribe(w);
// we should be called only once
verify(w, times(1)).onNext(anyInt());
verify(w).onNext(60);
}
@Ignore("Throwing is not allowed from the unsafeCreate?!")
@Test // FIXME throwing is not allowed from the create?!
public void testOnSubscribeFails() {
Observer<String> observer = TestHelper.mockObserver();
final RuntimeException re = new RuntimeException("bad impl");
Observable<String> o = Observable.unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> s) { throw re; }
});
o.subscribe(observer);
verify(observer, times(0)).onNext(anyString());
verify(observer, times(0)).onComplete();
verify(observer, times(1)).onError(re);
}
@Test
public void testMaterializeDematerializeChaining() {
Observable<Integer> obs = Observable.just(1);
Observable<Integer> chained = obs.materialize().dematerialize();
Observer<Integer> observer = TestHelper.mockObserver();
chained.subscribe(observer);
verify(observer, times(1)).onNext(1);
verify(observer, times(1)).onComplete();
verify(observer, times(0)).onError(any(Throwable.class));
}
/**
* The error from the user provided Observer is not handled by the subscribe method try/catch.
*
* It is handled by the AtomicObserver that wraps the provided Observer.
*
* Result: Passes (if AtomicObserver functionality exists)
* @throws InterruptedException if the test is interrupted
*/
@Test
public void testCustomObservableWithErrorInObserverAsynchronous() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger count = new AtomicInteger();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
// FIXME custom built???
Observable.just("1", "2", "three", "4")
.subscribeOn(Schedulers.newThread())
.safeSubscribe(new DefaultObserver<String>() {
@Override
public void onComplete() {
System.out.println("completed");
latch.countDown();
}
@Override
public void onError(Throwable e) {
error.set(e);
System.out.println("error");
e.printStackTrace();
latch.countDown();
}
@Override
public void onNext(String v) {
int num = Integer.parseInt(v);
System.out.println(num);
// doSomething(num);
count.incrementAndGet();
}
});
// wait for async sequence to complete
latch.await();
assertEquals(2, count.get());
assertNotNull(error.get());
if (!(error.get() instanceof NumberFormatException)) {
fail("It should be a NumberFormatException");
}
}
/**
* The error from the user provided Observer is handled by the subscribe try/catch because this is synchronous.
*
* Result: Passes
*/
@Test
public void testCustomObservableWithErrorInObserverSynchronous() {
final AtomicInteger count = new AtomicInteger();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
// FIXME custom built???
Observable.just("1", "2", "three", "4")
.safeSubscribe(new DefaultObserver<String>() {
@Override
public void onComplete() {
System.out.println("completed");
}
@Override
public void onError(Throwable e) {
error.set(e);
System.out.println("error");
e.printStackTrace();
}
@Override
public void onNext(String v) {
int num = Integer.parseInt(v);
System.out.println(num);
// doSomething(num);
count.incrementAndGet();
}
});
assertEquals(2, count.get());
assertNotNull(error.get());
if (!(error.get() instanceof NumberFormatException)) {
fail("It should be a NumberFormatException");
}
}
/**
* The error from the user provided Observable is handled by the subscribe try/catch because this is synchronous.
*
*
* Result: Passes
*/
@Test
public void testCustomObservableWithErrorInObservableSynchronous() {
final AtomicInteger count = new AtomicInteger();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
// FIXME custom built???
Observable.just("1", "2").concatWith(Observable.<String>error(new Callable<Throwable>() {
@Override
public Throwable call() {
return new NumberFormatException();
}
}))
.subscribe(new DefaultObserver<String>() {
@Override
public void onComplete() {
System.out.println("completed");
}
@Override
public void onError(Throwable e) {
error.set(e);
System.out.println("error");
e.printStackTrace();
}
@Override
public void onNext(String v) {
System.out.println(v);
count.incrementAndGet();
}
});
assertEquals(2, count.get());
assertNotNull(error.get());
if (!(error.get() instanceof NumberFormatException)) {
fail("It should be a NumberFormatException");
}
}
@Test
public void testPublishLast() throws InterruptedException {
final AtomicInteger count = new AtomicInteger();
ConnectableObservable<String> connectable = Observable.<String>unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> observer) {
observer.onSubscribe(Disposables.empty());
count.incrementAndGet();
new Thread(new Runnable() {
@Override
public void run() {
observer.onNext("first");
observer.onNext("last");
observer.onComplete();
}
}).start();
}
}).takeLast(1).publish();
// subscribe once
final CountDownLatch latch = new CountDownLatch(1);
connectable.subscribe(new Consumer<String>() {
@Override
public void accept(String value) {
assertEquals("last", value);
latch.countDown();
}
});
// subscribe twice
connectable.subscribe();
Disposable subscription = connectable.connect();
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
assertEquals(1, count.get());
subscription.dispose();
}
@Test
public void testReplay() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
ConnectableObservable<String> o = Observable.<String>unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> observer) {
observer.onSubscribe(Disposables.empty());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
observer.onNext("one");
observer.onComplete();
}
}).start();
}
}).replay();
// we connect immediately and it will emit the value
Disposable s = o.connect();
try {
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
// subscribe again
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
} finally {
s.dispose();
}
}
@Test
public void testCache() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Observable<String> o = Observable.<String>unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> observer) {
observer.onSubscribe(Disposables.empty());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
observer.onNext("one");
observer.onComplete();
}
}).start();
}
}).cache();
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
// subscribe again
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
}
@Test
public void testCacheWithCapacity() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Observable<String> o = Observable.<String>unsafeCreate(new ObservableSource<String>() {
@Override
public void subscribe(final Observer<? super String> observer) {
observer.onSubscribe(Disposables.empty());
new Thread(new Runnable() {
@Override
public void run() {
counter.incrementAndGet();
observer.onNext("one");
observer.onComplete();
}
}).start();
}
}).cacheWithInitialCapacity(1);
// we then expect the following 2 subscriptions to get that same value
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
// subscribe again
o.subscribe(new Consumer<String>() {
@Override
public void accept(String v) {
assertEquals("one", v);
latch.countDown();
}
});
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
}
/**
* https://github.com/ReactiveX/RxJava/issues/198
*
* Rx Design Guidelines 5.2
*
* "when calling the Subscribe method that only has an onNext argument, the OnError behavior will be
* to rethrow the exception on the thread that the message comes out from the Observable.
* The OnCompleted behavior in this case is to do nothing."
*/
@Test
@Ignore("Subscribers can't throw")
public void testErrorThrownWithoutErrorHandlerSynchronous() {
try {
Observable.error(new RuntimeException("failure"))
.subscribe();
fail("expected exception");
} catch (Throwable e) {
assertEquals("failure", e.getMessage());
}
}
/**
* https://github.com/ReactiveX/RxJava/issues/198
*
* Rx Design Guidelines 5.2
*
* "when calling the Subscribe method that only has an onNext argument, the OnError behavior will be
* to rethrow the exception on the thread that the message comes out from the Observable.
* The OnCompleted behavior in this case is to do nothing."
*
* @throws InterruptedException if the await is interrupted
*/
@Test
@Ignore("Subscribers can't throw")
public void testErrorThrownWithoutErrorHandlerAsynchronous() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
Observable.unsafeCreate(new ObservableSource<Object>() {
@Override
public void subscribe(final Observer<? super Object> observer) {
new Thread(new Runnable() {
@Override
public void run() {
try {
observer.onError(new Error("failure"));
} catch (Throwable e) {
// without an onError handler it has to just throw on whatever thread invokes it
exception.set(e);
}
latch.countDown();
}
}).start();
}
}).subscribe();
// wait for exception
latch.await(3000, TimeUnit.MILLISECONDS);
assertNotNull(exception.get());
assertEquals("failure", exception.get().getMessage());
}
@Test
public void testTakeWithErrorInObserver() {
final AtomicInteger count = new AtomicInteger();
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
Observable.just("1", "2", "three", "4").take(3)
.safeSubscribe(new DefaultObserver<String>() {
@Override
public void onComplete() {
System.out.println("completed");
}
@Override
public void onError(Throwable e) {
error.set(e);
System.out.println("error");
e.printStackTrace();
}
@Override
public void onNext(String v) {
int num = Integer.parseInt(v);
System.out.println(num);
// doSomething(num);
count.incrementAndGet();
}
});
assertEquals(2, count.get());
assertNotNull(error.get());
if (!(error.get() instanceof NumberFormatException)) {
fail("It should be a NumberFormatException");
}
}
@Test
public void testOfType() {
Observable<String> o = Observable.just(1, "abc", false, 2L).ofType(String.class);
Observer<Object> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, never()).onNext(1);
verify(observer, times(1)).onNext("abc");
verify(observer, never()).onNext(false);
verify(observer, never()).onNext(2L);
verify(observer, never()).onError(
any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testOfTypeWithPolymorphism() {
ArrayList<Integer> l1 = new ArrayList<Integer>();
l1.add(1);
LinkedList<Integer> l2 = new LinkedList<Integer>();
l2.add(2);
@SuppressWarnings("rawtypes")
Observable<List> o = Observable.<Object> just(l1, l2, "123").ofType(List.class);
Observer<Object> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, times(1)).onNext(l1);
verify(observer, times(1)).onNext(l2);
verify(observer, never()).onNext("123");
verify(observer, never()).onError(
any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testContainsObservable() {
Observable<Boolean> o = Observable.just("a", "b", "c").contains("b").toObservable();
Observer<Boolean> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, times(1)).onNext(true);
verify(observer, never()).onNext(false);
verify(observer, never()).onError(
any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testContainsWithInexistenceObservable() {
Observable<Boolean> o = Observable.just("a", "b").contains("c").toObservable();
Observer<Object> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, times(1)).onNext(false);
verify(observer, never()).onNext(true);
verify(observer, never()).onError(
any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
@Ignore("null values are not allowed")
public void testContainsWithNullObservable() {
Observable<Boolean> o = Observable.just("a", "b", null).contains(null).toObservable();
Observer<Object> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, times(1)).onNext(true);
verify(observer, never()).onNext(false);
verify(observer, never()).onError(
any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testContainsWithEmptyObservableObservable() {
Observable<Boolean> o = Observable.<String> empty().contains("a").toObservable();
Observer<Object> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, times(1)).onNext(false);
verify(observer, never()).onNext(true);
verify(observer, never()).onError(
any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testContains() {
Single<Boolean> o = Observable.just("a", "b", "c").contains("b"); // FIXME nulls not allowed, changed to "c"
SingleObserver<Boolean> observer = TestHelper.mockSingleObserver();
o.subscribe(observer);
verify(observer, times(1)).onSuccess(true);
verify(observer, never()).onSuccess(false);
verify(observer, never()).onError(
any(Throwable.class));
}
@Test
public void testContainsWithInexistence() {
Single<Boolean> o = Observable.just("a", "b").contains("c"); // FIXME null values are not allowed, removed
SingleObserver<Object> observer = TestHelper.mockSingleObserver();
o.subscribe(observer);
verify(observer, times(1)).onSuccess(false);
verify(observer, never()).onSuccess(true);
verify(observer, never()).onError(
any(Throwable.class));
}
@Test
@Ignore("null values are not allowed")
public void testContainsWithNull() {
Single<Boolean> o = Observable.just("a", "b", null).contains(null);
SingleObserver<Object> observer = TestHelper.mockSingleObserver();
o.subscribe(observer);
verify(observer, times(1)).onSuccess(true);
verify(observer, never()).onSuccess(false);
verify(observer, never()).onError(
any(Throwable.class));
}
@Test
public void testContainsWithEmptyObservable() {
Single<Boolean> o = Observable.<String> empty().contains("a");
SingleObserver<Object> observer = TestHelper.mockSingleObserver();
o.subscribe(observer);
verify(observer, times(1)).onSuccess(false);
verify(observer, never()).onSuccess(true);
verify(observer, never()).onError(
any(Throwable.class));
}
@Test
public void testIgnoreElements() {
Completable o = Observable.just(1, 2, 3).ignoreElements();
CompletableObserver observer = TestHelper.mockCompletableObserver();
o.subscribe(observer);
verify(observer, never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testIgnoreElementsObservable() {
Observable<Integer> o = Observable.just(1, 2, 3).ignoreElements().toObservable();
Observer<Object> observer = TestHelper.mockObserver();
o.subscribe(observer);
verify(observer, never()).onNext(any(Integer.class));
verify(observer, never()).onError(any(Throwable.class));
verify(observer, times(1)).onComplete();
}
@Test
public void testJustWithScheduler() {
TestScheduler scheduler = new TestScheduler();
Observable<Integer> o = Observable.fromArray(1, 2).subscribeOn(scheduler);
Observer<Integer> observer = TestHelper.mockObserver();
o.subscribe(observer);
scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onNext(1);
inOrder.verify(observer, times(1)).onNext(2);
inOrder.verify(observer, times(1)).onComplete();
inOrder.verifyNoMoreInteractions();
}
@Test
public void testStartWithWithScheduler() {
TestScheduler scheduler = new TestScheduler();
Observable<Integer> o = Observable.just(3, 4).startWith(Arrays.asList(1, 2)).subscribeOn(scheduler);
Observer<Integer> observer = TestHelper.mockObserver();
o.subscribe(observer);
scheduler.advanceTimeBy(1, TimeUnit.MILLISECONDS);
InOrder inOrder = inOrder(observer);
inOrder.verify(observer, times(1)).onNext(1);
inOrder.verify(observer, times(1)).onNext(2);