| 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.mockito.ArgumentMatchers.any; |
| 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 |
|
| 24 |
import io.reactivex.rxjava3.core.*; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.schedulers.*; |
| 27 |
import io.reactivex.rxjava3.testsupport.TestHelper; |
| 28 |
|
| 29 |
public class ObservableToFutureTest extends RxJavaTest { |
| 30 |
|
| 31 |
@Test |
| 32 |
public void success() throws Exception { |
| 33 |
@SuppressWarnings("unchecked") |
| 34 |
Future<Object> future = mock(Future.class); |
| 35 |
Object value = new Object(); |
| 36 |
when(future.get()).thenReturn(value); |
| 37 |
|
| 38 |
Observer<Object> o = TestHelper.mockObserver(); |
| 39 |
|
| 40 |
TestObserver<Object> to = new TestObserver<>(o); |
| 41 |
|
| 42 |
Observable.fromFuture(future).subscribe(to); |
| 43 |
|
| 44 |
to.dispose(); |
| 45 |
|
| 46 |
verify(o, times(1)).onNext(value); |
| 47 |
verify(o, times(1)).onComplete(); |
| 48 |
verify(o, never()).onError(any(Throwable.class)); |
| 49 |
verify(future, never()).cancel(true); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void successOperatesOnSuppliedScheduler() throws Exception { |
| 54 |
@SuppressWarnings("unchecked") |
| 55 |
Future<Object> future = mock(Future.class); |
| 56 |
Object value = new Object(); |
| 57 |
when(future.get()).thenReturn(value); |
| 58 |
|
| 59 |
Observer<Object> o = TestHelper.mockObserver(); |
| 60 |
|
| 61 |
TestScheduler scheduler = new TestScheduler(); |
| 62 |
TestObserver<Object> to = new TestObserver<>(o); |
| 63 |
|
| 64 |
Observable.fromFuture(future).subscribeOn(scheduler).subscribe(to); |
| 65 |
|
| 66 |
verify(o, never()).onNext(value); |
| 67 |
|
| 68 |
scheduler.triggerActions(); |
| 69 |
|
| 70 |
verify(o, times(1)).onNext(value); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void failure() throws Exception { |
| 75 |
@SuppressWarnings("unchecked") |
| 76 |
Future<Object> future = mock(Future.class); |
| 77 |
RuntimeException e = new RuntimeException(); |
| 78 |
when(future.get()).thenThrow(e); |
| 79 |
|
| 80 |
Observer<Object> o = TestHelper.mockObserver(); |
| 81 |
|
| 82 |
TestObserver<Object> to = new TestObserver<>(o); |
| 83 |
|
| 84 |
Observable.fromFuture(future).subscribe(to); |
| 85 |
|
| 86 |
to.dispose(); |
| 87 |
|
| 88 |
verify(o, never()).onNext(null); |
| 89 |
verify(o, never()).onComplete(); |
| 90 |
verify(o, times(1)).onError(e); |
| 91 |
verify(future, never()).cancel(true); |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
public void cancelledBeforeSubscribe() throws Exception { |
| 96 |
@SuppressWarnings("unchecked") |
| 97 |
Future<Object> future = mock(Future.class); |
| 98 |
CancellationException e = new CancellationException("unit test synthetic cancellation"); |
| 99 |
when(future.get()).thenThrow(e); |
| 100 |
|
| 101 |
Observer<Object> o = TestHelper.mockObserver(); |
| 102 |
|
| 103 |
TestObserver<Object> to = new TestObserver<>(o); |
| 104 |
to.dispose(); |
| 105 |
|
| 106 |
Observable.fromFuture(future).subscribe(to); |
| 107 |
|
| 108 |
to.assertNoErrors(); |
| 109 |
to.assertNotComplete(); |
| 110 |
} |
| 111 |
|
| 112 |
@Test |
| 113 |
public void cancellationDuringFutureGet() throws Exception { |
| 114 |
Future<Object> future = new Future<Object>() { |
| 115 |
private AtomicBoolean isCancelled = new AtomicBoolean(false); |
| 116 |
private AtomicBoolean isDone = new AtomicBoolean(false); |
| 117 |
|
| 118 |
@Override |
| 119 |
public boolean cancel(boolean mayInterruptIfRunning) { |
| 120 |
isCancelled.compareAndSet(false, true); |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public boolean isCancelled() { |
| 126 |
return isCancelled.get(); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public boolean isDone() { |
| 131 |
return isCancelled() || isDone.get(); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public Object get() throws InterruptedException, ExecutionException { |
| 136 |
Thread.sleep(500); |
| 137 |
isDone.compareAndSet(false, true); |
| 138 |
return "foo"; |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public Object get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
| 143 |
return get(); |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
Observer<Object> o = TestHelper.mockObserver(); |
| 148 |
|
| 149 |
TestObserver<Object> to = new TestObserver<>(o); |
| 150 |
Observable<Object> futureObservable = Observable.fromFuture(future); |
| 151 |
|
| 152 |
futureObservable.subscribeOn(Schedulers.computation()).subscribe(to); |
| 153 |
|
| 154 |
Thread.sleep(100); |
| 155 |
|
| 156 |
to.dispose(); |
| 157 |
|
| 158 |
to.assertNoErrors(); |
| 159 |
to.assertNoValues(); |
| 160 |
to.assertNotComplete(); |
| 161 |
} |
| 162 |
} |