Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableScalarXMapTest.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 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.TestException;
23 import io.reactivex.rxjava3.functions.*;
24 import io.reactivex.rxjava3.internal.subscriptions.*;
25 import io.reactivex.rxjava3.subscribers.TestSubscriber;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class FlowableScalarXMapTest extends RxJavaTest {
29
30 @Test
31 public void utilityClass() {
32 TestHelper.checkUtilityClass(FlowableScalarXMap.class);
33 }
34
35 static final class CallablePublisher implements Publisher<Integer>, Supplier<Integer> {
36 @Override
37 public void subscribe(Subscriber<? super Integer> s) {
38 EmptySubscription.error(new TestException(), s);
39 }
40
41 @Override
42 public Integer get() throws Exception {
43 throw new TestException();
44 }
45 }
46
47 static final class EmptyCallablePublisher implements Publisher<Integer>, Supplier<Integer> {
48 @Override
49 public void subscribe(Subscriber<? super Integer> s) {
50 EmptySubscription.complete(s);
51 }
52
53 @Override
54 public Integer get() throws Exception {
55 return null;
56 }
57 }
58
59 static final class OneCallablePublisher implements Publisher<Integer>, Supplier<Integer> {
60 @Override
61 public void subscribe(Subscriber<? super Integer> s) {
62 s.onSubscribe(new ScalarSubscription<>(s, 1));
63 }
64
65 @Override
66 public Integer get() throws Exception {
67 return 1;
68 }
69 }
70
71 @Test
72 public void tryScalarXMap() {
73 TestSubscriber<Integer> ts = new TestSubscriber<>();
74 assertTrue(FlowableScalarXMap.tryScalarXMapSubscribe(new CallablePublisher(), ts, new Function<Integer, Publisher<Integer>>() {
75 @Override
76 public Publisher<Integer> apply(Integer f) throws Exception {
77 return Flowable.just(1);
78 }
79 }));
80
81 ts.assertFailure(TestException.class);
82 }
83
84 @Test
85 public void emptyXMap() {
86 TestSubscriber<Integer> ts = new TestSubscriber<>();
87
88 assertTrue(FlowableScalarXMap.tryScalarXMapSubscribe(new EmptyCallablePublisher(), ts, new Function<Integer, Publisher<Integer>>() {
89 @Override
90 public Publisher<Integer> apply(Integer f) throws Exception {
91 return Flowable.just(1);
92 }
93 }));
94
95 ts.assertResult();
96 }
97
98 @Test
99 public void mapperCrashes() {
100 TestSubscriber<Integer> ts = new TestSubscriber<>();
101
102 assertTrue(FlowableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), ts, new Function<Integer, Publisher<Integer>>() {
103 @Override
104 public Publisher<Integer> apply(Integer f) throws Exception {
105 throw new TestException();
106 }
107 }));
108
109 ts.assertFailure(TestException.class);
110 }
111
112 @Test
113 public void mapperToJust() {
114 TestSubscriber<Integer> ts = new TestSubscriber<>();
115
116 assertTrue(FlowableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), ts, new Function<Integer, Publisher<Integer>>() {
117 @Override
118 public Publisher<Integer> apply(Integer f) throws Exception {
119 return Flowable.just(1);
120 }
121 }));
122
123 ts.assertResult(1);
124 }
125
126 @Test
127 public void mapperToEmpty() {
128 TestSubscriber<Integer> ts = new TestSubscriber<>();
129
130 assertTrue(FlowableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), ts, new Function<Integer, Publisher<Integer>>() {
131 @Override
132 public Publisher<Integer> apply(Integer f) throws Exception {
133 return Flowable.empty();
134 }
135 }));
136
137 ts.assertResult();
138 }
139
140 @Test
141 public void mapperToCrashingCallable() {
142 TestSubscriber<Integer> ts = new TestSubscriber<>();
143
144 assertTrue(FlowableScalarXMap.tryScalarXMapSubscribe(new OneCallablePublisher(), ts, new Function<Integer, Publisher<Integer>>() {
145 @Override
146 public Publisher<Integer> apply(Integer f) throws Exception {
147 return new CallablePublisher();
148 }
149 }));
150
151 ts.assertFailure(TestException.class);
152 }
153
154 @Test
155 public void scalarMapToEmpty() {
156 FlowableScalarXMap.scalarXMap(1, new Function<Integer, Publisher<Integer>>() {
157 @Override
158 public Publisher<Integer> apply(Integer v) throws Exception {
159 return Flowable.empty();
160 }
161 })
162 .test()
163 .assertResult();
164 }
165
166 @Test
167 public void scalarMapToCrashingCallable() {
168 FlowableScalarXMap.scalarXMap(1, new Function<Integer, Publisher<Integer>>() {
169 @Override
170 public Publisher<Integer> apply(Integer v) throws Exception {
171 return new CallablePublisher();
172 }
173 })
174 .test()
175 .assertFailure(TestException.class);
176 }
177
178 @Test
179 public void scalarDisposableStateCheck() {
180 TestSubscriber<Integer> ts = new TestSubscriber<>();
181 ScalarSubscription<Integer> sd = new ScalarSubscription<>(ts, 1);
182 ts.onSubscribe(sd);
183
184 assertFalse(sd.isCancelled());
185
186 assertTrue(sd.isEmpty());
187
188 sd.request(1);
189
190 assertFalse(sd.isCancelled());
191
192 assertTrue(sd.isEmpty());
193
194 ts.assertResult(1);
195
196 try {
197 sd.offer(1);
198 fail("Should have thrown");
199 } catch (UnsupportedOperationException ex) {
200 // expected
201 }
202
203 try {
204 sd.offer(1, 2);
205 fail("Should have thrown");
206 } catch (UnsupportedOperationException ex) {
207 // expected
208 }
209 }
210
211 @Test
212 public void scalarDisposableRunDisposeRace() {
213 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
214 TestSubscriber<Integer> ts = new TestSubscriber<>();
215 final ScalarSubscription<Integer> sd = new ScalarSubscription<>(ts, 1);
216 ts.onSubscribe(sd);
217
218 Runnable r1 = new Runnable() {
219 @Override
220 public void run() {
221 sd.request(1);
222 }
223 };
224
225 Runnable r2 = new Runnable() {
226 @Override
227 public void run() {
228 sd.cancel();
229 }
230 };
231
232 TestHelper.race(r1, r2);
233 }
234 }
235
236 @Test
237 public void cancelled() {
238 ScalarSubscription<Integer> scalar = new ScalarSubscription<>(new TestSubscriber<>(), 1);
239
240 assertFalse(scalar.isCancelled());
241
242 scalar.cancel();
243
244 assertTrue(scalar.isCancelled());
245 }
246
247 @Test
248 public void mapToNonScalar() {
249 Flowable.fromCallable(() -> 1)
250 .concatMap(v -> Flowable.range(1, 5))
251 .test()
252 .assertResult(1, 2, 3, 4, 5);
253 }
254 }