Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/single/SingleFromSupplierTest.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.single;
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 SingleFromSupplierTest extends RxJavaTest {
37
38 @Test
39 public void fromSupplierValue() {
40 Single.fromSupplier(new Supplier<Integer>() {
41 @Override public Integer get() throws Exception {
42 return 5;
43 }
44 })
45 .test()
46 .assertResult(5);
47 }
48
49 @Test
50 public void fromSupplierError() {
51 Single.fromSupplier(new Supplier<Integer>() {
52 @Override public Integer get() throws Exception {
53 throw new UnsupportedOperationException();
54 }
55 })
56 .test()
57 .assertFailure(UnsupportedOperationException.class);
58 }
59
60 @Test
61 public void fromSupplierNull() {
62 Single.fromSupplier(new Supplier<Integer>() {
63 @Override public Integer get() throws Exception {
64 return null;
65 }
66 })
67 .to(TestHelper.<Integer>testConsumer())
68 .assertFailureAndMessage(NullPointerException.class, "The supplier returned a null value");
69 }
70
71 @Test
72 public void fromSupplierTwice() {
73 final AtomicInteger atomicInteger = new AtomicInteger();
74
75 Supplier<Integer> supplier = new Supplier<Integer>() {
76 @Override
77 public Integer get() throws Exception {
78 return atomicInteger.incrementAndGet();
79 }
80 };
81
82 Single.fromSupplier(supplier)
83 .test()
84 .assertResult(1);
85
86 assertEquals(1, atomicInteger.get());
87
88 Single.fromSupplier(supplier)
89 .test()
90 .assertResult(2);
91
92 assertEquals(2, atomicInteger.get());
93 }
94
95 @SuppressWarnings("unchecked")
96 @Test
97 public void shouldNotInvokeFuncUntilSubscription() throws Throwable {
98 Supplier<Object> func = mock(Supplier.class);
99
100 when(func.get()).thenReturn(new Object());
101
102 Single<Object> fromSupplierSingle = Single.fromSupplier(func);
103
104 verifyNoInteractions(func);
105
106 fromSupplierSingle.subscribe();
107
108 verify(func).get();
109 }
110
111 @Test
112 public void noErrorLoss() throws Exception {
113 List<Throwable> errors = TestHelper.trackPluginErrors();
114 try {
115 final CountDownLatch cdl1 = new CountDownLatch(1);
116 final CountDownLatch cdl2 = new CountDownLatch(1);
117
118 TestObserver<Integer> to = Single.fromSupplier(new Supplier<Integer>() {
119 @Override
120 public Integer get() throws Exception {
121 cdl1.countDown();
122 cdl2.await(5, TimeUnit.SECONDS);
123 return 1;
124 }
125 }).subscribeOn(Schedulers.single()).test();
126
127 assertTrue(cdl1.await(5, TimeUnit.SECONDS));
128
129 to.dispose();
130
131 int timeout = 10;
132
133 while (timeout-- > 0 && errors.isEmpty()) {
134 Thread.sleep(100);
135 }
136
137 TestHelper.assertUndeliverable(errors, 0, InterruptedException.class);
138 } finally {
139 RxJavaPlugins.reset();
140 }
141 }
142
143 @SuppressWarnings("unchecked")
144 @Test
145 public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Throwable {
146 Supplier<String> func = mock(Supplier.class);
147
148 final CountDownLatch funcLatch = new CountDownLatch(1);
149 final CountDownLatch observerLatch = new CountDownLatch(1);
150
151 when(func.get()).thenAnswer(new Answer<String>() {
152 @Override
153 public String answer(InvocationOnMock invocation) throws Throwable {
154 observerLatch.countDown();
155
156 try {
157 funcLatch.await();
158 } catch (InterruptedException e) {
159 // It's okay, unsubscription causes Thread interruption
160
161 // Restoring interruption status of the Thread
162 Thread.currentThread().interrupt();
163 }
164
165 return "should_not_be_delivered";
166 }
167 });
168
169 Single<String> fromSupplierObservable = Single.fromSupplier(func);
170
171 Observer<Object> observer = TestHelper.mockObserver();
172
173 TestObserver<String> outer = new TestObserver<>(observer);
174
175 fromSupplierObservable
176 .subscribeOn(Schedulers.computation())
177 .subscribe(outer);
178
179 // Wait until func will be invoked
180 observerLatch.await();
181
182 // Unsubscribing before emission
183 outer.dispose();
184
185 // Emitting result
186 funcLatch.countDown();
187
188 // func must be invoked
189 verify(func).get();
190
191 // Observer must not be notified at all
192 verify(observer).onSubscribe(any(Disposable.class));
193 verifyNoMoreInteractions(observer);
194 }
195
196 @Test
197 public void shouldAllowToThrowCheckedException() {
198 final Exception checkedException = new Exception("test exception");
199
200 Single<Object> fromSupplierObservable = Single.fromSupplier(new Supplier<Object>() {
201 @Override
202 public Object get() throws Exception {
203 throw checkedException;
204 }
205 });
206
207 SingleObserver<Object> observer = TestHelper.mockSingleObserver();
208
209 fromSupplierObservable.subscribe(observer);
210
211 verify(observer).onSubscribe(any(Disposable.class));
212 verify(observer).onError(checkedException);
213 verifyNoMoreInteractions(observer);
214 }
215
216 @Test
217 public void disposedOnArrival() {
218 final int[] count = { 0 };
219 Single.fromSupplier(new Supplier<Object>() {
220 @Override
221 public Object get() throws Exception {
222 count[0]++;
223 return 1;
224 }
225 })
226 .test(true)
227 .assertEmpty();
228
229 assertEquals(0, count[0]);
230 }
231
232 @Test
233 public void disposedOnCall() {
234 final TestObserver<Integer> to = new TestObserver<>();
235
236 Single.fromSupplier(new Supplier<Integer>() {
237 @Override
238 public Integer get() throws Exception {
239 to.dispose();
240 return 1;
241 }
242 })
243 .subscribe(to);
244
245 to.assertEmpty();
246 }
247
248 @Test
249 public void toObservableTake() {
250 Single.fromSupplier(new Supplier<Object>() {
251 @Override
252 public Object get() throws Exception {
253 return 1;
254 }
255 })
256 .toObservable()
257 .take(1)
258 .test()
259 .assertResult(1);
260 }
261
262 @Test
263 public void toObservableAndBack() {
264 Single.fromSupplier(new Supplier<Integer>() {
265 @Override
266 public Integer get() throws Exception {
267 return 1;
268 }
269 })
270 .toObservable()
271 .singleOrError()
272 .test()
273 .assertResult(1);
274 }
275 }