Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscribers/FutureSubscriberTest.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.subscribers;
15
16 import static io.reactivex.rxjava3.internal.util.ExceptionHelper.timeoutMessage;
17 import static org.junit.Assert.*;
18
19 import java.util.*;
20 import java.util.concurrent.*;
21
22 import org.junit.*;
23
24 import io.reactivex.rxjava3.core.RxJavaTest;
25 import io.reactivex.rxjava3.exceptions.TestException;
26 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
27 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
28 import io.reactivex.rxjava3.schedulers.Schedulers;
29 import io.reactivex.rxjava3.testsupport.*;
30
31 public class FutureSubscriberTest extends RxJavaTest {
32
33 FutureSubscriber<Integer> fs;
34
35 @Before
36 public void before() {
37 fs = new FutureSubscriber<>();
38 }
39
40 @Test
41 public void cancel() throws Exception {
42 assertFalse(fs.isDone());
43
44 assertFalse(fs.isCancelled());
45
46 fs.cancel();
47
48 fs.cancel();
49
50 fs.request(10);
51
52 fs.request(-99);
53
54 fs.cancel(false);
55
56 assertTrue(fs.isDone());
57
58 assertTrue(fs.isCancelled());
59
60 try {
61 fs.get();
62 fail("Should have thrown");
63 } catch (CancellationException ex) {
64 // expected
65 }
66
67 try {
68 fs.get(1, TimeUnit.MILLISECONDS);
69 fail("Should have thrown");
70 } catch (CancellationException ex) {
71 // expected
72 }
73 }
74
75 @Test
76 public void onError() throws Exception {
77 List<Throwable> errors = TestHelper.trackPluginErrors();
78
79 try {
80 fs.onError(new TestException("One"));
81
82 fs.onError(new TestException("Two"));
83
84 try {
85 fs.get(5, TimeUnit.MILLISECONDS);
86 } catch (ExecutionException ex) {
87 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
88 assertEquals("One", ex.getCause().getMessage());
89 }
90
91 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Two");
92 } finally {
93 RxJavaPlugins.reset();
94 }
95 }
96
97 @Test
98 public void onNext() throws Exception {
99 fs.onNext(1);
100 fs.onComplete();
101
102 assertEquals(1, fs.get(5, TimeUnit.MILLISECONDS).intValue());
103 }
104
105 @Test
106 public void onSubscribe() throws Exception {
107 List<Throwable> errors = TestHelper.trackPluginErrors();
108
109 try {
110
111 BooleanSubscription s = new BooleanSubscription();
112
113 fs.onSubscribe(s);
114
115 BooleanSubscription s2 = new BooleanSubscription();
116
117 fs.onSubscribe(s2);
118
119 assertFalse(s.isCancelled());
120 assertTrue(s2.isCancelled());
121
122 TestHelper.assertError(errors, 0, IllegalStateException.class, "Subscription already set!");
123 } finally {
124 RxJavaPlugins.reset();
125 }
126 }
127
128 @Test
129 public void cancelRace() {
130 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
131 final FutureSubscriber<Integer> fs = new FutureSubscriber<>();
132
133 Runnable r = new Runnable() {
134 @Override
135 public void run() {
136 fs.cancel(false);
137 }
138 };
139
140 TestHelper.race(r, r);
141 }
142 }
143
144 @Test
145 public void await() throws Exception {
146 Schedulers.single().scheduleDirect(new Runnable() {
147 @Override
148 public void run() {
149 fs.onNext(1);
150 fs.onComplete();
151 }
152 }, 100, TimeUnit.MILLISECONDS);
153
154 assertEquals(1, fs.get(5, TimeUnit.SECONDS).intValue());
155 }
156
157 @Test
158 @SuppressUndeliverable
159 public void onErrorCancelRace() {
160 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
161 final FutureSubscriber<Integer> fs = new FutureSubscriber<>();
162
163 final TestException ex = new TestException();
164
165 Runnable r1 = new Runnable() {
166 @Override
167 public void run() {
168 fs.cancel(false);
169 }
170 };
171
172 Runnable r2 = new Runnable() {
173 @Override
174 public void run() {
175 fs.onError(ex);
176 }
177 };
178
179 TestHelper.race(r1, r2);
180 }
181 }
182
183 @Test
184 @SuppressUndeliverable
185 public void onCompleteCancelRace() {
186 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
187 final FutureSubscriber<Integer> fs = new FutureSubscriber<>();
188
189 if (i % 3 == 0) {
190 fs.onSubscribe(new BooleanSubscription());
191 }
192
193 if (i % 2 == 0) {
194 fs.onNext(1);
195 }
196
197 Runnable r1 = new Runnable() {
198 @Override
199 public void run() {
200 fs.cancel(false);
201 }
202 };
203
204 Runnable r2 = new Runnable() {
205 @Override
206 public void run() {
207 fs.onComplete();
208 }
209 };
210
211 TestHelper.race(r1, r2);
212 }
213 }
214
215 @Test
216 @SuppressUndeliverable
217 public void onErrorOnComplete() throws Exception {
218 fs.onError(new TestException("One"));
219 fs.onComplete();
220
221 try {
222 fs.get(5, TimeUnit.MILLISECONDS);
223 } catch (ExecutionException ex) {
224 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
225 assertEquals("One", ex.getCause().getMessage());
226 }
227 }
228
229 @Test
230 @SuppressUndeliverable
231 public void onCompleteOnError() throws Exception {
232 fs.onComplete();
233 fs.onError(new TestException("One"));
234
235 try {
236 assertNull(fs.get(5, TimeUnit.MILLISECONDS));
237 } catch (ExecutionException ex) {
238 assertTrue(ex.toString(), ex.getCause() instanceof NoSuchElementException);
239 }
240 }
241
242 @Test
243 @SuppressUndeliverable
244 public void cancelOnError() throws Exception {
245 fs.cancel(true);
246 fs.onError(new TestException("One"));
247
248 try {
249 fs.get(5, TimeUnit.MILLISECONDS);
250 fail("Should have thrown");
251 } catch (CancellationException ex) {
252 // expected
253 }
254 }
255
256 @Test
257 @SuppressUndeliverable
258 public void cancelOnComplete() throws Exception {
259 fs.cancel(true);
260 fs.onComplete();
261
262 try {
263 fs.get(5, TimeUnit.MILLISECONDS);
264 fail("Should have thrown");
265 } catch (CancellationException ex) {
266 // expected
267 }
268 }
269
270 @Test
271 public void onNextThenOnCompleteTwice() throws Exception {
272 fs.onNext(1);
273 fs.onComplete();
274 fs.onComplete();
275
276 assertEquals(1, fs.get(5, TimeUnit.MILLISECONDS).intValue());
277 }
278
279 @Test
280 public void completeAsync() throws Exception {
281 Schedulers.single().scheduleDirect(new Runnable() {
282 @Override
283 public void run() {
284 fs.onNext(1);
285 fs.onComplete();
286 }
287 }, 500, TimeUnit.MILLISECONDS);
288
289 assertEquals(1, fs.get().intValue());
290 }
291
292 @Test
293 public void getTimedOut() throws Exception {
294 try {
295 fs.get(1, TimeUnit.NANOSECONDS);
296 fail("Should have thrown");
297 } catch (TimeoutException expected) {
298 assertEquals(timeoutMessage(1, TimeUnit.NANOSECONDS), expected.getMessage());
299 }
300 }
301
302 @Test
303 public void onNextCompleteOnError() throws Exception {
304 List<Throwable> errors = TestHelper.trackPluginErrors();
305 try {
306 fs.onNext(1);
307 fs.onComplete();
308 fs.onError(new TestException("One"));
309
310 assertEquals((Integer)1, fs.get(5, TimeUnit.MILLISECONDS));
311
312 TestHelper.assertUndeliverable(errors, 0, TestException.class);
313 } finally {
314 RxJavaPlugins.reset();
315 }
316 }
317 }