Debian Codegraph @

src/test/java/io/reactivex/rxjava3/parallel/ParallelReduceTest.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.*;
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.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
26 import io.reactivex.rxjava3.processors.PublishProcessor;
27 import io.reactivex.rxjava3.subscribers.TestSubscriber;
28 import io.reactivex.rxjava3.testsupport.TestHelper;
29
30 public class ParallelReduceTest extends RxJavaTest {
31
32 @Test
33 public void subscriberCount() {
34 ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel()
35 .reduce(new Supplier<List<Integer>>() {
36 @Override
37 public List<Integer> get() throws Exception {
38 return new ArrayList<>();
39 }
40 }, new BiFunction<List<Integer>, Integer, List<Integer>>() {
41 @Override
42 public List<Integer> apply(List<Integer> a, Integer b) throws Exception {
43 a.add(b);
44 return a;
45 }
46 }));
47 }
48
49 @Test
50 public void initialCrash() {
51 Flowable.range(1, 5)
52 .parallel()
53 .reduce(new Supplier<List<Integer>>() {
54 @Override
55 public List<Integer> get() throws Exception {
56 throw new TestException();
57 }
58 }, new BiFunction<List<Integer>, Integer, List<Integer>>() {
59 @Override
60 public List<Integer> apply(List<Integer> a, Integer b) throws Exception {
61 a.add(b);
62 return a;
63 }
64 })
65 .sequential()
66 .test()
67 .assertFailure(TestException.class);
68 }
69
70 @Test
71 public void reducerCrash() {
72 Flowable.range(1, 5)
73 .parallel()
74 .reduce(new Supplier<List<Integer>>() {
75 @Override
76 public List<Integer> get() throws Exception {
77 return new ArrayList<>();
78 }
79 }, new BiFunction<List<Integer>, Integer, List<Integer>>() {
80 @Override
81 public List<Integer> apply(List<Integer> a, Integer b) throws Exception {
82 if (b == 3) {
83 throw new TestException();
84 }
85 a.add(b);
86 return a;
87 }
88 })
89 .sequential()
90 .test()
91 .assertFailure(TestException.class);
92 }
93
94 @Test
95 public void cancel() {
96 PublishProcessor<Integer> pp = PublishProcessor.create();
97
98 TestSubscriber<List<Integer>> ts = pp
99 .parallel()
100 .reduce(new Supplier<List<Integer>>() {
101 @Override
102 public List<Integer> get() throws Exception {
103 return new ArrayList<>();
104 }
105 }, new BiFunction<List<Integer>, Integer, List<Integer>>() {
106 @Override
107 public List<Integer> apply(List<Integer> a, Integer b) throws Exception {
108 a.add(b);
109 return a;
110 }
111 })
112 .sequential()
113 .test();
114
115 assertTrue(pp.hasSubscribers());
116
117 ts.cancel();
118
119 assertFalse(pp.hasSubscribers());
120 }
121
122 @Test
123 public void error() {
124 Flowable.<Integer>error(new TestException())
125 .parallel()
126 .reduce(new Supplier<List<Integer>>() {
127 @Override
128 public List<Integer> get() throws Exception {
129 return new ArrayList<>();
130 }
131 }, new BiFunction<List<Integer>, Integer, List<Integer>>() {
132 @Override
133 public List<Integer> apply(List<Integer> a, Integer b) throws Exception {
134 a.add(b);
135 return a;
136 }
137 })
138 .sequential()
139 .test()
140 .assertFailure(TestException.class);
141 }
142
143 @Test
144 public void doubleError() {
145 List<Throwable> errors = TestHelper.trackPluginErrors();
146 try {
147 new ParallelInvalid()
148 .reduce(new Supplier<List<Object>>() {
149 @Override
150 public List<Object> get() throws Exception {
151 return new ArrayList<>();
152 }
153 }, new BiFunction<List<Object>, Object, List<Object>>() {
154 @Override
155 public List<Object> apply(List<Object> a, Object b) throws Exception {
156 a.add(b);
157 return a;
158 }
159 })
160 .sequential()
161 .test()
162 .assertFailure(TestException.class);
163
164 assertFalse(errors.isEmpty());
165 for (Throwable ex : errors) {
166 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
167 }
168 } finally {
169 RxJavaPlugins.reset();
170 }
171 }
172
173 @Test
174 public void doubleOnSubscribe() {
175 TestHelper.checkDoubleOnSubscribeParallel(
176 pf -> pf.reduce(ArrayList::new, (a, b) -> a)
177 );
178 }
179 }