| 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.mockito.ArgumentMatchers.*; |
| 17 |
import static org.mockito.Mockito.*; |
| 18 |
|
| 19 |
import java.util.concurrent.*; |
| 20 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.reactivestreams.Subscriber; |
| 24 |
|
| 25 |
import io.reactivex.rxjava3.core.*; |
| 26 |
import io.reactivex.rxjava3.schedulers.*; |
| 27 |
import io.reactivex.rxjava3.subscribers.TestSubscriber; |
| 28 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 29 |
|
| 30 |
public class FlowableToFutureTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test |
| 33 |
public void success() throws Exception { |
| 34 |
@SuppressWarnings("unchecked") |
| 35 |
Future<Object> future = mock(Future.class); |
| 36 |
Object value = new Object(); |
| 37 |
when(future.get()).thenReturn(value); |
| 38 |
|
| 39 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 40 |
|
| 41 |
TestSubscriber<Object> ts = new TestSubscriber<>(subscriber); |
| 42 |
|
| 43 |
Flowable.fromFuture(future).subscribe(ts); |
| 44 |
|
| 45 |
ts.cancel(); |
| 46 |
|
| 47 |
verify(subscriber, times(1)).onNext(value); |
| 48 |
verify(subscriber, times(1)).onComplete(); |
| 49 |
verify(subscriber, never()).onError(any(Throwable.class)); |
| 50 |
verify(future, never()).cancel(anyBoolean()); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void successOperatesOnSuppliedScheduler() throws Exception { |
| 55 |
@SuppressWarnings("unchecked") |
| 56 |
Future<Object> future = mock(Future.class); |
| 57 |
Object value = new Object(); |
| 58 |
when(future.get()).thenReturn(value); |
| 59 |
|
| 60 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 61 |
|
| 62 |
TestScheduler scheduler = new TestScheduler(); |
| 63 |
TestSubscriber<Object> ts = new TestSubscriber<>(subscriber); |
| 64 |
|
| 65 |
Flowable.fromFuture(future).subscribeOn(scheduler).subscribe(ts); |
| 66 |
|
| 67 |
verify(subscriber, never()).onNext(value); |
| 68 |
|
| 69 |
scheduler.triggerActions(); |
| 70 |
|
| 71 |
verify(subscriber, times(1)).onNext(value); |
| 72 |
} |
| 73 |
|
| 74 |
@Test |
| 75 |
public void failure() throws Exception { |
| 76 |
@SuppressWarnings("unchecked") |
| 77 |
Future<Object> future = mock(Future.class); |
| 78 |
RuntimeException e = new RuntimeException(); |
| 79 |
when(future.get()).thenThrow(e); |
| 80 |
|
| 81 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 82 |
|
| 83 |
TestSubscriber<Object> ts = new TestSubscriber<>(subscriber); |
| 84 |
|
| 85 |
Flowable.fromFuture(future).subscribe(ts); |
| 86 |
|
| 87 |
ts.cancel(); |
| 88 |
|
| 89 |
verify(subscriber, never()).onNext(null); |
| 90 |
verify(subscriber, never()).onComplete(); |
| 91 |
verify(subscriber, times(1)).onError(e); |
| 92 |
verify(future, never()).cancel(anyBoolean()); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void cancelledBeforeSubscribe() throws Exception { |
| 97 |
@SuppressWarnings("unchecked") |
| 98 |
Future<Object> future = mock(Future.class); |
| 99 |
CancellationException e = new CancellationException("unit test synthetic cancellation"); |
| 100 |
when(future.get()).thenThrow(e); |
| 101 |
|
| 102 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 103 |
|
| 104 |
TestSubscriber<Object> ts = new TestSubscriber<>(subscriber); |
| 105 |
ts.cancel(); |
| 106 |
|
| 107 |
Flowable.fromFuture(future).subscribe(ts); |
| 108 |
|
| 109 |
ts.assertNoErrors(); |
| 110 |
ts.assertNotComplete(); |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void cancellationDuringFutureGet() throws Exception { |
| 115 |
Future<Object> future = new Future<Object>() { |
| 116 |
private AtomicBoolean isCancelled = new AtomicBoolean(false); |
| 117 |
private AtomicBoolean isDone = new AtomicBoolean(false); |
| 118 |
|
| 119 |
@Override |
| 120 |
public boolean cancel(boolean mayInterruptIfRunning) { |
| 121 |
isCancelled.compareAndSet(false, true); |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isCancelled() { |
| 127 |
return isCancelled.get(); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public boolean isDone() { |
| 132 |
return isCancelled() || isDone.get(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public Object get() throws InterruptedException, ExecutionException { |
| 137 |
Thread.sleep(500); |
| 138 |
isDone.compareAndSet(false, true); |
| 139 |
return "foo"; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 144 |
return get(); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
Subscriber<Object> subscriber = TestHelper.mockSubscriber(); |
| 149 |
|
| 150 |
TestSubscriber<Object> ts = new TestSubscriber<>(subscriber); |
| 151 |
Flowable<Object> futureObservable = Flowable.fromFuture(future); |
| 152 |
|
| 153 |
futureObservable.subscribeOn(Schedulers.computation()).subscribe(ts); |
| 154 |
|
| 155 |
Thread.sleep(100); |
| 156 |
|
| 157 |
ts.cancel(); |
| 158 |
|
| 159 |
ts.assertNoErrors(); |
| 160 |
ts.assertNoValues(); |
| 161 |
ts.assertNotComplete(); |
| 162 |
} |
| 163 |
|
| 164 |
@Test |
| 165 |
public void backpressure() { |
| 166 |
TestSubscriber<Integer> ts = new TestSubscriber<>(0); |
| 167 |
|
| 168 |
FutureTask<Integer> f = new FutureTask<>(new Runnable() { |
| 169 |
@Override |
| 170 |
public void run() { |
| 171 |
|
| 172 |
} |
| 173 |
}, 1); |
| 174 |
|
| 175 |
f.run(); |
| 176 |
|
| 177 |
Flowable.fromFuture(f).subscribe(ts); |
| 178 |
|
| 179 |
ts.assertNoValues(); |
| 180 |
|
| 181 |
ts.request(1); |
| 182 |
|
| 183 |
ts.assertValue(1); |
| 184 |
ts.assertNoErrors(); |
| 185 |
ts.assertComplete(); |
| 186 |
} |
| 187 |
|
| 188 |
@Test |
| 189 |
public void withTimeoutNoTimeout() { |
| 190 |
FutureTask<Integer> task = new FutureTask<>(new Runnable() { |
| 191 |
@Override |
| 192 |
public void run() { |
| 193 |
|
| 194 |
} |
| 195 |
}, 1); |
| 196 |
|
| 197 |
task.run(); |
| 198 |
|
| 199 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 200 |
|
| 201 |
Flowable.fromFuture(task, 1, TimeUnit.SECONDS).subscribe(ts); |
| 202 |
|
| 203 |
ts.assertValue(1); |
| 204 |
ts.assertNoErrors(); |
| 205 |
ts.assertComplete(); |
| 206 |
} |
| 207 |
|
| 208 |
@Test |
| 209 |
public void withTimeoutTimeout() { |
| 210 |
FutureTask<Integer> task = new FutureTask<>(new Runnable() { |
| 211 |
@Override |
| 212 |
public void run() { |
| 213 |
|
| 214 |
} |
| 215 |
}, 1); |
| 216 |
|
| 217 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 218 |
|
| 219 |
Flowable.fromFuture(task, 10, TimeUnit.MILLISECONDS).subscribe(ts); |
| 220 |
|
| 221 |
ts.assertNoValues(); |
| 222 |
ts.assertError(TimeoutException.class); |
| 223 |
ts.assertNotComplete(); |
| 224 |
} |
| 225 |
|
| 226 |
@Test |
| 227 |
public void withTimeoutNoTimeoutScheduler() { |
| 228 |
FutureTask<Integer> task = new FutureTask<>(new Runnable() { |
| 229 |
@Override |
| 230 |
public void run() { |
| 231 |
|
| 232 |
} |
| 233 |
}, 1); |
| 234 |
|
| 235 |
TestSubscriber<Integer> ts = TestSubscriber.create(); |
| 236 |
|
| 237 |
Flowable.fromFuture(task).subscribeOn(Schedulers.computation()).subscribe(ts); |
| 238 |
|
| 239 |
task.run(); |
| 240 |
|
| 241 |
ts.awaitDone(5, TimeUnit.SECONDS); |
| 242 |
ts.assertValue(1); |
| 243 |
ts.assertNoErrors(); |
| 244 |
ts.assertComplete(); |
| 245 |
} |
| 246 |
} |