Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFromCallableTest.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.assertEquals;
17 import static org.mockito.ArgumentMatchers.any;
18 import static org.mockito.Mockito.*;
19
20 import java.util.List;
21 import java.util.concurrent.*;
22
23 import org.junit.Test;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.reactivestreams.*;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.exceptions.TestException;
30 import io.reactivex.rxjava3.functions.Function;
31 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
32 import io.reactivex.rxjava3.schedulers.Schedulers;
33 import io.reactivex.rxjava3.subscribers.TestSubscriber;
34 import io.reactivex.rxjava3.testsupport.TestHelper;
35
36 public class FlowableFromCallableTest extends RxJavaTest {
37
38 @SuppressWarnings("unchecked")
39 @Test
40 public void shouldNotInvokeFuncUntilSubscription() throws Exception {
41 Callable<Object> func = mock(Callable.class);
42
43 when(func.call()).thenReturn(new Object());
44
45 Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func);
46
47 verifyNoInteractions(func);
48
49 fromCallableFlowable.subscribe();
50
51 verify(func).call();
52 }
53
54 @SuppressWarnings("unchecked")
55 @Test
56 public void shouldCallOnNextAndOnCompleted() throws Exception {
57 Callable<String> func = mock(Callable.class);
58
59 when(func.call()).thenReturn("test_value");
60
61 Flowable<String> fromCallableFlowable = Flowable.fromCallable(func);
62
63 Subscriber<String> subscriber = TestHelper.mockSubscriber();
64
65 fromCallableFlowable.subscribe(subscriber);
66
67 verify(subscriber).onNext("test_value");
68 verify(subscriber).onComplete();
69 verify(subscriber, never()).onError(any(Throwable.class));
70 }
71
72 @SuppressWarnings("unchecked")
73 @Test
74 public void shouldCallOnError() throws Exception {
75 Callable<Object> func = mock(Callable.class);
76
77 Throwable throwable = new IllegalStateException("Test exception");
78 when(func.call()).thenThrow(throwable);
79
80 Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func);
81
82 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
83
84 fromCallableFlowable.subscribe(subscriber);
85
86 verify(subscriber, never()).onNext(any());
87 verify(subscriber, never()).onComplete();
88 verify(subscriber).onError(throwable);
89 }
90
91 @SuppressWarnings("unchecked")
92 @Test
93 public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Exception {
94 Callable<String> func = mock(Callable.class);
95
96 final CountDownLatch funcLatch = new CountDownLatch(1);
97 final CountDownLatch observerLatch = new CountDownLatch(1);
98
99 when(func.call()).thenAnswer(new Answer<String>() {
100 @Override
101 public String answer(InvocationOnMock invocation) throws Throwable {
102 observerLatch.countDown();
103
104 try {
105 funcLatch.await();
106 } catch (InterruptedException e) {
107 // It's okay, unsubscription causes Thread interruption
108
109 // Restoring interruption status of the Thread
110 Thread.currentThread().interrupt();
111 }
112
113 return "should_not_be_delivered";
114 }
115 });
116
117 Flowable<String> fromCallableFlowable = Flowable.fromCallable(func);
118
119 Subscriber<String> subscriber = TestHelper.mockSubscriber();
120
121 TestSubscriber<String> outer = new TestSubscriber<>(subscriber);
122
123 fromCallableFlowable
124 .subscribeOn(Schedulers.computation())
125 .subscribe(outer);
126
127 // Wait until func will be invoked
128 observerLatch.await();
129
130 // Unsubscribing before emission
131 outer.cancel();
132
133 // Emitting result
134 funcLatch.countDown();
135
136 // func must be invoked
137 verify(func).call();
138
139 // Observer must not be notified at all
140 verify(subscriber).onSubscribe(any(Subscription.class));
141 verifyNoMoreInteractions(subscriber);
142 }
143
144 @Test
145 public void shouldAllowToThrowCheckedException() {
146 final Exception checkedException = new Exception("test exception");
147
148 Flowable<Object> fromCallableFlowable = Flowable.fromCallable(new Callable<Object>() {
149 @Override
150 public Object call() throws Exception {
151 throw checkedException;
152 }
153 });
154
155 Subscriber<Object> subscriber = TestHelper.mockSubscriber();
156
157 fromCallableFlowable.subscribe(subscriber);
158
159 verify(subscriber).onSubscribe(any(Subscription.class));
160 verify(subscriber).onError(checkedException);
161 verifyNoMoreInteractions(subscriber);
162 }
163
164 @Test
165 public void fusedFlatMapExecution() {
166 final int[] calls = { 0 };
167
168 Flowable.just(1).flatMap(new Function<Integer, Publisher<? extends Object>>() {
169 @Override
170 public Publisher<? extends Object> apply(Integer v)
171 throws Exception {
172 return Flowable.fromCallable(new Callable<Object>() {
173 @Override
174 public Object call() throws Exception {
175 return ++calls[0];
176 }
177 });
178 }
179 })
180 .test()
181 .assertResult(1);
182
183 assertEquals(1, calls[0]);
184 }
185
186 @Test
187 public void fusedFlatMapExecutionHidden() {
188 final int[] calls = { 0 };
189
190 Flowable.just(1).hide().flatMap(new Function<Integer, Publisher<? extends Object>>() {
191 @Override
192 public Publisher<? extends Object> apply(Integer v)
193 throws Exception {
194 return Flowable.fromCallable(new Callable<Object>() {
195 @Override
196 public Object call() throws Exception {
197 return ++calls[0];
198 }
199 });
200 }
201 })
202 .test()
203 .assertResult(1);
204
205 assertEquals(1, calls[0]);
206 }
207
208 @Test
209 public void fusedFlatMapNull() {
210 Flowable.just(1).flatMap(new Function<Integer, Publisher<? extends Object>>() {
211 @Override
212 public Publisher<? extends Object> apply(Integer v)
213 throws Exception {
214 return Flowable.fromCallable(new Callable<Object>() {
215 @Override
216 public Object call() throws Exception {
217 return null;
218 }
219 });
220 }
221 })
222 .test()
223 .assertFailure(NullPointerException.class);
224 }
225
226 @Test
227 public void fusedFlatMapNullHidden() {
228 Flowable.just(1).hide().flatMap(new Function<Integer, Publisher<? extends Object>>() {
229 @Override
230 public Publisher<? extends Object> apply(Integer v)
231 throws Exception {
232 return Flowable.fromCallable(new Callable<Object>() {
233 @Override
234 public Object call() throws Exception {
235 return null;
236 }
237 });
238 }
239 })
240 .test()
241 .assertFailure(NullPointerException.class);
242 }
243
244 @Test
245 public void undeliverableUponCancellation() throws Exception {
246 List<Throwable> errors = TestHelper.trackPluginErrors();
247 try {
248 final TestSubscriber<Integer> ts = new TestSubscriber<>();
249
250 Flowable.fromCallable(new Callable<Integer>() {
251 @Override
252 public Integer call() throws Exception {
253 ts.cancel();
254 throw new TestException();
255 }
256 })
257 .subscribe(ts);
258
259 ts.assertEmpty();
260
261 TestHelper.assertUndeliverable(errors, 0, TestException.class);
262 } finally {
263 RxJavaPlugins.reset();
264 }
265 }
266 }