Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeFromCallableTest.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.maybe;
15
16 import static org.junit.Assert.*;
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 import java.util.concurrent.atomic.AtomicInteger;
23
24 import org.junit.Test;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27
28 import io.reactivex.rxjava3.core.*;
29 import io.reactivex.rxjava3.disposables.Disposable;
30 import io.reactivex.rxjava3.functions.Supplier;
31 import io.reactivex.rxjava3.observers.TestObserver;
32 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
33 import io.reactivex.rxjava3.schedulers.Schedulers;
34 import io.reactivex.rxjava3.testsupport.TestHelper;
35
36 public class MaybeFromCallableTest extends RxJavaTest {
37 @Test
38 public void fromCallable() {
39 final AtomicInteger atomicInteger = new AtomicInteger();
40
41 Maybe.fromCallable(new Callable<Object>() {
42 @Override
43 public Object call() throws Exception {
44 atomicInteger.incrementAndGet();
45 return null;
46 }
47 })
48 .test()
49 .assertResult();
50
51 assertEquals(1, atomicInteger.get());
52 }
53
54 @Test
55 public void fromCallableTwice() {
56 final AtomicInteger atomicInteger = new AtomicInteger();
57
58 Callable<Object> callable = new Callable<Object>() {
59 @Override
60 public Object call() throws Exception {
61 atomicInteger.incrementAndGet();
62 return null;
63 }
64 };
65
66 Maybe.fromCallable(callable)
67 .test()
68 .assertResult();
69
70 assertEquals(1, atomicInteger.get());
71
72 Maybe.fromCallable(callable)
73 .test()
74 .assertResult();
75
76 assertEquals(2, atomicInteger.get());
77 }
78
79 @Test
80 public void fromCallableInvokesLazy() {
81 final AtomicInteger atomicInteger = new AtomicInteger();
82
83 Maybe<Object> completable = Maybe.fromCallable(new Callable<Object>() {
84 @Override
85 public Object call() throws Exception {
86 atomicInteger.incrementAndGet();
87 return null;
88 }
89 });
90
91 assertEquals(0, atomicInteger.get());
92
93 completable
94 .test()
95 .assertResult();
96
97 assertEquals(1, atomicInteger.get());
98 }
99
100 @Test
101 public void fromCallableThrows() {
102 Maybe.fromCallable(new Callable<Object>() {
103 @Override
104 public Object call() throws Exception {
105 throw new UnsupportedOperationException();
106 }
107 })
108 .test()
109 .assertFailure(UnsupportedOperationException.class);
110 }
111
112 @SuppressWarnings("unchecked")
113 @Test
114 public void callable() throws Throwable {
115 final int[] counter = { 0 };
116
117 Maybe<Integer> m = Maybe.fromCallable(new Callable<Integer>() {
118 @Override
119 public Integer call() throws Exception {
120 counter[0]++;
121 return 0;
122 }
123 });
124
125 assertTrue(m.getClass().toString(), m instanceof Supplier);
126
127 assertEquals(0, ((Supplier<Void>)m).get());
128
129 assertEquals(1, counter[0]);
130 }
131
132 @Test
133 public void noErrorLoss() throws Exception {
134 List<Throwable> errors = TestHelper.trackPluginErrors();
135 try {
136 final CountDownLatch cdl1 = new CountDownLatch(1);
137 final CountDownLatch cdl2 = new CountDownLatch(1);
138
139 TestObserver<Integer> to = Maybe.fromCallable(new Callable<Integer>() {
140 @Override
141 public Integer call() throws Exception {
142 cdl1.countDown();
143 cdl2.await(5, TimeUnit.SECONDS);
144 return 1;
145 }
146 }).subscribeOn(Schedulers.single()).test();
147
148 assertTrue(cdl1.await(5, TimeUnit.SECONDS));
149
150 to.dispose();
151
152 int timeout = 10;
153
154 while (timeout-- > 0 && errors.isEmpty()) {
155 Thread.sleep(100);
156 }
157
158 TestHelper.assertUndeliverable(errors, 0, InterruptedException.class);
159 } finally {
160 RxJavaPlugins.reset();
161 }
162 }
163
164 @SuppressWarnings("unchecked")
165 @Test
166 public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Exception {
167 Callable<String> func = mock(Callable.class);
168
169 final CountDownLatch funcLatch = new CountDownLatch(1);
170 final CountDownLatch observerLatch = new CountDownLatch(1);
171
172 when(func.call()).thenAnswer(new Answer<String>() {
173 @Override
174 public String answer(InvocationOnMock invocation) throws Throwable {
175 observerLatch.countDown();
176
177 try {
178 funcLatch.await();
179 } catch (InterruptedException e) {
180 // It's okay, unsubscription causes Thread interruption
181
182 // Restoring interruption status of the Thread
183 Thread.currentThread().interrupt();
184 }
185
186 return "should_not_be_delivered";
187 }
188 });
189
190 Maybe<String> fromCallableObservable = Maybe.fromCallable(func);
191
192 Observer<Object> observer = TestHelper.mockObserver();
193
194 TestObserver<String> outer = new TestObserver<>(observer);
195
196 fromCallableObservable
197 .subscribeOn(Schedulers.computation())
198 .subscribe(outer);
199
200 // Wait until func will be invoked
201 observerLatch.await();
202
203 // Unsubscribing before emission
204 outer.dispose();
205
206 // Emitting result
207 funcLatch.countDown();
208
209 // func must be invoked
210 verify(func).call();
211
212 // Observer must not be notified at all
213 verify(observer).onSubscribe(any(Disposable.class));
214 verifyNoMoreInteractions(observer);
215 }
216
217 @Test
218 public void disposeUpfront() {
219 Maybe.fromCallable(() -> 1)
220 .test(true)
221 .assertEmpty();
222 }
223
224 @Test
225 public void success() {
226 Maybe.fromCallable(() -> 1)
227 .test()
228 .assertResult(1);
229 }
230 }