Debian Codegraph @

src/test/java/io/reactivex/rxjava3/parallel/ParallelMapTryTest.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.parallel;
15
16 import java.util.*;
17
18 import org.junit.Test;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.*;
22 import io.reactivex.rxjava3.functions.*;
23 import io.reactivex.rxjava3.internal.functions.Functions;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25 import io.reactivex.rxjava3.testsupport.*;
26
27 public class ParallelMapTryTest extends RxJavaTest implements Consumer<Object> {
28
29 volatile int calls;
30
31 @Override
32 public void accept(Object t) throws Exception {
33 calls++;
34 }
35
36 @Test
37 public void mapNoError() {
38 for (ParallelFailureHandling e : ParallelFailureHandling.values()) {
39 Flowable.just(1)
40 .parallel(1)
41 .map(Functions.identity(), e)
42 .sequential()
43 .test()
44 .assertResult(1);
45 }
46 }
47
48 @Test
49 public void mapErrorNoError() {
50 for (ParallelFailureHandling e : ParallelFailureHandling.values()) {
51 Flowable.<Integer>error(new TestException())
52 .parallel(1)
53 .map(Functions.identity(), e)
54 .sequential()
55 .test()
56 .assertFailure(TestException.class);
57 }
58 }
59
60 @Test
61 public void mapConditionalNoError() {
62 for (ParallelFailureHandling e : ParallelFailureHandling.values()) {
63 Flowable.just(1)
64 .parallel(1)
65 .map(Functions.identity(), e)
66 .filter(Functions.alwaysTrue())
67 .sequential()
68 .test()
69 .assertResult(1);
70 }
71 }
72
73 @Test
74 public void mapErrorConditionalNoError() {
75 for (ParallelFailureHandling e : ParallelFailureHandling.values()) {
76 Flowable.<Integer>error(new TestException())
77 .parallel(1)
78 .map(Functions.identity(), e)
79 .filter(Functions.alwaysTrue())
80 .sequential()
81 .test()
82 .assertFailure(TestException.class);
83 }
84 }
85
86 @Test
87 public void mapFailWithError() {
88 Flowable.range(0, 2)
89 .parallel(1)
90 .map(new Function<Integer, Integer>() {
91 @Override
92 public Integer apply(Integer v) throws Exception {
93 return 1 / v;
94 }
95 }, ParallelFailureHandling.ERROR)
96 .sequential()
97 .test()
98 .assertFailure(ArithmeticException.class);
99 }
100
101 @Test
102 public void mapFailWithStop() {
103 Flowable.range(0, 2)
104 .parallel(1)
105 .map(new Function<Integer, Integer>() {
106 @Override
107 public Integer apply(Integer v) throws Exception {
108 return 1 / v;
109 }
110 }, ParallelFailureHandling.STOP)
111 .sequential()
112 .test()
113 .assertResult();
114 }
115
116 @Test
117 public void mapFailWithRetry() {
118 Flowable.range(0, 2)
119 .parallel(1)
120 .map(new Function<Integer, Integer>() {
121 int count;
122 @Override
123 public Integer apply(Integer v) throws Exception {
124 if (count++ == 1) {
125 return -1;
126 }
127 return 1 / v;
128 }
129 }, ParallelFailureHandling.RETRY)
130 .sequential()
131 .test()
132 .assertResult(-1, 1);
133 }
134
135 @Test
136 public void mapFailWithRetryLimited() {
137 Flowable.range(0, 2)
138 .parallel(1)
139 .map(new Function<Integer, Integer>() {
140 @Override
141 public Integer apply(Integer v) throws Exception {
142 return 1 / v;
143 }
144 }, new BiFunction<Long, Throwable, ParallelFailureHandling>() {
145 @Override
146 public ParallelFailureHandling apply(Long n, Throwable e) throws Exception {
147 return n < 5 ? ParallelFailureHandling.RETRY : ParallelFailureHandling.SKIP;
148 }
149 })
150 .sequential()
151 .test()
152 .assertResult(1);
153 }
154
155 @Test
156 public void mapFailWithSkip() {
157 Flowable.range(0, 2)
158 .parallel(1)
159 .map(new Function<Integer, Integer>() {
160 @Override
161 public Integer apply(Integer v) throws Exception {
162 return 1 / v;
163 }
164 }, ParallelFailureHandling.SKIP)
165 .sequential()
166 .test()
167 .assertResult(1);
168 }
169
170 @Test
171 public void mapFailHandlerThrows() {
172 TestSubscriberEx<Integer> ts = Flowable.range(0, 2)
173 .parallel(1)
174 .map(new Function<Integer, Integer>() {
175 @Override
176 public Integer apply(Integer v) throws Exception {
177 return 1 / v;
178 }
179 }, new BiFunction<Long, Throwable, ParallelFailureHandling>() {
180 @Override
181 public ParallelFailureHandling apply(Long n, Throwable e) throws Exception {
182 throw new TestException();
183 }
184 })
185 .sequential()
186 .to(TestHelper.<Integer>testConsumer())
187 .assertFailure(CompositeException.class);
188
189 TestHelper.assertCompositeExceptions(ts, ArithmeticException.class, TestException.class);
190 }
191
192 @Test
193 public void mapWrongParallelism() {
194 TestHelper.checkInvalidParallelSubscribers(
195 Flowable.just(1).parallel(1)
196 .map(Functions.identity(), ParallelFailureHandling.ERROR)
197 );
198 }
199
200 @Test
201 public void mapInvalidSource() {
202 List<Throwable> errors = TestHelper.trackPluginErrors();
203 try {
204 new ParallelInvalid()
205 .map(Functions.identity(), ParallelFailureHandling.ERROR)
206 .sequential()
207 .test();
208
209 TestHelper.assertUndeliverable(errors, 0, TestException.class);
210 } finally {
211 RxJavaPlugins.reset();
212 }
213 }
214
215 @Test
216 public void mapFailWithErrorConditional() {
217 Flowable.range(0, 2)
218 .parallel(1)
219 .map(new Function<Integer, Integer>() {
220 @Override
221 public Integer apply(Integer v) throws Exception {
222 return 1 / v;
223 }
224 }, ParallelFailureHandling.ERROR)
225 .filter(Functions.alwaysTrue())
226 .sequential()
227 .test()
228 .assertFailure(ArithmeticException.class);
229 }
230
231 @Test
232 public void mapFailWithStopConditional() {
233 Flowable.range(0, 2)
234 .parallel(1)
235 .map(new Function<Integer, Integer>() {
236 @Override
237 public Integer apply(Integer v) throws Exception {
238 return 1 / v;
239 }
240 }, ParallelFailureHandling.STOP)
241 .filter(Functions.alwaysTrue())
242 .sequential()
243 .test()
244 .assertResult();
245 }
246
247 @Test
248 public void mapFailWithRetryConditional() {
249 Flowable.range(0, 2)
250 .parallel(1)
251 .map(new Function<Integer, Integer>() {
252 int count;
253 @Override
254 public Integer apply(Integer v) throws Exception {
255 if (count++ == 1) {
256 return -1;
257 }
258 return 1 / v;
259 }
260 }, ParallelFailureHandling.RETRY)
261 .filter(Functions.alwaysTrue())
262 .sequential()
263 .test()
264 .assertResult(-1, 1);
265 }
266
267 @Test
268 public void mapFailWithRetryLimitedConditional() {
269 Flowable.range(0, 2)
270 .parallel(1)
271 .map(new Function<Integer, Integer>() {
272 @Override
273 public Integer apply(Integer v) throws Exception {
274 return 1 / v;
275 }
276 }, new BiFunction<Long, Throwable, ParallelFailureHandling>() {
277 @Override
278 public ParallelFailureHandling apply(Long n, Throwable e) throws Exception {
279 return n < 5 ? ParallelFailureHandling.RETRY : ParallelFailureHandling.SKIP;
280 }
281 })
282 .filter(Functions.alwaysTrue())
283 .sequential()
284 .test()
285 .assertResult(1);
286 }
287
288 @Test
289 public void mapFailWithSkipConditional() {
290 Flowable.range(0, 2)
291 .parallel(1)
292 .map(new Function<Integer, Integer>() {
293 @Override
294 public Integer apply(Integer v) throws Exception {
295 return 1 / v;
296 }
297 }, ParallelFailureHandling.SKIP)
298 .filter(Functions.alwaysTrue())
299 .sequential()
300 .test()
301 .assertResult(1);
302 }
303
304 @Test
305 public void mapFailHandlerThrowsConditional() {
306 TestSubscriberEx<Integer> ts = Flowable.range(0, 2)
307 .parallel(1)
308 .map(new Function<Integer, Integer>() {
309 @Override
310 public Integer apply(Integer v) throws Exception {
311 return 1 / v;
312 }
313 }, new BiFunction<Long, Throwable, ParallelFailureHandling>() {
314 @Override
315 public ParallelFailureHandling apply(Long n, Throwable e) throws Exception {
316 throw new TestException();
317 }
318 })
319 .filter(Functions.alwaysTrue())
320 .sequential()
321 .to(TestHelper.<Integer>testConsumer())
322 .assertFailure(CompositeException.class);
323
324 TestHelper.assertCompositeExceptions(ts, ArithmeticException.class, TestException.class);
325 }
326
327 @Test
328 public void mapWrongParallelismConditional() {
329 TestHelper.checkInvalidParallelSubscribers(
330 Flowable.just(1).parallel(1)
331 .map(Functions.identity(), ParallelFailureHandling.ERROR)
332 .filter(Functions.alwaysTrue())
333 );
334 }
335
336 @Test
337 public void mapInvalidSourceConditional() {
338 List<Throwable> errors = TestHelper.trackPluginErrors();
339 try {
340 new ParallelInvalid()
341 .map(Functions.identity(), ParallelFailureHandling.ERROR)
342 .filter(Functions.alwaysTrue())
343 .sequential()
344 .test();
345
346 TestHelper.assertUndeliverable(errors, 0, TestException.class);
347 } finally {
348 RxJavaPlugins.reset();
349 }
350 }
351
352 @Test
353 public void failureHandlingEnum() {
354 TestHelper.checkEnum(ParallelFailureHandling.class);
355 }
356
357 @Test
358 public void invalidSubscriberCount() {
359 TestHelper.checkInvalidParallelSubscribers(
360 Flowable.range(1, 10).parallel()
361 .map(v -> v, ParallelFailureHandling.SKIP)
362 );
363 }
364
365 @Test
366 public void doubleOnSubscribe() {
367 TestHelper.checkDoubleOnSubscribeParallel(
368 p -> p.map(v -> v, ParallelFailureHandling.ERROR)
369 );
370
371 TestHelper.checkDoubleOnSubscribeParallel(
372 p -> p.map(v -> v, ParallelFailureHandling.ERROR)
373 .filter(v -> true)
374 );
375 }
376 }