Debian Codegraph @

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