Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableMergeWithMaybeTest.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.*;
17
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.concurrent.atomic.AtomicReference;
21
22 import org.junit.Test;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.TestException;
27 import io.reactivex.rxjava3.functions.Function;
28 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
29 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
30 import io.reactivex.rxjava3.processors.PublishProcessor;
31 import io.reactivex.rxjava3.subjects.*;
32 import io.reactivex.rxjava3.subscribers.TestSubscriber;
33 import io.reactivex.rxjava3.testsupport.TestHelper;
34
35 public class FlowableMergeWithMaybeTest extends RxJavaTest {
36
37 @Test
38 public void normal() {
39 Flowable.range(1, 5)
40 .mergeWith(Maybe.just(100))
41 .test()
42 .assertResult(1, 2, 3, 4, 5, 100);
43 }
44
45 @Test
46 public void emptyOther() {
47 Flowable.range(1, 5)
48 .mergeWith(Maybe.<Integer>empty())
49 .test()
50 .assertResult(1, 2, 3, 4, 5);
51 }
52
53 @Test
54 public void normalLong() {
55 Flowable.range(1, 512)
56 .mergeWith(Maybe.just(100))
57 .test()
58 .assertValueCount(513)
59 .assertComplete();
60 }
61
62 @Test
63 public void normalLongRequestExact() {
64 Flowable.range(1, 512)
65 .mergeWith(Maybe.just(100))
66 .test(513)
67 .assertValueCount(513)
68 .assertComplete();
69 }
70
71 @Test
72 public void take() {
73 Flowable.range(1, 5)
74 .mergeWith(Maybe.just(100))
75 .take(3)
76 .test()
77 .assertResult(1, 2, 3);
78 }
79
80 @Test
81 public void cancel() {
82 final PublishProcessor<Integer> pp = PublishProcessor.create();
83 final MaybeSubject<Integer> cs = MaybeSubject.create();
84
85 TestSubscriber<Integer> ts = pp.mergeWith(cs).test();
86
87 assertTrue(pp.hasSubscribers());
88 assertTrue(cs.hasObservers());
89
90 ts.cancel();
91
92 assertFalse(pp.hasSubscribers());
93 assertFalse(cs.hasObservers());
94 }
95
96 @Test
97 public void normalBackpressured() {
98 Flowable.range(1, 5).mergeWith(
99 Maybe.just(100)
100 )
101 .test(0L)
102 .assertEmpty()
103 .requestMore(2)
104 .assertValues(100, 1)
105 .requestMore(2)
106 .assertValues(100, 1, 2, 3)
107 .requestMore(2)
108 .assertResult(100, 1, 2, 3, 4, 5);
109 }
110
111 @Test
112 public void mainError() {
113 Flowable.error(new TestException())
114 .mergeWith(Maybe.just(100))
115 .test()
116 .assertFailure(TestException.class);
117 }
118
119 @Test
120 public void otherError() {
121 Flowable.never()
122 .mergeWith(Maybe.error(new TestException()))
123 .test()
124 .assertFailure(TestException.class);
125 }
126
127 @Test
128 public void completeRace() {
129 for (int i = 0; i < 10000; i++) {
130 final PublishProcessor<Integer> pp = PublishProcessor.create();
131 final MaybeSubject<Integer> cs = MaybeSubject.create();
132
133 TestSubscriber<Integer> ts = pp.mergeWith(cs).test();
134
135 Runnable r1 = new Runnable() {
136 @Override
137 public void run() {
138 pp.onNext(1);
139 pp.onComplete();
140 }
141 };
142
143 Runnable r2 = new Runnable() {
144 @Override
145 public void run() {
146 cs.onSuccess(1);
147 }
148 };
149
150 TestHelper.race(r1, r2);
151
152 ts.assertResult(1, 1);
153 }
154 }
155
156 @Test
157 public void onNextSlowPath() {
158 final PublishProcessor<Integer> pp = PublishProcessor.create();
159 final MaybeSubject<Integer> cs = MaybeSubject.create();
160
161 TestSubscriber<Integer> ts = pp.mergeWith(cs).subscribeWith(new TestSubscriber<Integer>() {
162 @Override
163 public void onNext(Integer t) {
164 super.onNext(t);
165 if (t == 1) {
166 pp.onNext(2);
167 }
168 }
169 });
170
171 pp.onNext(1);
172 cs.onSuccess(3);
173
174 pp.onNext(4);
175 pp.onComplete();
176
177 ts.assertResult(1, 2, 3, 4);
178 }
179
180 @Test
181 public void onSuccessSlowPath() {
182 final PublishProcessor<Integer> pp = PublishProcessor.create();
183 final MaybeSubject<Integer> cs = MaybeSubject.create();
184
185 TestSubscriber<Integer> ts = pp.mergeWith(cs).subscribeWith(new TestSubscriber<Integer>() {
186 @Override
187 public void onNext(Integer t) {
188 super.onNext(t);
189 if (t == 1) {
190 cs.onSuccess(2);
191 }
192 }
193 });
194
195 pp.onNext(1);
196
197 pp.onNext(3);
198 pp.onComplete();
199
200 ts.assertResult(1, 2, 3);
201 }
202
203 @Test
204 public void onSuccessSlowPathBackpressured() {
205 final PublishProcessor<Integer> pp = PublishProcessor.create();
206 final MaybeSubject<Integer> cs = MaybeSubject.create();
207
208 TestSubscriber<Integer> ts = pp.mergeWith(cs).subscribeWith(new TestSubscriber<Integer>(1) {
209 @Override
210 public void onNext(Integer t) {
211 super.onNext(t);
212 if (t == 1) {
213 cs.onSuccess(2);
214 }
215 }
216 });
217
218 pp.onNext(1);
219
220 pp.onNext(3);
221 pp.onComplete();
222
223 ts.request(2);
224 ts.assertResult(1, 2, 3);
225 }
226
227 @Test
228 public void onSuccessFastPathBackpressuredRace() {
229 for (int i = 0; i < 10000; i++) {
230 final PublishProcessor<Integer> pp = PublishProcessor.create();
231 final MaybeSubject<Integer> cs = MaybeSubject.create();
232
233 final TestSubscriber<Integer> ts = pp.mergeWith(cs).subscribeWith(new TestSubscriber<>(0));
234
235 Runnable r1 = new Runnable() {
236 @Override
237 public void run() {
238 cs.onSuccess(1);
239 }
240 };
241
242 Runnable r2 = new Runnable() {
243 @Override
244 public void run() {
245 ts.request(2);
246 }
247 };
248
249 TestHelper.race(r1, r2);
250
251 pp.onNext(2);
252 pp.onComplete();
253
254 ts.assertResult(1, 2);
255 }
256 }
257
258 @Test
259 public void onErrorMainOverflow() {
260 List<Throwable> errors = TestHelper.trackPluginErrors();
261 try {
262 final AtomicReference<Subscriber<?>> subscriber = new AtomicReference<>();
263 TestSubscriber<Integer> ts = new Flowable<Integer>() {
264 @Override
265 protected void subscribeActual(Subscriber<? super Integer> s) {
266 s.onSubscribe(new BooleanSubscription());
267 subscriber.set(s);
268 }
269 }
270 .mergeWith(Maybe.<Integer>error(new IOException()))
271 .test();
272
273 subscriber.get().onError(new TestException());
274
275 ts.assertFailure(IOException.class)
276 ;
277
278 TestHelper.assertUndeliverable(errors, 0, TestException.class);
279 } finally {
280 RxJavaPlugins.reset();
281 }
282 }
283
284 @Test
285 public void onErrorOtherOverflow() {
286 List<Throwable> errors = TestHelper.trackPluginErrors();
287 try {
288 Flowable.error(new IOException())
289 .mergeWith(Maybe.error(new TestException()))
290 .test()
291 .assertFailure(IOException.class)
292 ;
293
294 TestHelper.assertUndeliverable(errors, 0, TestException.class);
295 } finally {
296 RxJavaPlugins.reset();
297 }
298 }
299
300 @Test
301 public void onNextRequestRace() {
302 for (int i = 0; i < 10000; i++) {
303 final PublishProcessor<Integer> pp = PublishProcessor.create();
304 final MaybeSubject<Integer> cs = MaybeSubject.create();
305
306 final TestSubscriber<Integer> ts = pp.mergeWith(cs).test(0);
307
308 pp.onNext(0);
309
310 Runnable r1 = new Runnable() {
311 @Override
312 public void run() {
313 pp.onNext(1);
314 }
315 };
316
317 Runnable r2 = new Runnable() {
318 @Override
319 public void run() {
320 ts.request(3);
321 }
322 };
323
324 TestHelper.race(r1, r2);
325
326 cs.onSuccess(1);
327 pp.onComplete();
328
329 ts.assertResult(0, 1, 1);
330 }
331 }
332
333 @Test
334 public void doubleOnSubscribeMain() {
335 TestHelper.checkDoubleOnSubscribeFlowable(
336 new Function<Flowable<Object>, Publisher<Object>>() {
337 @Override
338 public Publisher<Object> apply(Flowable<Object> f)
339 throws Exception {
340 return f.mergeWith(Maybe.just(1));
341 }
342 }
343 );
344 }
345
346 @Test
347 public void noRequestOnError() {
348 Flowable.empty()
349 .mergeWith(Maybe.error(new TestException()))
350 .test(0)
351 .assertFailure(TestException.class);
352 }
353
354 @Test
355 public void drainExactRequestCancel() {
356 final PublishProcessor<Integer> pp = PublishProcessor.create();
357 final MaybeSubject<Integer> cs = MaybeSubject.create();
358
359 TestSubscriber<Integer> ts = pp.mergeWith(cs)
360 .take(2)
361 .subscribeWith(new TestSubscriber<Integer>(2) {
362 @Override
363 public void onNext(Integer t) {
364 super.onNext(t);
365 if (t == 1) {
366 cs.onSuccess(2);
367 }
368 }
369 });
370
371 pp.onNext(1);
372
373 pp.onComplete();
374
375 ts.request(2);
376 ts.assertResult(1, 2);
377 }
378
379 @Test
380 public void drainRequestWhenLimitReached() {
381 final PublishProcessor<Integer> pp = PublishProcessor.create();
382 final MaybeSubject<Integer> cs = MaybeSubject.create();
383
384 TestSubscriber<Integer> ts = pp.mergeWith(cs)
385 .subscribeWith(new TestSubscriber<Integer>() {
386 @Override
387 public void onNext(Integer t) {
388 super.onNext(t);
389 if (t == 1) {
390 for (int i = 0; i < Flowable.bufferSize() - 1; i++) {
391 pp.onNext(i + 2);
392 }
393 }
394 }
395 });
396
397 cs.onSuccess(1);
398
399 pp.onComplete();
400
401 ts.request(2);
402 ts.assertValueCount(Flowable.bufferSize());
403 ts.assertComplete();
404 }
405
406 @Test
407 public void cancelOtherOnMainError() {
408 PublishProcessor<Integer> pp = PublishProcessor.create();
409 MaybeSubject<Integer> ms = MaybeSubject.create();
410
411 TestSubscriber<Integer> ts = pp.mergeWith(ms).test();
412
413 assertTrue(pp.hasSubscribers());
414 assertTrue(ms.hasObservers());
415
416 pp.onError(new TestException());
417
418 ts.assertFailure(TestException.class);
419
420 assertFalse("main has observers!", pp.hasSubscribers());
421 assertFalse("other has observers", ms.hasObservers());
422 }
423
424 @Test
425 public void cancelMainOnOtherError() {
426 PublishProcessor<Integer> pp = PublishProcessor.create();
427 MaybeSubject<Integer> ms = MaybeSubject.create();
428
429 TestSubscriber<Integer> ts = pp.mergeWith(ms).test();
430
431 assertTrue(pp.hasSubscribers());
432 assertTrue(ms.hasObservers());
433
434 ms.onError(new TestException());
435
436 ts.assertFailure(TestException.class);
437
438 assertFalse("main has observers!", pp.hasSubscribers());
439 assertFalse("other has observers", ms.hasObservers());
440 }
441
442 @Test
443 public void undeliverableUponCancel() {
444 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() {
445 @Override
446 public Flowable<Integer> apply(Flowable<Integer> upstream) {
447 return upstream.mergeWith(Maybe.just(1).hide());
448 }
449 });
450 }
451
452 @Test
453 public void drainMoreWorkBeforeCancel() {
454 MaybeSubject<Integer> ms = MaybeSubject.create();
455
456 TestSubscriber<Integer> ts = new TestSubscriber<>();
457
458 Flowable.range(1, 5).mergeWith(ms)
459 .doOnNext(v -> {
460 if (v == 1) {
461 ms.onSuccess(6);
462 ts.cancel();
463 }
464 })
465 .subscribe(ts);
466
467 ts.assertValuesOnly(1);
468 }
469 }