Debian Codegraph @

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