Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableConcatMapTest.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.internal.operators.flowable;
15
16 import static org.junit.Assert.assertEquals;
17
18 import java.util.concurrent.*;
19 import java.util.concurrent.atomic.AtomicInteger;
20
21 import org.junit.Test;
22 import org.reactivestreams.Publisher;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.exceptions.*;
26 import io.reactivex.rxjava3.functions.*;
27 import io.reactivex.rxjava3.internal.operators.flowable.FlowableConcatMap.SimpleScalarSubscription;
28 import io.reactivex.rxjava3.processors.*;
29 import io.reactivex.rxjava3.schedulers.Schedulers;
30 import io.reactivex.rxjava3.subscribers.TestSubscriber;
31 import io.reactivex.rxjava3.testsupport.TestHelper;
32
33 public class FlowableConcatMapTest extends RxJavaTest {
34
35 @Test
36 public void simpleSubscriptionRequest() {
37 TestSubscriber<Integer> ts = new TestSubscriber<>(0);
38 SimpleScalarSubscription<Integer> ws = new SimpleScalarSubscription<>(1, ts);
39 ts.onSubscribe(ws);
40
41 ws.request(0);
42
43 ts.assertEmpty();
44
45 ws.request(1);
46
47 ts.assertResult(1);
48
49 ws.request(1);
50
51 ts.assertResult(1);
52 }
53
54 @Test
55 public void boundaryFusion() {
56 Flowable.range(1, 10000)
57 .observeOn(Schedulers.single())
58 .map(new Function<Integer, String>() {
59 @Override
60 public String apply(Integer t) throws Exception {
61 String name = Thread.currentThread().getName();
62 if (name.contains("RxSingleScheduler")) {
63 return "RxSingleScheduler";
64 }
65 return name;
66 }
67 })
68 .concatMap(new Function<String, Publisher<? extends Object>>() {
69 @Override
70 public Publisher<? extends Object> apply(String v)
71 throws Exception {
72 return Flowable.just(v);
73 }
74 })
75 .observeOn(Schedulers.computation())
76 .distinct()
77 .test()
78 .awaitDone(5, TimeUnit.SECONDS)
79 .assertResult("RxSingleScheduler");
80 }
81
82 @Test
83 public void innerScalarRequestRace() {
84 Flowable<Integer> just = Flowable.just(1);
85 int n = 1000;
86 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
87 PublishProcessor<Flowable<Integer>> source = PublishProcessor.create();
88
89 TestSubscriber<Integer> ts = source
90 .concatMap(v -> v, n + 1)
91 .test(1L);
92
93 TestHelper.race(() -> {
94 for (int j = 0; j < n; j++) {
95 source.onNext(just);
96 }
97 }, () -> {
98 for (int j = 0; j < n; j++) {
99 ts.request(1);
100 }
101 });
102
103 ts.assertValueCount(n);
104 }
105 }
106
107 @Test
108 public void innerScalarRequestRaceDelayError() {
109 Flowable<Integer> just = Flowable.just(1);
110 int n = 1000;
111 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
112 PublishProcessor<Flowable<Integer>> source = PublishProcessor.create();
113
114 TestSubscriber<Integer> ts = source
115 .concatMapDelayError(v -> v, true, n + 1)
116 .test(1L);
117
118 TestHelper.race(() -> {
119 for (int j = 0; j < n; j++) {
120 source.onNext(just);
121 }
122 }, () -> {
123 for (int j = 0; j < n; j++) {
124 ts.request(1);
125 }
126 });
127
128 ts.assertValueCount(n);
129 }
130 }
131
132 @Test
133 public void boundaryFusionDelayError() {
134 Flowable.range(1, 10000)
135 .observeOn(Schedulers.single())
136 .map(new Function<Integer, String>() {
137 @Override
138 public String apply(Integer t) throws Exception {
139 String name = Thread.currentThread().getName();
140 if (name.contains("RxSingleScheduler")) {
141 return "RxSingleScheduler";
142 }
143 return name;
144 }
145 })
146 .concatMapDelayError(new Function<String, Publisher<? extends Object>>() {
147 @Override
148 public Publisher<? extends Object> apply(String v)
149 throws Exception {
150 return Flowable.just(v);
151 }
152 })
153 .observeOn(Schedulers.computation())
154 .distinct()
155 .test()
156 .awaitDone(5, TimeUnit.SECONDS)
157 .assertResult("RxSingleScheduler");
158 }
159
160 @Test
161 public void pollThrows() {
162 Flowable.just(1)
163 .map(new Function<Integer, Integer>() {
164 @Override
165 public Integer apply(Integer v) throws Exception {
166 throw new TestException();
167 }
168 })
169 .compose(TestHelper.<Integer>flowableStripBoundary())
170 .concatMap(new Function<Integer, Publisher<Integer>>() {
171 @Override
172 public Publisher<Integer> apply(Integer v)
173 throws Exception {
174 return Flowable.just(v);
175 }
176 })
177 .test()
178 .assertFailure(TestException.class);
179 }
180
181 @Test
182 public void pollThrowsDelayError() {
183 Flowable.just(1)
184 .map(new Function<Integer, Integer>() {
185 @Override
186 public Integer apply(Integer v) throws Exception {
187 throw new TestException();
188 }
189 })
190 .compose(TestHelper.<Integer>flowableStripBoundary())
191 .concatMapDelayError(new Function<Integer, Publisher<Integer>>() {
192 @Override
193 public Publisher<Integer> apply(Integer v)
194 throws Exception {
195 return Flowable.just(v);
196 }
197 })
198 .test()
199 .assertFailure(TestException.class);
200 }
201
202 @Test
203 public void noCancelPrevious() {
204 final AtomicInteger counter = new AtomicInteger();
205
206 Flowable.range(1, 5)
207 .concatMap(new Function<Integer, Flowable<Integer>>() {
208 @Override
209 public Flowable<Integer> apply(Integer v) throws Exception {
210 return Flowable.just(v).doOnCancel(new Action() {
211 @Override
212 public void run() throws Exception {
213 counter.getAndIncrement();
214 }
215 });
216 }
217 })
218 .test()
219 .assertResult(1, 2, 3, 4, 5);
220
221 assertEquals(0, counter.get());
222 }
223
224 @Test
225 public void delayErrorCallableTillTheEnd() {
226 Flowable.just(1, 2, 3, 101, 102, 23, 890, 120, 32)
227 .concatMapDelayError(new Function<Integer, Flowable<Integer>>() {
228 @Override public Flowable<Integer> apply(final Integer integer) throws Exception {
229 return Flowable.fromCallable(new Callable<Integer>() {
230 @Override public Integer call() throws Exception {
231 if (integer >= 100) {
232 throw new NullPointerException("test null exp");
233 }
234 return integer;
235 }
236 });
237 }
238 })
239 .test()
240 .assertFailure(CompositeException.class, 1, 2, 3, 23, 32);
241 }
242
243 @Test
244 public void delayErrorCallableEager() {
245 Flowable.just(1, 2, 3, 101, 102, 23, 890, 120, 32)
246 .concatMapDelayError(new Function<Integer, Flowable<Integer>>() {
247 @Override public Flowable<Integer> apply(final Integer integer) throws Exception {
248 return Flowable.fromCallable(new Callable<Integer>() {
249 @Override public Integer call() throws Exception {
250 if (integer >= 100) {
251 throw new NullPointerException("test null exp");
252 }
253 return integer;
254 }
255 });
256 }
257 }, false, 2)
258 .test()
259 .assertFailure(NullPointerException.class, 1, 2, 3);
260 }
261
262 @Test
263 public void undeliverableUponCancel() {
264 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() {
265 @Override
266 public Flowable<Integer> apply(Flowable<Integer> upstream) {
267 return upstream.concatMap(new Function<Integer, Publisher<Integer>>() {
268 @Override
269 public Publisher<Integer> apply(Integer v) throws Throwable {
270 return Flowable.just(v).hide();
271 }
272 });
273 }
274 });
275 }
276
277 @Test
278 public void undeliverableUponCancelDelayError() {
279 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() {
280 @Override
281 public Flowable<Integer> apply(Flowable<Integer> upstream) {
282 return upstream.concatMapDelayError(new Function<Integer, Publisher<Integer>>() {
283 @Override
284 public Publisher<Integer> apply(Integer v) throws Throwable {
285 return Flowable.just(v).hide();
286 }
287 }, false, 2);
288 }
289 });
290 }
291
292 @Test
293 public void undeliverableUponCancelDelayErrorTillEnd() {
294 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() {
295 @Override
296 public Flowable<Integer> apply(Flowable<Integer> upstream) {
297 return upstream.concatMapDelayError(new Function<Integer, Publisher<Integer>>() {
298 @Override
299 public Publisher<Integer> apply(Integer v) throws Throwable {
300 return Flowable.just(v).hide();
301 }
302 }, true, 2);
303 }
304 });
305 }
306
307 @Test
308 public void asyncFusedSource() {
309 UnicastProcessor<Integer> up = UnicastProcessor.create();
310 up.onNext(1);
311 up.onComplete();
312
313 up.concatMap(v -> Flowable.just(1).hide())
314 .test()
315 .assertResult(1);
316 }
317
318 @Test
319 public void scalarCallableSource() {
320 Flowable.fromCallable(() -> 1)
321 .concatMap(v -> Flowable.just(1))
322 .test()
323 .assertResult(1);
324 }
325 }