|
22 | 22 | import java.util.ArrayList;
|
23 | 23 | import java.util.Arrays;
|
24 | 24 | import java.util.Collection;
|
| 25 | +import java.util.Collections; |
25 | 26 | import java.util.List;
|
26 | 27 | import java.util.concurrent.CountDownLatch;
|
27 | 28 | import java.util.concurrent.TimeUnit;
|
|
42 | 43 | import rx.functions.Func3;
|
43 | 44 | import rx.functions.FuncN;
|
44 | 45 | import rx.functions.Functions;
|
| 46 | +import rx.observers.TestSubscriber; |
45 | 47 | import rx.subjects.PublishSubject;
|
46 | 48 | import rx.subscriptions.Subscriptions;
|
47 | 49 |
|
@@ -986,6 +988,72 @@ public void call(String s) {
|
986 | 988 | assertEquals("OnCompleted_null-OnCompleted_null", list.get(3));
|
987 | 989 | }
|
988 | 990 |
|
| 991 | + @Test |
| 992 | + public void testZipEmptyObservables() { |
| 993 | + |
| 994 | + Observable<String> o = Observable.zip(Observable.<Integer> empty(), Observable.<String> empty(), new Func2<Integer, String, String>() { |
| 995 | + |
| 996 | + @Override |
| 997 | + public String call(Integer t1, String t2) { |
| 998 | + return t1 + "-" + t2; |
| 999 | + } |
| 1000 | + |
| 1001 | + }); |
| 1002 | + |
| 1003 | + final ArrayList<String> list = new ArrayList<String>(); |
| 1004 | + o.subscribe(new Action1<String>() { |
| 1005 | + |
| 1006 | + @Override |
| 1007 | + public void call(String s) { |
| 1008 | + System.out.println(s); |
| 1009 | + list.add(s); |
| 1010 | + } |
| 1011 | + }); |
| 1012 | + |
| 1013 | + assertEquals(0, list.size()); |
| 1014 | + } |
| 1015 | + |
| 1016 | + @Test |
| 1017 | + public void testZipEmptyList() { |
| 1018 | + |
| 1019 | + final Object invoked = new Object(); |
| 1020 | + Collection<Observable<Object>> observables = Collections.emptyList(); |
| 1021 | + |
| 1022 | + Observable<Object> o = Observable.zip(observables, new FuncN<Object>() { |
| 1023 | + @Override |
| 1024 | + public Object call(final Object... args) { |
| 1025 | + assertEquals("No argument should have been passed", 0, args.length); |
| 1026 | + return invoked; |
| 1027 | + } |
| 1028 | + }); |
| 1029 | + |
| 1030 | + TestSubscriber<Object> ts = new TestSubscriber<Object>(); |
| 1031 | + o.subscribe(ts); |
| 1032 | + ts.awaitTerminalEvent(200, TimeUnit.MILLISECONDS); |
| 1033 | + ts.assertReceivedOnNext(Collections.emptyList()); |
| 1034 | + } |
| 1035 | + |
| 1036 | + /** |
| 1037 | + * Expect IllegalArgumentException instead of blocking forever as zip should emit onCompleted and no onNext |
| 1038 | + * and last() expects at least a single response. |
| 1039 | + */ |
| 1040 | + @Test(expected = IllegalArgumentException.class) |
| 1041 | + public void testZipEmptyListBlocking() { |
| 1042 | + |
| 1043 | + final Object invoked = new Object(); |
| 1044 | + Collection<Observable<Object>> observables = Collections.emptyList(); |
| 1045 | + |
| 1046 | + Observable<Object> o = Observable.zip(observables, new FuncN<Object>() { |
| 1047 | + @Override |
| 1048 | + public Object call(final Object... args) { |
| 1049 | + assertEquals("No argument should have been passed", 0, args.length); |
| 1050 | + return invoked; |
| 1051 | + } |
| 1052 | + }); |
| 1053 | + |
| 1054 | + o.toBlockingObservable().last(); |
| 1055 | + } |
| 1056 | + |
989 | 1057 | Observable<Integer> OBSERVABLE_OF_5_INTEGERS = OBSERVABLE_OF_5_INTEGERS(new AtomicInteger());
|
990 | 1058 |
|
991 | 1059 | Observable<Integer> OBSERVABLE_OF_5_INTEGERS(final AtomicInteger numEmitted) {
|
|
0 commit comments