| 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.flowable; |
| 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.exceptions.TestException; |
| 27 |
import io.reactivex.rxjava3.functions.Supplier; |
| 28 |
import io.reactivex.rxjava3.operators.QueueFuseable; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 31 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 32 |
import io.reactivex.rxjava3.testsupport.*; |
| 33 |
|
| 34 |
public class FlowableFromRunnableTest extends RxJavaTest { |
| 35 |
@Test |
| 36 |
public void fromRunnable() { |
| 37 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 38 |
|
| 39 |
Flowable.fromRunnable(new Runnable() { |
| 40 |
@Override |
| 41 |
public void run() { |
| 42 |
atomicInteger.incrementAndGet(); |
| 43 |
} |
| 44 |
}) |
| 45 |
.test() |
| 46 |
.assertResult(); |
| 47 |
|
| 48 |
assertEquals(1, atomicInteger.get()); |
| 49 |
} |
| 50 |
|
| 51 |
@Test |
| 52 |
public void fromRunnableTwice() { |
| 53 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 54 |
|
| 55 |
Runnable run = new Runnable() { |
| 56 |
@Override |
| 57 |
public void run() { |
| 58 |
atomicInteger.incrementAndGet(); |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
Flowable.fromRunnable(run) |
| 63 |
.test() |
| 64 |
.assertResult(); |
| 65 |
|
| 66 |
assertEquals(1, atomicInteger.get()); |
| 67 |
|
| 68 |
Flowable.fromRunnable(run) |
| 69 |
.test() |
| 70 |
.assertResult(); |
| 71 |
|
| 72 |
assertEquals(2, atomicInteger.get()); |
| 73 |
} |
| 74 |
|
| 75 |
@Test |
| 76 |
public void fromRunnableInvokesLazy() { |
| 77 |
final AtomicInteger atomicInteger = new AtomicInteger(); |
| 78 |
|
| 79 |
Flowable<Object> source = Flowable.fromRunnable(new Runnable() { |
| 80 |
@Override |
| 81 |
public void run() { |
| 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 fromRunnableThrows() { |
| 97 |
Flowable.fromRunnable(new Runnable() { |
| 98 |
@Override |
| 99 |
public void run() { |
| 100 |
throw new UnsupportedOperationException(); |
| 101 |
} |
| 102 |
}) |
| 103 |
.test() |
| 104 |
.assertFailure(UnsupportedOperationException.class); |
| 105 |
} |
| 106 |
|
| 107 |
@SuppressWarnings("unchecked") |
| 108 |
@Test |
| 109 |
public void callable() throws Throwable { |
| 110 |
final int[] counter = { 0 }; |
| 111 |
|
| 112 |
Flowable<Void> m = Flowable.fromRunnable(new Runnable() { |
| 113 |
@Override |
| 114 |
public void run() { |
| 115 |
counter[0]++; |
| 116 |
} |
| 117 |
}); |
| 118 |
|
| 119 |
assertTrue(m.getClass().toString(), m instanceof Supplier); |
| 120 |
|
| 121 |
assertNull(((Supplier<Void>)m).get()); |
| 122 |
|
| 123 |
assertEquals(1, counter[0]); |
| 124 |
} |
| 125 |
|
| 126 |
@Test |
| 127 |
public void noErrorLoss() throws Exception { |
| 128 |
List<Throwable> errors = TestHelper.trackPluginErrors(); |
| 129 |
try { |
| 130 |
final CountDownLatch cdl1 = new CountDownLatch(1); |
| 131 |
final CountDownLatch cdl2 = new CountDownLatch(1); |
| 132 |
|
| 133 |
TestSubscriber<Object> ts = Flowable.fromRunnable(new Runnable() { |
| 134 |
@Override |
| 135 |
public void run() { |
| 136 |
cdl1.countDown(); |
| 137 |
try { |
| 138 |
cdl2.await(5, TimeUnit.SECONDS); |
| 139 |
} catch (InterruptedException e) { |
| 140 |
e.printStackTrace(); |
| 141 |
throw new TestException(e); |
| 142 |
} |
| 143 |
} |
| 144 |
}).subscribeOn(Schedulers.single()).test(); |
| 145 |
|
| 146 |
assertTrue(cdl1.await(5, TimeUnit.SECONDS)); |
| 147 |
|
| 148 |
ts.cancel(); |
| 149 |
|
| 150 |
int timeout = 10; |
| 151 |
|
| 152 |
while (timeout-- > 0 && errors.isEmpty()) { |
| 153 |
Thread.sleep(100); |
| 154 |
} |
| 155 |
|
| 156 |
TestHelper.assertUndeliverable(errors, 0, TestException.class); |
| 157 |
} finally { |
| 158 |
RxJavaPlugins.reset(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void disposedUpfront() throws Throwable { |
| 164 |
Runnable run = mock(Runnable.class); |
| 165 |
|
| 166 |
Flowable.fromRunnable(run) |
| 167 |
.test(1L, true) |
| 168 |
.assertEmpty(); |
| 169 |
|
| 170 |
verify(run, never()).run(); |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void cancelWhileRunning() { |
| 175 |
final TestSubscriber<Object> ts = new TestSubscriber<>(); |
| 176 |
|
| 177 |
Flowable.fromRunnable(new Runnable() { |
| 178 |
@Override |
| 179 |
public void run() { |
| 180 |
ts.cancel(); |
| 181 |
} |
| 182 |
}) |
| 183 |
.subscribeWith(ts) |
| 184 |
.assertEmpty(); |
| 185 |
|
| 186 |
assertTrue(ts.isCancelled()); |
| 187 |
} |
| 188 |
|
| 189 |
@Test |
| 190 |
public void asyncFused() throws Throwable { |
| 191 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 192 |
ts.setInitialFusionMode(QueueFuseable.ASYNC); |
| 193 |
|
| 194 |
Runnable action = mock(Runnable.class); |
| 195 |
|
| 196 |
Flowable.fromRunnable(action) |
| 197 |
.subscribe(ts); |
| 198 |
|
| 199 |
ts.assertFusionMode(QueueFuseable.ASYNC) |
| 200 |
.assertResult(); |
| 201 |
|
| 202 |
verify(action).run(); |
| 203 |
} |
| 204 |
|
| 205 |
@Test |
| 206 |
public void syncFusedRejected() throws Throwable { |
| 207 |
TestSubscriberEx<Object> ts = new TestSubscriberEx<>(); |
| 208 |
ts.setInitialFusionMode(QueueFuseable.SYNC); |
| 209 |
|
| 210 |
Runnable action = mock(Runnable.class); |
| 211 |
|
| 212 |
Flowable.fromRunnable(action) |
| 213 |
.subscribe(ts); |
| 214 |
|
| 215 |
ts.assertFusionMode(QueueFuseable.NONE) |
| 216 |
.assertResult(); |
| 217 |
|
| 218 |
verify(action).run(); |
| 219 |
} |
| 220 |
} |