|
| 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.*; |
| 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.schedulers.Schedulers; |
| 25 | +import io.reactivex.rxjava3.subjects.MaybeSubject; |
| 26 | + |
| 27 | +public class MaybeToFutureTest extends RxJavaTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void success() throws Exception { |
| 31 | + assertEquals((Integer)1, Maybe.just(1) |
| 32 | + .subscribeOn(Schedulers.computation()) |
| 33 | + .toFuture() |
| 34 | + .get()); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void empty() throws Exception { |
| 39 | + assertNull(Maybe.empty() |
| 40 | + .subscribeOn(Schedulers.computation()) |
| 41 | + .toFuture() |
| 42 | + .get()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void error() throws InterruptedException { |
| 47 | + try { |
| 48 | + Maybe.error(new TestException()) |
| 49 | + .subscribeOn(Schedulers.computation()) |
| 50 | + .toFuture() |
| 51 | + .get(); |
| 52 | + |
| 53 | + fail("Should have thrown!"); |
| 54 | + } catch (ExecutionException ex) { |
| 55 | + assertTrue("" + ex.getCause(), ex.getCause() instanceof TestException); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void cancel() { |
| 61 | + MaybeSubject<Integer> ms = MaybeSubject.create(); |
| 62 | + |
| 63 | + Future<Integer> f = ms.toFuture(); |
| 64 | + |
| 65 | + assertTrue(ms.hasObservers()); |
| 66 | + |
| 67 | + f.cancel(true); |
| 68 | + |
| 69 | + assertFalse(ms.hasObservers()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void cancel2() { |
| 74 | + MaybeSubject<Integer> ms = MaybeSubject.create(); |
| 75 | + |
| 76 | + Future<Integer> f = ms.toFuture(); |
| 77 | + |
| 78 | + assertTrue(ms.hasObservers()); |
| 79 | + |
| 80 | + f.cancel(false); |
| 81 | + |
| 82 | + assertFalse(ms.hasObservers()); |
| 83 | + } |
| 84 | +} |
0 commit comments