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