Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableJoinTest.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.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18
19 import java.util.List;
20
21 import org.junit.*;
22 import org.mockito.MockitoAnnotations;
23 import org.reactivestreams.Subscriber;
24
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.subscriptions.BooleanSubscription;
30 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
31 import io.reactivex.rxjava3.processors.PublishProcessor;
32 import io.reactivex.rxjava3.subscribers.TestSubscriber;
33 import io.reactivex.rxjava3.testsupport.*;
34
35 public class FlowableJoinTest extends RxJavaTest {
36 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
37
38 BiFunction<Integer, Integer, Integer> add = new BiFunction<Integer, Integer, Integer>() {
39 @Override
40 public Integer apply(Integer t1, Integer t2) {
41 return t1 + t2;
42 }
43 };
44
45 <T> Function<Integer, Flowable<T>> just(final Flowable<T> flowable) {
46 return new Function<Integer, Flowable<T>>() {
47 @Override
48 public Flowable<T> apply(Integer t1) {
49 return flowable;
50 }
51 };
52 }
53
54 @Before
55 public void before() {
56 MockitoAnnotations.openMocks(this);
57 }
58
59 @Test
60 public void normal1() {
61 PublishProcessor<Integer> source1 = PublishProcessor.create();
62 PublishProcessor<Integer> source2 = PublishProcessor.create();
63
64 Flowable<Integer> m = source1.join(source2,
65 just(Flowable.never()),
66 just(Flowable.never()), add);
67
68 m.subscribe(subscriber);
69
70 source1.onNext(1);
71 source1.onNext(2);
72 source1.onNext(4);
73
74 source2.onNext(16);
75 source2.onNext(32);
76 source2.onNext(64);
77
78 source1.onComplete();
79 source2.onComplete();
80
81 verify(subscriber, times(1)).onNext(17);
82 verify(subscriber, times(1)).onNext(18);
83 verify(subscriber, times(1)).onNext(20);
84 verify(subscriber, times(1)).onNext(33);
85 verify(subscriber, times(1)).onNext(34);
86 verify(subscriber, times(1)).onNext(36);
87 verify(subscriber, times(1)).onNext(65);
88 verify(subscriber, times(1)).onNext(66);
89 verify(subscriber, times(1)).onNext(68);
90
91 verify(subscriber, times(1)).onComplete();
92 verify(subscriber, never()).onError(any(Throwable.class));
93 }
94
95 @Test
96 public void normal1WithDuration() {
97 PublishProcessor<Integer> source1 = PublishProcessor.create();
98 PublishProcessor<Integer> source2 = PublishProcessor.create();
99
100 PublishProcessor<Integer> duration1 = PublishProcessor.create();
101
102 Flowable<Integer> m = source1.join(source2,
103 just(duration1),
104 just(Flowable.never()), add);
105 m.subscribe(subscriber);
106
107 source1.onNext(1);
108 source1.onNext(2);
109 source2.onNext(16);
110
111 duration1.onNext(1);
112
113 source1.onNext(4);
114 source1.onNext(8);
115
116 source1.onComplete();
117 source2.onComplete();
118
119 verify(subscriber, times(1)).onNext(17);
120 verify(subscriber, times(1)).onNext(18);
121 verify(subscriber, times(1)).onNext(20);
122 verify(subscriber, times(1)).onNext(24);
123
124 verify(subscriber, times(1)).onComplete();
125 verify(subscriber, never()).onError(any(Throwable.class));
126
127 }
128
129 @Test
130 public void normal2() {
131 PublishProcessor<Integer> source1 = PublishProcessor.create();
132 PublishProcessor<Integer> source2 = PublishProcessor.create();
133
134 Flowable<Integer> m = source1.join(source2,
135 just(Flowable.never()),
136 just(Flowable.never()), add);
137
138 m.subscribe(subscriber);
139
140 source1.onNext(1);
141 source1.onNext(2);
142 source1.onComplete();
143
144 source2.onNext(16);
145 source2.onNext(32);
146 source2.onNext(64);
147
148 source2.onComplete();
149
150 verify(subscriber, times(1)).onNext(17);
151 verify(subscriber, times(1)).onNext(18);
152 verify(subscriber, times(1)).onNext(33);
153 verify(subscriber, times(1)).onNext(34);
154 verify(subscriber, times(1)).onNext(65);
155 verify(subscriber, times(1)).onNext(66);
156
157 verify(subscriber, times(1)).onComplete();
158 verify(subscriber, never()).onError(any(Throwable.class));
159 }
160
161 @Test
162 public void leftThrows() {
163 PublishProcessor<Integer> source1 = PublishProcessor.create();
164 PublishProcessor<Integer> source2 = PublishProcessor.create();
165
166 Flowable<Integer> m = source1.join(source2,
167 just(Flowable.never()),
168 just(Flowable.never()), add);
169
170 m.subscribe(subscriber);
171
172 source2.onNext(1);
173 source1.onError(new RuntimeException("Forced failure"));
174
175 verify(subscriber, times(1)).onError(any(Throwable.class));
176 verify(subscriber, never()).onComplete();
177 verify(subscriber, never()).onNext(any());
178 }
179
180 @Test
181 public void rightThrows() {
182 PublishProcessor<Integer> source1 = PublishProcessor.create();
183 PublishProcessor<Integer> source2 = PublishProcessor.create();
184
185 Flowable<Integer> m = source1.join(source2,
186 just(Flowable.never()),
187 just(Flowable.never()), add);
188
189 m.subscribe(subscriber);
190
191 source1.onNext(1);
192 source2.onError(new RuntimeException("Forced failure"));
193
194 verify(subscriber, times(1)).onError(any(Throwable.class));
195 verify(subscriber, never()).onComplete();
196 verify(subscriber, never()).onNext(any());
197 }
198
199 @Test
200 public void leftDurationThrows() {
201 PublishProcessor<Integer> source1 = PublishProcessor.create();
202 PublishProcessor<Integer> source2 = PublishProcessor.create();
203
204 Flowable<Integer> duration1 = Flowable.<Integer> error(new RuntimeException("Forced failure"));
205
206 Flowable<Integer> m = source1.join(source2,
207 just(duration1),
208 just(Flowable.never()), add);
209 m.subscribe(subscriber);
210
211 source1.onNext(1);
212
213 verify(subscriber, times(1)).onError(any(Throwable.class));
214 verify(subscriber, never()).onComplete();
215 verify(subscriber, never()).onNext(any());
216 }
217
218 @Test
219 public void rightDurationThrows() {
220 PublishProcessor<Integer> source1 = PublishProcessor.create();
221 PublishProcessor<Integer> source2 = PublishProcessor.create();
222
223 Flowable<Integer> duration1 = Flowable.<Integer> error(new RuntimeException("Forced failure"));
224
225 Flowable<Integer> m = source1.join(source2,
226 just(Flowable.never()),
227 just(duration1), add);
228 m.subscribe(subscriber);
229
230 source2.onNext(1);
231
232 verify(subscriber, times(1)).onError(any(Throwable.class));
233 verify(subscriber, never()).onComplete();
234 verify(subscriber, never()).onNext(any());
235 }
236
237 @Test
238 public void leftDurationSelectorThrows() {
239 PublishProcessor<Integer> source1 = PublishProcessor.create();
240 PublishProcessor<Integer> source2 = PublishProcessor.create();
241
242 Function<Integer, Flowable<Integer>> fail = new Function<Integer, Flowable<Integer>>() {
243 @Override
244 public Flowable<Integer> apply(Integer t1) {
245 throw new RuntimeException("Forced failure");
246 }
247 };
248
249 Flowable<Integer> m = source1.join(source2,
250 fail,
251 just(Flowable.never()), add);
252 m.subscribe(subscriber);
253
254 source1.onNext(1);
255
256 verify(subscriber, times(1)).onError(any(Throwable.class));
257 verify(subscriber, never()).onComplete();
258 verify(subscriber, never()).onNext(any());
259 }
260
261 @Test
262 public void rightDurationSelectorThrows() {
263 PublishProcessor<Integer> source1 = PublishProcessor.create();
264 PublishProcessor<Integer> source2 = PublishProcessor.create();
265
266 Function<Integer, Flowable<Integer>> fail = new Function<Integer, Flowable<Integer>>() {
267 @Override
268 public Flowable<Integer> apply(Integer t1) {
269 throw new RuntimeException("Forced failure");
270 }
271 };
272
273 Flowable<Integer> m = source1.join(source2,
274 just(Flowable.never()),
275 fail, add);
276 m.subscribe(subscriber);
277
278 source2.onNext(1);
279
280 verify(subscriber, times(1)).onError(any(Throwable.class));
281 verify(subscriber, never()).onComplete();
282 verify(subscriber, never()).onNext(any());
283 }
284
285 @Test
286 public void resultSelectorThrows() {
287 PublishProcessor<Integer> source1 = PublishProcessor.create();
288 PublishProcessor<Integer> source2 = PublishProcessor.create();
289
290 BiFunction<Integer, Integer, Integer> fail = new BiFunction<Integer, Integer, Integer>() {
291 @Override
292 public Integer apply(Integer t1, Integer t2) {
293 throw new RuntimeException("Forced failure");
294 }
295 };
296
297 Flowable<Integer> m = source1.join(source2,
298 just(Flowable.never()),
299 just(Flowable.never()), fail);
300 m.subscribe(subscriber);
301
302 source1.onNext(1);
303 source2.onNext(2);
304
305 verify(subscriber, times(1)).onError(any(Throwable.class));
306 verify(subscriber, never()).onComplete();
307 verify(subscriber, never()).onNext(any());
308 }
309
310 @Test
311 public void dispose() {
312 TestHelper.checkDisposed(PublishProcessor.<Integer>create().join(Flowable.just(1),
313 Functions.justFunction(Flowable.never()),
314 Functions.justFunction(Flowable.never()), new BiFunction<Integer, Integer, Integer>() {
315 @Override
316 public Integer apply(Integer a, Integer b) throws Exception {
317 return a + b;
318 }
319 }));
320 }
321
322 @Test
323 public void take() {
324 Flowable.just(1).join(
325 Flowable.just(2),
326 Functions.justFunction(Flowable.never()),
327 Functions.justFunction(Flowable.never()),
328 new BiFunction<Integer, Integer, Integer>() {
329 @Override
330 public Integer apply(Integer a, Integer b) throws Exception {
331 return a + b;
332 }
333 })
334 .take(1)
335 .test()
336 .assertResult(3);
337 }
338
339 @Test
340 public void rightClose() {
341 PublishProcessor<Integer> pp = PublishProcessor.create();
342
343 TestSubscriber<Integer> ts = pp.join(Flowable.just(2),
344 Functions.justFunction(Flowable.never()),
345 Functions.justFunction(Flowable.empty()),
346 new BiFunction<Integer, Integer, Integer>() {
347 @Override
348 public Integer apply(Integer a, Integer b) throws Exception {
349 return a + b;
350 }
351 })
352 .test()
353 .assertEmpty();
354
355 pp.onNext(1);
356
357 ts.assertEmpty();
358 }
359
360 @Test
361 public void resultSelectorThrows2() {
362 PublishProcessor<Integer> pp = PublishProcessor.create();
363
364 TestSubscriber<Integer> ts = pp.join(
365 Flowable.just(2),
366 Functions.justFunction(Flowable.never()),
367 Functions.justFunction(Flowable.never()),
368 new BiFunction<Integer, Integer, Integer>() {
369 @Override
370 public Integer apply(Integer a, Integer b) throws Exception {
371 throw new TestException();
372 }
373 })
374 .test();
375
376 pp.onNext(1);
377 pp.onComplete();
378
379 ts.assertFailure(TestException.class);
380 }
381
382 @Test
383 public void badOuterSource() {
384 List<Throwable> errors = TestHelper.trackPluginErrors();
385 try {
386 new Flowable<Integer>() {
387 @Override
388 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
389 subscriber.onSubscribe(new BooleanSubscription());
390 subscriber.onError(new TestException("First"));
391 subscriber.onError(new TestException("Second"));
392 }
393 }
394 .join(Flowable.just(2),
395 Functions.justFunction(Flowable.never()),
396 Functions.justFunction(Flowable.never()),
397 new BiFunction<Integer, Integer, Integer>() {
398 @Override
399 public Integer apply(Integer a, Integer b) throws Exception {
400 return a + b;
401 }
402 })
403 .to(TestHelper.<Integer>testConsumer())
404 .assertFailureAndMessage(TestException.class, "First");
405
406 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
407 } finally {
408 RxJavaPlugins.reset();
409 }
410 }
411
412 @Test
413 public void badEndSource() {
414 List<Throwable> errors = TestHelper.trackPluginErrors();
415 try {
416 @SuppressWarnings("rawtypes")
417 final Subscriber[] o = { null };
418
419 TestSubscriberEx<Integer> ts = Flowable.just(1)
420 .join(Flowable.just(2),
421 Functions.justFunction(Flowable.never()),
422 Functions.justFunction(new Flowable<Integer>() {
423 @Override
424 protected void subscribeActual(Subscriber<? super Integer> subscriber) {
425 o[0] = subscriber;
426 subscriber.onSubscribe(new BooleanSubscription());
427 subscriber.onError(new TestException("First"));
428 }
429 }),
430 new BiFunction<Integer, Integer, Integer>() {
431 @Override
432 public Integer apply(Integer a, Integer b) throws Exception {
433 return a + b;
434 }
435 })
436 .to(TestHelper.<Integer>testConsumer());
437
438 o[0].onError(new TestException("Second"));
439
440 ts
441 .assertFailureAndMessage(TestException.class, "First");
442
443 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Second");
444 } finally {
445 RxJavaPlugins.reset();
446 }
447 }
448
449 @Test
450 public void backpressureOverflowRight() {
451 PublishProcessor<Integer> pp1 = PublishProcessor.create();
452 PublishProcessor<Integer> pp2 = PublishProcessor.create();
453
454 TestSubscriber<Object> ts = pp1.join(pp2, Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()),
455 new BiFunction<Integer, Integer, Object>() {
456 @Override
457 public Object apply(Integer a, Integer b) throws Exception {
458 return a + b;
459 }
460 })
461 .test(0L);
462
463 pp1.onNext(1);
464 pp2.onNext(2);
465
466 ts.assertFailure(MissingBackpressureException.class);
467 }
468
469 @Test
470 public void backpressureOverflowLeft() {
471 PublishProcessor<Integer> pp1 = PublishProcessor.create();
472 PublishProcessor<Integer> pp2 = PublishProcessor.create();
473
474 TestSubscriber<Object> ts = pp1.join(pp2, Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()),
475 new BiFunction<Integer, Integer, Object>() {
476 @Override
477 public Object apply(Integer a, Integer b) throws Exception {
478 return a + b;
479 }
480 })
481 .test(0L);
482
483 pp2.onNext(2);
484 pp1.onNext(1);
485
486 ts.assertFailure(MissingBackpressureException.class);
487 }
488
489 @Test
490 public void badRequest() {
491 PublishProcessor<Integer> pp1 = PublishProcessor.create();
492 PublishProcessor<Integer> pp2 = PublishProcessor.create();
493
494 TestHelper.assertBadRequestReported(pp1.join(pp2, Functions.justFunction(Flowable.never()), Functions.justFunction(Flowable.never()), (a, b) -> a + b));
495 }
496
497 @Test
498 public void bothTerminateWithWorkRemaining() {
499 PublishProcessor<Integer> pp1 = PublishProcessor.create();
500 PublishProcessor<Integer> pp2 = PublishProcessor.create();
501
502 TestSubscriber<Integer> ts = pp1.join(
503 pp2,
504 v -> Flowable.never(),
505 v -> Flowable.never(),
506 (a, b) -> a + b)
507 .doOnNext(v -> {
508 pp1.onComplete();
509 pp2.onNext(2);
510 pp2.onComplete();
511 })
512 .test();
513
514 pp1.onNext(0);
515 pp2.onNext(1);
516
517 ts.assertComplete();
518 }
519 }