| 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.maybe; |
| 15 |
|
| 16 |
import static org.junit.Assert.assertTrue; |
| 17 |
|
| 18 |
import java.util.concurrent.*; |
| 19 |
|
| 20 |
import org.junit.Test; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.core.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.TestException; |
| 24 |
import io.reactivex.rxjava3.internal.functions.Functions; |
| 25 |
import io.reactivex.rxjava3.observers.TestObserver; |
| 26 |
import io.reactivex.rxjava3.schedulers.Schedulers; |
| 27 |
|
| 28 |
public class MaybeFromFutureTest extends RxJavaTest { |
| 29 |
|
| 30 |
@Test |
| 31 |
public void cancelImmediately() { |
| 32 |
FutureTask<Integer> ft = new FutureTask<>(Functions.justCallable(1)); |
| 33 |
|
| 34 |
Maybe.fromFuture(ft).test(true) |
| 35 |
.assertEmpty(); |
| 36 |
} |
| 37 |
|
| 38 |
@Test |
| 39 |
public void timeout() { |
| 40 |
FutureTask<Integer> ft = new FutureTask<>(Functions.justCallable(1)); |
| 41 |
|
| 42 |
Maybe.fromFuture(ft, 1, TimeUnit.MILLISECONDS).test() |
| 43 |
.awaitDone(5, TimeUnit.SECONDS) |
| 44 |
.assertFailure(TimeoutException.class); |
| 45 |
} |
| 46 |
|
| 47 |
@Test |
| 48 |
public void timedWait() { |
| 49 |
FutureTask<Integer> ft = new FutureTask<>(Functions.justCallable(1)); |
| 50 |
ft.run(); |
| 51 |
|
| 52 |
Maybe.fromFuture(ft, 1, TimeUnit.MILLISECONDS).test() |
| 53 |
.awaitDone(5, TimeUnit.SECONDS) |
| 54 |
.assertResult(1); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void interrupt() { |
| 59 |
FutureTask<Integer> ft = new FutureTask<>(Functions.justCallable(1)); |
| 60 |
|
| 61 |
Thread.currentThread().interrupt(); |
| 62 |
|
| 63 |
Maybe.fromFuture(ft, 1, TimeUnit.MILLISECONDS).test() |
| 64 |
.assertFailure(InterruptedException.class); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void cancelWhileRunning() { |
| 69 |
final TestObserver<Object> to = new TestObserver<>(); |
| 70 |
|
| 71 |
FutureTask<Object> ft = new FutureTask<>(new Runnable() { |
| 72 |
@Override |
| 73 |
public void run() { |
| 74 |
to.dispose(); |
| 75 |
} |
| 76 |
}, null); |
| 77 |
|
| 78 |
Schedulers.single().scheduleDirect(ft, 100, TimeUnit.MILLISECONDS); |
| 79 |
|
| 80 |
Maybe.fromFuture(ft) |
| 81 |
.subscribeWith(to) |
| 82 |
.assertEmpty(); |
| 83 |
|
| 84 |
assertTrue(to.isDisposed()); |
| 85 |
} |
| 86 |
|
| 87 |
@Test |
| 88 |
public void cancelAndCrashWhileRunning() { |
| 89 |
final TestObserver<Object> to = new TestObserver<>(); |
| 90 |
|
| 91 |
FutureTask<Object> ft = new FutureTask<>(new Runnable() { |
| 92 |
@Override |
| 93 |
public void run() { |
| 94 |
to.dispose(); |
| 95 |
throw new TestException(); |
| 96 |
} |
| 97 |
}, null); |
| 98 |
|
| 99 |
Schedulers.single().scheduleDirect(ft, 100, TimeUnit.MILLISECONDS); |
| 100 |
|
| 101 |
Maybe.fromFuture(ft) |
| 102 |
.subscribeWith(to) |
| 103 |
.assertEmpty(); |
| 104 |
|
| 105 |
assertTrue(to.isDisposed()); |
| 106 |
} |
| 107 |
|
| 108 |
@Test |
| 109 |
public void futureNull() { |
| 110 |
FutureTask<Object> ft = new FutureTask<>(new Runnable() { |
| 111 |
@Override |
| 112 |
public void run() { |
| 113 |
} |
| 114 |
}, null); |
| 115 |
|
| 116 |
Schedulers.single().scheduleDirect(ft, 100, TimeUnit.MILLISECONDS); |
| 117 |
|
| 118 |
Maybe.fromFuture(ft) |
| 119 |
.test() |
| 120 |
.assertResult(); |
| 121 |
} |
| 122 |
} |