Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleZipIterableTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.single;
15
16 import static org.junit.Assert.*;
17
18 import java.util.*;
19
20 import org.junit.Test;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.exceptions.TestException;
24 import io.reactivex.rxjava3.functions.Function;
25 import io.reactivex.rxjava3.internal.functions.Functions;
26 import io.reactivex.rxjava3.internal.util.CrashingMappedIterable;
27 import io.reactivex.rxjava3.observers.TestObserver;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.processors.PublishProcessor;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class SingleZipIterableTest extends RxJavaTest {
33
34 final Function<Object[], Object> addString = new Function<Object[], Object>() {
35 @Override
36 public Object apply(Object[] a) throws Exception {
37 return Arrays.toString(a);
38 }
39 };
40
41 @Test
42 public void firstError() {
43 Single.zip(Arrays.asList(Single.error(new TestException()), Single.just(1)), addString)
44 .test()
45 .assertFailure(TestException.class);
46 }
47
48 @Test
49 public void secondError() {
50 Single.zip(Arrays.asList(Single.just(1), Single.<Integer>error(new TestException())), addString)
51 .test()
52 .assertFailure(TestException.class);
53 }
54
55 @Test
56 public void dispose() {
57 PublishProcessor<Integer> pp = PublishProcessor.create();
58
59 TestObserver<Object> to = Single.zip(Arrays.asList(pp.single(0), pp.single(0)), addString)
60 .test();
61
62 assertTrue(pp.hasSubscribers());
63
64 to.dispose();
65
66 assertFalse(pp.hasSubscribers());
67 }
68
69 @Test
70 public void zipperThrows() {
71 Single.zip(Arrays.asList(Single.just(1), Single.just(2)), new Function<Object[], Object>() {
72 @Override
73 public Object apply(Object[] b) throws Exception {
74 throw new TestException();
75 }
76 })
77 .test()
78 .assertFailure(TestException.class);
79 }
80
81 @Test
82 public void zipperReturnsNull() {
83 Single.zip(Arrays.asList(Single.just(1), Single.just(2)), new Function<Object[], Object>() {
84 @Override
85 public Object apply(Object[] a) throws Exception {
86 return null;
87 }
88 })
89 .test()
90 .assertFailure(NullPointerException.class);
91 }
92
93 @Test
94 public void middleError() {
95 PublishProcessor<Integer> pp0 = PublishProcessor.create();
96 PublishProcessor<Integer> pp1 = PublishProcessor.create();
97
98 TestObserver<Object> to = Single.zip(
99 Arrays.asList(pp0.single(0), pp1.single(0), pp0.single(0)), addString)
100 .test();
101
102 pp1.onError(new TestException());
103
104 assertFalse(pp0.hasSubscribers());
105
106 to.assertFailure(TestException.class);
107 }
108
109 @Test
110 public void innerErrorRace() {
111 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
112 List<Throwable> errors = TestHelper.trackPluginErrors();
113 try {
114 final PublishProcessor<Integer> pp0 = PublishProcessor.create();
115 final PublishProcessor<Integer> pp1 = PublishProcessor.create();
116
117 final TestObserver<Object> to = Single.zip(
118 Arrays.asList(pp0.single(0), pp1.single(0)), addString)
119 .test();
120
121 final TestException ex = new TestException();
122
123 Runnable r1 = new Runnable() {
124 @Override
125 public void run() {
126 pp0.onError(ex);
127 }
128 };
129
130 Runnable r2 = new Runnable() {
131 @Override
132 public void run() {
133 pp1.onError(ex);
134 }
135 };
136
137 TestHelper.race(r1, r2);
138
139 to.assertFailure(TestException.class);
140
141 if (!errors.isEmpty()) {
142 TestHelper.assertUndeliverable(errors, 0, TestException.class);
143 }
144 } finally {
145 RxJavaPlugins.reset();
146 }
147 }
148 }
149
150 @Test
151 public void iteratorThrows() {
152 Single.zip(new CrashingMappedIterable<>(1, 100, 100, new Function<Integer, Single<Integer>>() {
153 @Override
154 public Single<Integer> apply(Integer v) throws Exception {
155 return Single.just(v);
156 }
157 }), addString)
158 .to(TestHelper.<Object>testConsumer())
159 .assertFailureAndMessage(TestException.class, "iterator()");
160 }
161
162 @Test
163 public void hasNextThrows() {
164 Single.zip(new CrashingMappedIterable<>(100, 20, 100, new Function<Integer, Single<Integer>>() {
165 @Override
166 public Single<Integer> apply(Integer v) throws Exception {
167 return Single.just(v);
168 }
169 }), addString)
170 .to(TestHelper.<Object>testConsumer())
171 .assertFailureAndMessage(TestException.class, "hasNext()");
172 }
173
174 @Test
175 public void nextThrows() {
176 Single.zip(new CrashingMappedIterable<>(100, 100, 5, new Function<Integer, Single<Integer>>() {
177 @Override
178 public Single<Integer> apply(Integer v) throws Exception {
179 return Single.just(v);
180 }
181 }), addString)
182 .to(TestHelper.<Object>testConsumer())
183 .assertFailureAndMessage(TestException.class, "next()");
184 }
185
186 @Test(expected = NullPointerException.class)
187 public void zipIterableOneIsNull() {
188 Single.zip(Arrays.asList(null, Single.just(1)), new Function<Object[], Object>() {
189 @Override
190 public Object apply(Object[] v) {
191 return 1;
192 }
193 })
194 .blockingGet();
195 }
196
197 @Test(expected = NullPointerException.class)
198 public void zipIterableTwoIsNull() {
199 Single.zip(Arrays.asList(Single.just(1), null), new Function<Object[], Object>() {
200 @Override
201 public Object apply(Object[] v) {
202 return 1;
203 }
204 })
205 .blockingGet();
206 }
207
208 @Test
209 public void emptyIterable() {
210 Single.zip(Collections.<SingleSource<Integer>>emptyList(), new Function<Object[], Object[]>() {
211 @Override
212 public Object[] apply(Object[] a) throws Exception {
213 return a;
214 }
215 })
216 .test()
217 .assertFailure(NoSuchElementException.class);
218 }
219
220 @Test
221 public void oneIterable() {
222 Single.zip(Collections.singleton(Single.just(1)), new Function<Object[], Object>() {
223 @Override
224 public Object apply(Object[] a) throws Exception {
225 return (Integer)a[0] + 1;
226 }
227 })
228 .test()
229 .assertResult(2);
230 }
231
232 @Test
233 public void singleSourceZipperReturnsNull() {
234 Single.zip(Arrays.asList(Single.just(1)), Functions.justFunction(null))
235 .to(TestHelper.<Object>testConsumer())
236 .assertFailureAndMessage(NullPointerException.class, "The zipper returned a null value");
237 }
238
239 @Test
240 public void singleSourcesInIterable() {
241 SingleSource<Integer> source = new SingleSource<Integer>() {
242 @Override
243 public void subscribe(SingleObserver<? super Integer> observer) {
244 Single.just(1).subscribe(observer);
245 }
246 };
247
248 Single.zip(Arrays.asList(source, source), new Function<Object[], Integer>() {
249 @Override
250 public Integer apply(Object[] t) throws Throwable {
251 return 2;
252 }
253 })
254 .test()
255 .assertResult(2);
256 }
257 }