Debian Codegraph @

src/test/java/io/reactivex/rxjava3/parallel/ParallelMapTest.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.List;
19 import java.util.concurrent.TimeUnit;
20
21 import org.junit.Test;
22 import org.reactivestreams.Subscriber;
23
24 import io.reactivex.rxjava3.annotations.NonNull;
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.TestException;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
31 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
32 import io.reactivex.rxjava3.schedulers.Schedulers;
33 import io.reactivex.rxjava3.testsupport.TestHelper;
34
35 public class ParallelMapTest extends RxJavaTest {
36
37 @Test
38 public void subscriberCount() {
39 ParallelFlowableTest.checkSubscriberCount(Flowable.range(1, 5).parallel()
40 .map(Functions.identity()));
41 }
42
43 @Test
44 public void doubleFilter() {
45 Flowable.range(1, 10)
46 .parallel()
47 .map(Functions.<Integer>identity())
48 .filter(new Predicate<Integer>() {
49 @Override
50 public boolean test(Integer v) throws Exception {
51 return v % 2 == 0;
52 }
53 })
54 .filter(new Predicate<Integer>() {
55 @Override
56 public boolean test(Integer v) throws Exception {
57 return v % 3 == 0;
58 }
59 })
60 .sequential()
61 .test()
62 .assertResult(6);
63 }
64
65 @Test
66 public void doubleFilterAsync() {
67 Flowable.range(1, 10)
68 .parallel()
69 .runOn(Schedulers.computation())
70 .map(Functions.<Integer>identity())
71 .filter(new Predicate<Integer>() {
72 @Override
73 public boolean test(Integer v) throws Exception {
74 return v % 2 == 0;
75 }
76 })
77 .filter(new Predicate<Integer>() {
78 @Override
79 public boolean test(Integer v) throws Exception {
80 return v % 3 == 0;
81 }
82 })
83 .sequential()
84 .test()
85 .awaitDone(5, TimeUnit.SECONDS)
86 .assertResult(6);
87 }
88
89 @Test
90 public void doubleError() {
91 List<Throwable> errors = TestHelper.trackPluginErrors();
92 try {
93 new ParallelInvalid()
94 .map(Functions.<Object>identity())
95 .sequential()
96 .test()
97 .assertFailure(TestException.class);
98
99 assertFalse(errors.isEmpty());
100 for (Throwable ex : errors) {
101 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
102 }
103 } finally {
104 RxJavaPlugins.reset();
105 }
106 }
107
108 @Test
109 public void doubleError2() {
110 List<Throwable> errors = TestHelper.trackPluginErrors();
111 try {
112 new ParallelInvalid()
113 .map(Functions.<Object>identity())
114 .filter(Functions.alwaysTrue())
115 .sequential()
116 .test()
117 .assertFailure(TestException.class);
118
119 assertFalse(errors.isEmpty());
120 for (Throwable ex : errors) {
121 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
122 }
123 } finally {
124 RxJavaPlugins.reset();
125 }
126 }
127
128 @Test
129 public void error() {
130 Flowable.error(new TestException())
131 .parallel()
132 .map(Functions.<Object>identity())
133 .sequential()
134 .test()
135 .assertFailure(TestException.class);
136 }
137
138 @Test
139 public void mapCrash() {
140 Flowable.just(1)
141 .parallel()
142 .map(new Function<Integer, Object>() {
143 @Override
144 public Object apply(Integer v) throws Exception {
145 throw new TestException();
146 }
147 })
148 .sequential()
149 .test()
150 .assertFailure(TestException.class);
151 }
152
153 @Test
154 public void mapCrashConditional() {
155 Flowable.just(1)
156 .parallel()
157 .map(new Function<Integer, Object>() {
158 @Override
159 public Object apply(Integer v) throws Exception {
160 throw new TestException();
161 }
162 })
163 .filter(Functions.alwaysTrue())
164 .sequential()
165 .test()
166 .assertFailure(TestException.class);
167 }
168
169 @Test
170 public void mapCrashConditional2() {
171 Flowable.just(1)
172 .parallel()
173 .runOn(Schedulers.computation())
174 .map(new Function<Integer, Object>() {
175 @Override
176 public Object apply(Integer v) throws Exception {
177 throw new TestException();
178 }
179 })
180 .filter(Functions.alwaysTrue())
181 .sequential()
182 .test()
183 .awaitDone(5, TimeUnit.SECONDS)
184 .assertFailure(TestException.class);
185 }
186
187 @Test
188 public void invalidSubscriberCount() {
189 TestHelper.checkInvalidParallelSubscribers(
190 Flowable.range(1, 10).parallel()
191 .map(v -> v)
192 );
193 }
194
195 @Test
196 public void doubleOnSubscribe() {
197 TestHelper.checkDoubleOnSubscribeParallel(
198 p -> p.map(v -> v)
199 );
200
201 TestHelper.checkDoubleOnSubscribeParallel(
202 p -> p.map(v -> v)
203 .filter(v -> true)
204 );
205 }
206
207 @Test
208 public void conditionalCancelIgnored() {
209 Flowable<Integer> f = new Flowable<Integer>() {
210 @Override
211 protected void subscribeActual(@NonNull Subscriber<@NonNull ? super @NonNull Integer> s) {
212 @SuppressWarnings("unchecked")
213 ConditionalSubscriber<Integer> subscriber = (ConditionalSubscriber<Integer>)s;
214 subscriber.onSubscribe(new BooleanSubscription());
215 subscriber.tryOnNext(1);
216 subscriber.tryOnNext(2);
217 }
218 };
219
220 ParallelFlowable.fromArray(f)
221 .map(v -> { throw new TestException(); })
222 .filter(v -> true)
223 .sequential()
224 .test()
225 .assertFailure(TestException.class);
226 }
227 }