Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/operators/observable/BlockingObservableToFutureTest.java

raw
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.junit.Assert.*;
17
18 import java.util.*;
19 import java.util.concurrent.*;
20
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import org.junit.Test;
23
24 import io.reactivex.rxjava3.core.*;
25 import io.reactivex.rxjava3.core.Observable;
26 import io.reactivex.rxjava3.core.Observer;
27 import io.reactivex.rxjava3.exceptions.TestException;
28
29 public class BlockingObservableToFutureTest extends RxJavaTest {
30 @Test
31 public void toFuture() throws InterruptedException, ExecutionException {
32 Observable<String> obs = Observable.just("one");
33 Future<String> f = obs.toFuture();
34 assertEquals("one", f.get());
35 }
36
37 @Test
38 public void toFutureList() throws InterruptedException, ExecutionException {
39 Observable<String> obs = Observable.just("one", "two", "three");
40 Future<List<String>> f = obs.toList().toFuture();
41 assertEquals("one", f.get().get(0));
42 assertEquals("two", f.get().get(1));
43 assertEquals("three", f.get().get(2));
44 }
45
46 @Test(expected = IndexOutOfBoundsException.class)
47 public void exceptionWithMoreThanOneElement() throws Throwable {
48 Observable<String> obs = Observable.just("one", "two");
49 Future<String> f = obs.toFuture();
50 try {
51 // we expect an exception since there are more than 1 element
52 f.get();
53 fail("Should have thrown!");
54 }
55 catch (ExecutionException e) {
56 throw e.getCause();
57 }
58 }
59
60 @Test
61 public void toFutureWithException() {
62 Observable<String> obs = Observable.unsafeCreate(new ObservableSource<String>() {
63
64 @Override
65 public void subscribe(Observer<? super String> observer) {
66 observer.onSubscribe(Disposable.empty());
67 observer.onNext("one");
68 observer.onError(new TestException());
69 }
70 });
71
72 Future<String> f = obs.toFuture();
73 try {
74 f.get();
75 fail("expected exception");
76 } catch (Throwable e) {
77 assertEquals(TestException.class, e.getCause().getClass());
78 }
79 }
80
81 @Test(expected = CancellationException.class)
82 public void getAfterCancel() throws Exception {
83 Observable<String> obs = Observable.never();
84 Future<String> f = obs.toFuture();
85 boolean cancelled = f.cancel(true);
86 assertTrue(cancelled); // because OperationNeverComplete never does
87 f.get(); // Future.get() docs require this to throw
88 }
89
90 @Test(expected = CancellationException.class)
91 public void getWithTimeoutAfterCancel() throws Exception {
92 Observable<String> obs = Observable.never();
93 Future<String> f = obs.toFuture();
94 boolean cancelled = f.cancel(true);
95 assertTrue(cancelled); // because OperationNeverComplete never does
96 f.get(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // Future.get() docs require this to throw
97 }
98
99 @Test(expected = NoSuchElementException.class)
100 public void getWithEmptyFlowable() throws Throwable {
101 Observable<String> obs = Observable.empty();
102 Future<String> f = obs.toFuture();
103 try {
104 f.get();
105 }
106 catch (ExecutionException e) {
107 throw e.getCause();
108 }
109 }
110 }