| 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.completable; |
| 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.concurrent.CountDownLatch; |
| 21 |
import java.util.concurrent.atomic.AtomicInteger; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
import org.mockito.invocation.InvocationOnMock; |
| 25 |
import org.mockito.stubbing.Answer; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.*; |
| 28 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 29 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 30 |
import io.reactivex.rxjava3.functions.Supplier; |
| 31 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 32 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 33 |
import io.reactivex.rxjava3.testsupport.*; |
| 34 |
|
| 35 |
public class CompletableFromSupplierTest extends RxJavaTest { |
| 36 |
|
| 37 |
@Test |
| 38 |
public void fromSupplier() { |
| 39 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 40 |
|
| 41 |
Completable.fromSupplier(new Supplier<Object>() { |
| 42 |
@Override |
| 43 |
public Object get() 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 fromSupplierTwice() { |
| 56 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 57 |
|
| 58 |
Supplier<Object> supplier = new Supplier<Object>() { |
| 59 |
@Override |
| 60 |
public Object get() throws Exception { |
| 61 |
atomicInteger.incrementAndGet(); |
| 62 |
return null; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
Completable.fromSupplier(supplier) |
| 67 |
.test() |
| 68 |
.assertResult(); |
| 69 |
|
| 70 |
assertEquals(1, atomicInteger.get()); |
| 71 |
|
| 72 |
Completable.fromSupplier(supplier) |
| 73 |
.test() |
| 74 |
.assertResult(); |
| 75 |
|
| 76 |
assertEquals(2, atomicInteger.get()); |
| 77 |
} |
| 78 |
|
| 79 |
@Test |
| 80 |
public void fromSupplierInvokesLazy() { |
| 81 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 82 |
|
| 83 |
Completable completable = Completable.fromSupplier(new Supplier<Object>() { |
| 84 |
@Override |
| 85 |
public Object get() 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 fromSupplierThrows() { |
| 102 |
Completable.fromSupplier(new Supplier<Object>() { |
| 103 |
@Override |
| 104 |
public Object get() throws Exception { |
| 105 |
throw new UnsupportedOperationException(); |
| 106 |
} |
| 107 |
}) |
| 108 |
.test() |
| 109 |
.assertFailure(UnsupportedOperationException.class); |
| 110 |
} |
| 111 |
|
| 112 |
@SuppressWarnings("unchecked") |
| 113 |
@Test |
| 114 |
public void shouldNotDeliverResultIfSubscriberUnsubscribedBeforeEmission() throws Throwable { |
| 115 |
Supplier<String> func = mock(Supplier.class); |
| 116 |
|
| 117 |
final CountDownLatch funcLatch = new CountDownLatch(1); |
| 118 |
final CountDownLatch observerLatch = new CountDownLatch(1); |
| 119 |
|
| 120 |
when(func.get()).thenAnswer(new Answer<String>() { |
| 121 |
@Override |
| 122 |
public String answer(InvocationOnMock invocation) throws Throwable { |
| 123 |
observerLatch.countDown(); |
| 124 |
|
| 125 |
try { |
| 126 |
funcLatch.await(); |
| 127 |
} catch (InterruptedException e) { |
| 128 |
// It's okay, unsubscription causes Thread interruption |
| 129 |
|
| 130 |
// Restoring interruption status of the Thread |
| 131 |
Thread.currentThread().interrupt(); |
| 132 |
} |
| 133 |
|
| 134 |
return "should_not_be_delivered"; |
| 135 |
} |
| 136 |
}); |
| 137 |
|
| 138 |
Completable fromSupplierObservable = Completable.fromSupplier(func); |
| 139 |
|
| 140 |
Observer<Object> observer = TestHelper.mockObserver(); |
| 141 |
|
| 142 |
TestObserver<String> outer = new TestObserver<>(observer); |
| 143 |
|
| 144 |
fromSupplierObservable |
| 145 |
.subscribeOn(Schedulers.computation()) |
| 146 |
.subscribe(outer); |
| 147 |
|
| 148 |
// Wait until func will be invoked |
| 149 |
observerLatch.await(); |
| 150 |
|
| 151 |
// Unsubscribing before emission |
| 152 |
outer.dispose(); |
| 153 |
|
| 154 |
// Emitting result |
| 155 |
funcLatch.countDown(); |
| 156 |
|
| 157 |
// func must be invoked |
| 158 |
verify(func).get(); |
| 159 |
|
| 160 |
// Observer must not be notified at all |
| 161 |
verify(observer).onSubscribe(any(Disposable.class)); |
| 162 |
verifyNoMoreInteractions(observer); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
@SuppressUndeliverable |
| 167 |
public void fromActionErrorsDisposed() { |
| 168 |
final AtomicInteger calls = new AtomicInteger(); |
| 169 |
Completable.fromSupplier(new Supplier<Object>() { |
| 170 |
@Override |
| 171 |
public Object get() throws Exception { |
| 172 |
calls.incrementAndGet(); |
| 173 |
throw new TestException(); |
| 174 |
} |
| 175 |
}) |
| 176 |
.test(true) |
| 177 |
.assertEmpty(); |
| 178 |
|
| 179 |
assertEquals(1, calls.get()); |
| 180 |
} |
| 181 |
} |