Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/observers/FutureSingleObserverTest.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.observers;
15
16 import static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage;
17 import static org.junit.Assert.*;
18
19 import java.util.concurrent.*;
20
21 import org.junit.Test;
22
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.Disposable;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.internal.functions.Functions;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.subjects.PublishSubject;
29 import io.reactivex.rxjava3.testsupport.TestHelper;
30
31 public class FutureSingleObserverTest extends RxJavaTest {
32
33 @Test
34 public void cancel() {
35 final Future<?> f = Single.never().toFuture();
36
37 assertFalse(f.isCancelled());
38 assertFalse(f.isDone());
39
40 f.cancel(true);
41
42 assertTrue(f.isCancelled());
43 assertTrue(f.isDone());
44
45 try {
46 f.get();
47 fail("Should have thrown!");
48 } catch (CancellationException ex) {
49 // expected
50 } catch (InterruptedException ex) {
51 throw new AssertionError(ex);
52 } catch (ExecutionException ex) {
53 throw new AssertionError(ex);
54 }
55
56 try {
57 f.get(5, TimeUnit.SECONDS);
58 fail("Should have thrown!");
59 } catch (CancellationException ex) {
60 // expected
61 } catch (InterruptedException ex) {
62 throw new AssertionError(ex);
63 } catch (ExecutionException ex) {
64 throw new AssertionError(ex);
65 } catch (TimeoutException ex) {
66 throw new AssertionError(ex);
67 }
68 }
69
70 @Test
71 public void cancelRace() {
72 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
73 final Future<?> f = Single.never().toFuture();
74
75 Runnable r = new Runnable() {
76 @Override
77 public void run() {
78 f.cancel(true);
79 }
80 };
81
82 TestHelper.race(r, r);
83 }
84 }
85
86 @Test
87 public void timeout() throws Exception {
88
89 Future<?> f = Single.never().toFuture();
90
91 try {
92 f.get(100, TimeUnit.MILLISECONDS);
93 fail("Should have thrown");
94 } catch (TimeoutException expected) {
95 assertEquals(timeoutMessage(100, TimeUnit.MILLISECONDS), expected.getMessage());
96 }
97 }
98
99 @Test
100 public void dispose() {
101 Future<Integer> f = Single.just(1).toFuture();
102
103 ((Disposable)f).dispose();
104
105 assertTrue(((Disposable)f).isDisposed());
106 }
107
108 @Test
109 public void errorGetWithTimeout() throws Exception {
110 Future<?> f = Single.error(new TestException()).toFuture();
111
112 try {
113 f.get(5, TimeUnit.SECONDS);
114 fail("Should have thrown");
115 } catch (ExecutionException ex) {
116 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
117 }
118 }
119
120 @Test
121 public void normalGetWitHTimeout() throws Exception {
122 Future<Integer> f = Single.just(1).toFuture();
123
124 assertEquals(1, f.get(5, TimeUnit.SECONDS).intValue());
125 }
126
127 @Test
128 public void getAwait() throws Exception {
129 Future<Integer> f = Single.just(1).delay(100, TimeUnit.MILLISECONDS).toFuture();
130
131 assertEquals(1, f.get(5, TimeUnit.SECONDS).intValue());
132 }
133
134 @Test
135 public void onSuccessCancelRace() {
136 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
137 final PublishSubject<Integer> ps = PublishSubject.create();
138
139 final Future<?> f = ps.single(-99).toFuture();
140
141 ps.onNext(1);
142
143 Runnable r1 = new Runnable() {
144 @Override
145 public void run() {
146 f.cancel(true);
147 }
148 };
149
150 Runnable r2 = new Runnable() {
151 @Override
152 public void run() {
153 ps.onComplete();
154 }
155 };
156
157 TestHelper.race(r1, r2);
158 }
159 }
160
161 @Test
162 public void onErrorCancelRace() {
163 RxJavaPlugins.setErrorHandler(Functions.emptyConsumer());
164 try {
165 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
166 final PublishSubject<Integer> ps = PublishSubject.create();
167
168 final Future<?> f = ps.single(-99).toFuture();
169
170 final TestException ex = new TestException();
171
172 Runnable r1 = new Runnable() {
173 @Override
174 public void run() {
175 f.cancel(true);
176 }
177 };
178
179 Runnable r2 = new Runnable() {
180 @Override
181 public void run() {
182 ps.onError(ex);
183 }
184 };
185
186 TestHelper.race(r1, r2);
187 }
188 } finally {
189 RxJavaPlugins.reset();
190 }
191 }
192 }