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