Debian Codegraph @

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