Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableMergeWithCompletableTest.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 org.junit.Test;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.TestException;
22 import io.reactivex.rxjava3.functions.Action;
23 import io.reactivex.rxjava3.processors.PublishProcessor;
24 import io.reactivex.rxjava3.subjects.CompletableSubject;
25 import io.reactivex.rxjava3.subscribers.TestSubscriber;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class FlowableMergeWithCompletableTest extends RxJavaTest {
29
30 @Test
31 public void normal() {
32 final TestSubscriber<Integer> ts = new TestSubscriber<>();
33
34 Flowable.range(1, 5).mergeWith(
35 Completable.fromAction(new Action() {
36 @Override
37 public void run() throws Exception {
38 ts.onNext(100);
39 }
40 })
41 )
42 .subscribe(ts);
43
44 ts.assertResult(1, 2, 3, 4, 5, 100);
45 }
46
47 @Test
48 public void take() {
49 Flowable.range(1, 5)
50 .mergeWith(Completable.complete())
51 .take(3)
52 .test()
53 .assertResult(1, 2, 3);
54 }
55
56 @Test
57 public void cancel() {
58 final PublishProcessor<Integer> pp = PublishProcessor.create();
59 final CompletableSubject cs = CompletableSubject.create();
60
61 TestSubscriber<Integer> ts = pp.mergeWith(cs).test();
62
63 assertTrue(pp.hasSubscribers());
64 assertTrue(cs.hasObservers());
65
66 ts.cancel();
67
68 assertFalse(pp.hasSubscribers());
69 assertFalse(cs.hasObservers());
70 }
71
72 @Test
73 public void normalBackpressured() {
74 final TestSubscriber<Integer> ts = new TestSubscriber<>(0L);
75
76 Flowable.range(1, 5).mergeWith(
77 Completable.fromAction(new Action() {
78 @Override
79 public void run() throws Exception {
80 ts.onNext(100);
81 }
82 })
83 )
84 .subscribe(ts);
85
86 ts
87 .assertValue(100)
88 .requestMore(2)
89 .assertValues(100, 1, 2)
90 .requestMore(2)
91 .assertValues(100, 1, 2, 3, 4)
92 .requestMore(1)
93 .assertResult(100, 1, 2, 3, 4, 5);
94 }
95
96 @Test
97 public void mainError() {
98 Flowable.error(new TestException())
99 .mergeWith(Completable.complete())
100 .test()
101 .assertFailure(TestException.class);
102 }
103
104 @Test
105 public void otherError() {
106 Flowable.never()
107 .mergeWith(Completable.error(new TestException()))
108 .test()
109 .assertFailure(TestException.class);
110 }
111
112 @Test
113 public void completeRace() {
114 for (int i = 0; i < 1000; i++) {
115 final PublishProcessor<Integer> pp = PublishProcessor.create();
116 final CompletableSubject cs = CompletableSubject.create();
117
118 TestSubscriber<Integer> ts = pp.mergeWith(cs).test();
119
120 Runnable r1 = new Runnable() {
121 @Override
122 public void run() {
123 pp.onNext(1);
124 pp.onComplete();
125 }
126 };
127
128 Runnable r2 = new Runnable() {
129 @Override
130 public void run() {
131 cs.onComplete();
132 }
133 };
134
135 TestHelper.race(r1, r2);
136
137 ts.assertResult(1);
138 }
139 }
140
141 @Test
142 public void cancelOtherOnMainError() {
143 PublishProcessor<Integer> pp = PublishProcessor.create();
144 CompletableSubject cs = CompletableSubject.create();
145
146 TestSubscriber<Integer> ts = pp.mergeWith(cs).test();
147
148 assertTrue(pp.hasSubscribers());
149 assertTrue(cs.hasObservers());
150
151 pp.onError(new TestException());
152
153 ts.assertFailure(TestException.class);
154
155 assertFalse("main has observers!", pp.hasSubscribers());
156 assertFalse("other has observers", cs.hasObservers());
157 }
158
159 @Test
160 public void cancelMainOnOtherError() {
161 PublishProcessor<Integer> pp = PublishProcessor.create();
162 CompletableSubject cs = CompletableSubject.create();
163
164 TestSubscriber<Integer> ts = pp.mergeWith(cs).test();
165
166 assertTrue(pp.hasSubscribers());
167 assertTrue(cs.hasObservers());
168
169 cs.onError(new TestException());
170
171 ts.assertFailure(TestException.class);
172
173 assertFalse("main has observers!", pp.hasSubscribers());
174 assertFalse("other has observers", cs.hasObservers());
175 }
176
177 @Test
178 public void undeliverableUponCancel() {
179 TestHelper.checkUndeliverableUponCancel(new FlowableConverter<Integer, Flowable<Integer>>() {
180 @Override
181 public Flowable<Integer> apply(Flowable<Integer> upstream) {
182 return upstream.mergeWith(Completable.complete().hide());
183 }
184 });
185 }
186 }