Debian Codegraph @

src/test/java/io/reactivex/rxjava3/parallel/ParallelFromPublisherTest.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 import java.util.concurrent.*;
20
21 import org.junit.Test;
22 import org.reactivestreams.*;
23
24 import io.reactivex.rxjava3.annotations.NonNull;
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29 import io.reactivex.rxjava3.internal.subscribers.BasicFuseableSubscriber;
30 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
31 import io.reactivex.rxjava3.operators.QueueFuseable;
32 import io.reactivex.rxjava3.operators.QueueSubscription;
33 import io.reactivex.rxjava3.processors.*;
34 import io.reactivex.rxjava3.schedulers.Schedulers;
35 import io.reactivex.rxjava3.testsupport.*;
36
37 public class ParallelFromPublisherTest extends RxJavaTest {
38
39 @Test
40 public void sourceOverflow() {
41 new Flowable<Integer>() {
42 @Override
43 protected void subscribeActual(Subscriber<? super Integer> s) {
44 s.onSubscribe(new BooleanSubscription());
45 for (int i = 0; i < 10; i++) {
46 s.onNext(i);
47 }
48 }
49 }
50 .parallel(1, 1)
51 .sequential(1)
52 .test(0)
53 .assertFailure(MissingBackpressureException.class);
54 }
55
56 @Test
57 public void fusedFilterBecomesEmpty() {
58 Flowable.just(1)
59 .filter(Functions.alwaysFalse())
60 .parallel()
61 .sequential()
62 .test()
63 .assertResult();
64 }
65
66 static final class StripBoundary<T> extends Flowable<T> implements FlowableTransformer<T, T> {
67
68 final Flowable<T> source;
69
70 StripBoundary(Flowable<T> source) {
71 this.source = source;
72 }
73
74 @Override
75 public Publisher<T> apply(Flowable<T> upstream) {
76 return new StripBoundary<>(upstream);
77 }
78
79 @Override
80 protected void subscribeActual(Subscriber<? super T> s) {
81 source.subscribe(new StripBoundarySubscriber<>(s));
82 }
83
84 static final class StripBoundarySubscriber<T> extends BasicFuseableSubscriber<T, T> {
85
86 StripBoundarySubscriber(Subscriber<? super T> downstream) {
87 super(downstream);
88 }
89
90 @Override
91 public void onNext(T t) {
92 downstream.onNext(t);
93 }
94
95 @Override
96 public int requestFusion(int mode) {
97 QueueSubscription<T> fs = qs;
98 if (fs != null) {
99 int m = fs.requestFusion(mode & ~QueueFuseable.BOUNDARY);
100 this.sourceMode = m;
101 return m;
102 }
103 return QueueFuseable.NONE;
104 }
105
106 @Override
107 public T poll() throws Throwable {
108 return qs.poll();
109 }
110 }
111 }
112
113 @Test
114 public void syncFusedMapCrash() {
115 Flowable.just(1)
116 .map(new Function<Integer, Object>() {
117 @Override
118 public Object apply(Integer v) throws Exception {
119 throw new TestException();
120 }
121 })
122 .compose(new StripBoundary<>(null))
123 .parallel()
124 .sequential()
125 .test()
126 .assertFailure(TestException.class);
127 }
128
129 @Test
130 public void asyncFusedMapCrash() {
131 UnicastProcessor<Integer> up = UnicastProcessor.create();
132
133 up.onNext(1);
134
135 up
136 .map(new Function<Integer, Object>() {
137 @Override
138 public Object apply(Integer v) throws Exception {
139 throw new TestException();
140 }
141 })
142 .compose(new StripBoundary<>(null))
143 .parallel()
144 .sequential()
145 .test()
146 .assertFailure(TestException.class);
147
148 assertFalse(up.hasSubscribers());
149 }
150
151 @Test
152 public void boundaryConfinement() {
153 final Set<String> between = new HashSet<>();
154 final ConcurrentHashMap<String, String> processing = new ConcurrentHashMap<>();
155
156 TestSubscriberEx<Object> ts = Flowable.range(1, 10)
157 .observeOn(Schedulers.single(), false, 1)
158 .doOnNext(new Consumer<Integer>() {
159 @Override
160 public void accept(Integer v) throws Exception {
161 between.add(Thread.currentThread().getName());
162 }
163 })
164 .parallel(2, 1)
165 .runOn(Schedulers.computation(), 1)
166 .map(new Function<Integer, Object>() {
167 @Override
168 public Object apply(Integer v) throws Exception {
169 processing.putIfAbsent(Thread.currentThread().getName(), "");
170 return v;
171 }
172 })
173 .sequential()
174 .to(TestHelper.<Object>testConsumer())
175 .awaitDone(5, TimeUnit.SECONDS)
176 .assertSubscribed()
177 .assertComplete()
178 .assertNoErrors()
179 ;
180
181 TestHelper.assertValueSet(ts, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
182
183 assertEquals(between.toString(), 1, between.size());
184 assertTrue(between.toString(), between.iterator().next().contains("RxSingleScheduler"));
185
186 Map<String, String> map = processing; // AnimalSniffer: CHM.keySet() in Java 8 returns KeySetView
187
188 for (String e : map.keySet()) {
189 assertTrue(map.toString(), e.contains("RxComputationThreadPool"));
190 }
191 }
192
193 @Test
194 public void badRequest() {
195 TestHelper.assertBadRequestReported(PublishProcessor.create().parallel());
196 }
197
198 @Test
199 public void syncFusedEmptyPoll() {
200 Flowable.just(1, 2)
201 .filter(v -> v == 1)
202 .compose(TestHelper.flowableStripBoundary())
203 .parallel(1)
204 .sequential()
205 .test()
206 .assertResult(1);
207 }
208
209 @Test
210 public void asyncFusedEmptyPoll() {
211 UnicastProcessor<Integer> up = UnicastProcessor.create();
212 up.onNext(1);
213 up.onNext(2);
214 up.onComplete();
215
216 up
217 .filter(v -> v == 1)
218 .compose(TestHelper.flowableStripBoundary())
219 .parallel(1)
220 .sequential()
221 .test()
222 .assertResult(1);
223 }
224
225 @Test
226 public void doubleOnSubscribe() {
227 TestHelper.checkDoubleOnSubscribeFlowable(f -> f.parallel().sequential());
228 }
229
230 @SuppressWarnings("unchecked")
231 @Test
232 public void requestUnboundedRace() {
233 FlowableSubscriber<Integer> fs = new FlowableSubscriber<Integer>() {
234
235 @Override
236 public void onNext(@NonNull Integer t) {
237 }
238
239 @Override
240 public void onError(Throwable t) {
241 }
242
243 @Override
244 public void onComplete() {
245 }
246
247 @Override
248 public void onSubscribe(@NonNull Subscription s) {
249 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
250 TestHelper.race(
251 () -> s.request(Long.MAX_VALUE),
252 () -> s.request(Long.MAX_VALUE)
253 );
254 }
255 }
256 };
257
258 PublishProcessor.create()
259 .parallel(1)
260 .subscribe(new FlowableSubscriber[] { fs });
261 }
262
263 @SuppressWarnings("unchecked")
264 @Test
265 public void requestRace() {
266 FlowableSubscriber<Integer> fs = new FlowableSubscriber<Integer>() {
267
268 @Override
269 public void onNext(@NonNull Integer t) {
270 }
271
272 @Override
273 public void onError(Throwable t) {
274 }
275
276 @Override
277 public void onComplete() {
278 }
279
280 @Override
281 public void onSubscribe(@NonNull Subscription s) {
282 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
283 TestHelper.race(
284 () -> s.request(1),
285 () -> s.request(1)
286 );
287 }
288 }
289 };
290
291 PublishProcessor.create()
292 .parallel(1)
293 .subscribe(new FlowableSubscriber[] { fs });
294 }
295 }