| 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.observable; |
| 15 |
|
| 16 |
import static org.junit.Assert.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.List; |
| 20 |
import java.util.concurrent.*; |
| 21 |
import java.util.concurrent.atomic.AtomicInteger; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.functions.*; |
| 27 |
import io.reactivex.rxjava3.internal.fuseable.*; |
| 28 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 29 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 30 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 31 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class ObservableFromCompletableTest extends RxJavaTest { |
| 35 |
@Test |
| 36 |
public void fromCompletable() { |
| 37 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 38 |
|
| 39 |
Observable.fromCompletable(Completable.fromAction(new Action() { |
| 40 |
@Override |
| 41 |
public void run() throws Exception { |
| 42 |
atomicInteger.incrementAndGet(); |
| 43 |
} |
| 44 |
})) |
| 45 |
.test() |
| 46 |
.assertResult(); |
| 47 |
|
| 48 |
assertEquals(1, atomicInteger.get()); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void fromCompletableTwice() { |
| 53 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 54 |
|
| 55 |
Action run = new Action() { |
| 56 |
@Override |
| 57 |
public void run() throws Exception { |
| 58 |
atomicInteger.incrementAndGet(); |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
Observable.fromCompletable(Completable.fromAction(run)) |
| 63 |
.test() |
| 64 |
.assertResult(); |
| 65 |
|
| 66 |
assertEquals(1, atomicInteger.get()); |
| 67 |
|
| 68 |
Observable.fromCompletable(Completable.fromAction(run)) |
| 69 |
.test() |
| 70 |
.assertResult(); |
| 71 |
|
| 72 |
assertEquals(2, atomicInteger.get()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void fromCompletableInvokesLazy() { |
| 77 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 78 |
|
| 79 |
Observable<Object> source = Observable.fromCompletable(Completable.fromAction(new Action() { |
| 80 |
@Override |
| 81 |
public void run() throws Exception { |
| 82 |
atomicInteger.incrementAndGet(); |
| 83 |
} |
| 84 |
})); |
| 85 |
|
| 86 |
assertEquals(0, atomicInteger.get()); |
| 87 |
|
| 88 |
source |
| 89 |
.test() |
| 90 |
.assertResult(); |
| 91 |
|
| 92 |
assertEquals(1, atomicInteger.get()); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void fromCompletableThrows() { |
| 97 |
Observable.fromCompletable(Completable.fromAction(new Action() { |
| 98 |
@Override |
| 99 |
public void run() throws Exception { |
| 100 |
throw new UnsupportedOperationException(); |
| 101 |
} |
| 102 |
})) |
| 103 |
.test() |
| 104 |
.assertFailure(UnsupportedOperationException.class); |
| 105 |
} |
| 106 |
|
| 107 |
@Test |
| 108 |
public void noErrorLoss() throws Exception { |
| 109 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 110 |
try { |
| 111 |
final CountDownLatch cdl1 = new CountDownLatch(1); |
| 112 |
final CountDownLatch cdl2 = new CountDownLatch(1); |
| 113 |
|
| 114 |
TestObserver<Object> to = Observable.fromCompletable(Completable.fromAction(new Action() { |
| 115 |
@Override |
| 116 |
public void run() throws Exception { |
| 117 |
cdl1.countDown(); |
| 118 |
cdl2.await(5, TimeUnit.SECONDS); |
| 119 |
} |
| 120 |
})).subscribeOn(Schedulers.single()).test(); |
| 121 |
|
| 122 |
assertTrue(cdl1.await(5, TimeUnit.SECONDS)); |
| 123 |
|
| 124 |
to.dispose(); |
| 125 |
|
| 126 |
int timeout = 10; |
| 127 |
|
| 128 |
while (timeout-- > 0 && errors.isEmpty()) { |
| 129 |
Thread.sleep(100); |
| 130 |
} |
| 131 |
|
| 132 |
TestHelper.assertUndeliverable(errors, 0, InterruptedException.class); |
| 133 |
} finally { |
| 134 |
RxJavaPlugins.reset(); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void disposedUpfront() throws Throwable { |
| 140 |
Action run = mock(Action.class); |
| 141 |
|
| 142 |
Observable.fromCompletable(Completable.fromAction(run)) |
| 143 |
.test(true) |
| 144 |
.assertEmpty(); |
| 145 |
|
| 146 |
verify(run, never()).run(); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void cancelWhileRunning() { |
| 151 |
final TestObserver<Object> to = new TestObserver<>(); |
| 152 |
|
| 153 |
Observable.fromCompletable(Completable.fromAction(new Action() { |
| 154 |
@Override |
| 155 |
public void run() throws Exception { |
| 156 |
to.dispose(); |
| 157 |
} |
| 158 |
})) |
| 159 |
.subscribeWith(to) |
| 160 |
.assertEmpty(); |
| 161 |
|
| 162 |
assertTrue(to.isDisposed()); |
| 163 |
} |
| 164 |
|
| 165 |
@Test |
| 166 |
public void asyncFused() throws Throwable { |
| 167 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 168 |
to.setInitialFusionMode(QueueFuseable.ASYNC); |
| 169 |
|
| 170 |
Action action = mock(Action.class); |
| 171 |
|
| 172 |
Observable.fromCompletable(Completable.fromAction(action)) |
| 173 |
.subscribe(to); |
| 174 |
|
| 175 |
to.assertFusionMode(QueueFuseable.ASYNC) |
| 176 |
.assertResult(); |
| 177 |
|
| 178 |
verify(action).run(); |
| 179 |
} |
| 180 |
|
| 181 |
@Test |
| 182 |
public void syncFusedRejected() throws Throwable { |
| 183 |
TestObserverEx<Object> to = new TestObserverEx<>(); |
| 184 |
to.setInitialFusionMode(QueueFuseable.SYNC); |
| 185 |
|
| 186 |
Action action = mock(Action.class); |
| 187 |
|
| 188 |
Observable.fromCompletable(Completable.fromAction(action)) |
| 189 |
.subscribe(to); |
| 190 |
|
| 191 |
to.assertFusionMode(QueueFuseable.NONE) |
| 192 |
.assertResult(); |
| 193 |
|
| 194 |
verify(action).run(); |
| 195 |
} |
| 196 |
|
| 197 |
@Test |
| 198 |
public void disposed() { |
| 199 |
TestHelper.checkDisposed(Observable.fromCompletable(Completable.never())); |
| 200 |
} |
| 201 |
|
| 202 |
@Test |
| 203 |
public void upstream() { |
| 204 |
Observable<?> o = Observable.fromCompletable(Completable.never()); |
| 205 |
assertTrue(o instanceof HasUpstreamCompletableSource); |
| 206 |
assertSame(Completable.never(), ((HasUpstreamCompletableSource)o).source()); |
| 207 |
} |
| 208 |
} |