Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/observers/FutureObserverTest.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.*;
20 import java.util.concurrent.*;
21
22 import org.junit.*;
23
24 import io.reactivex.rxjava3.core.RxJavaTest;
25 import io.reactivex.rxjava3.disposables.Disposable;
26 import io.reactivex.rxjava3.exceptions.TestException;
27 import io.reactivex.rxjava3.internal.functions.Functions;
28 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
29 import io.reactivex.rxjava3.schedulers.Schedulers;
30 import io.reactivex.rxjava3.testsupport.TestHelper;
31
32 public class FutureObserverTest extends RxJavaTest {
33 FutureObserver<Integer> fo;
34
35 @Before
36 public void before() {
37 fo = new FutureObserver<>();
38 }
39
40 @Test
41 public void cancel2() {
42
43 fo.dispose();
44
45 assertFalse(fo.isCancelled());
46 assertFalse(fo.isDisposed());
47 assertFalse(fo.isDone());
48
49 for (int i = 0; i < 2; i++) {
50 fo.cancel(i == 0);
51
52 assertTrue(fo.isCancelled());
53 assertTrue(fo.isDisposed());
54 assertTrue(fo.isDone());
55 }
56
57 List<Throwable> errors = TestHelper.trackPluginErrors();
58 try {
59 fo.onNext(1);
60 fo.onError(new TestException("First"));
61 fo.onError(new TestException("Second"));
62 fo.onComplete();
63
64 assertTrue(fo.isCancelled());
65 assertTrue(fo.isDisposed());
66 assertTrue(fo.isDone());
67
68 TestHelper.assertUndeliverable(errors, 0, TestException.class);
69 TestHelper.assertUndeliverable(errors, 1, TestException.class);
70 } finally {
71 RxJavaPlugins.reset();
72 }
73 }
74
75 @Test
76 public void cancel() throws Exception {
77 assertFalse(fo.isDone());
78
79 assertFalse(fo.isCancelled());
80
81 fo.cancel(false);
82
83 assertTrue(fo.isDone());
84
85 assertTrue(fo.isCancelled());
86
87 try {
88 fo.get();
89 fail("Should have thrown");
90 } catch (CancellationException ex) {
91 // expected
92 }
93
94 try {
95 fo.get(1, TimeUnit.MILLISECONDS);
96 fail("Should have thrown");
97 } catch (CancellationException ex) {
98 // expected
99 }
100 }
101
102 @Test
103 public void onError() throws Exception {
104 List<Throwable> errors = TestHelper.trackPluginErrors();
105
106 try {
107 fo.onError(new TestException("One"));
108
109 fo.onError(new TestException("Two"));
110
111 try {
112 fo.get(5, TimeUnit.MILLISECONDS);
113 } catch (ExecutionException ex) {
114 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
115 assertEquals("One", ex.getCause().getMessage());
116 }
117
118 TestHelper.assertUndeliverable(errors, 0, TestException.class, "Two");
119 } finally {
120 RxJavaPlugins.reset();
121 }
122 }
123
124 @Test
125 public void onNext() throws Exception {
126 fo.onNext(1);
127 fo.onComplete();
128
129 assertEquals(1, fo.get(5, TimeUnit.MILLISECONDS).intValue());
130 }
131
132 @Test
133 public void onSubscribe() throws Exception {
134 List<Throwable> errors = TestHelper.trackPluginErrors();
135
136 try {
137
138 Disposable d1 = Disposable.empty();
139
140 fo.onSubscribe(d1);
141
142 Disposable d2 = Disposable.empty();
143
144 fo.onSubscribe(d2);
145
146 assertFalse(d1.isDisposed());
147 assertTrue(d2.isDisposed());
148
149 TestHelper.assertError(errors, 0, IllegalStateException.class, "Disposable already set!");
150 } finally {
151 RxJavaPlugins.reset();
152 }
153 }
154
155 @Test
156 public void cancelRace() {
157 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
158 final FutureObserver<Integer> fo = new FutureObserver<>();
159
160 Runnable r = new Runnable() {
161 @Override
162 public void run() {
163 fo.cancel(false);
164 }
165 };
166
167 TestHelper.race(r, r);
168 }
169 }
170
171 @Test
172 public void await() throws Exception {
173 Schedulers.single().scheduleDirect(new Runnable() {
174 @Override
175 public void run() {
176 fo.onNext(1);
177 fo.onComplete();
178 }
179 }, 100, TimeUnit.MILLISECONDS);
180
181 assertEquals(1, fo.get(5, TimeUnit.SECONDS).intValue());
182 }
183
184 @Test
185 public void onErrorCancelRace() {
186 RxJavaPlugins.setErrorHandler(Functions.emptyConsumer());
187 try {
188 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
189 final FutureObserver<Integer> fo = new FutureObserver<>();
190
191 final TestException ex = new TestException();
192
193 Runnable r1 = new Runnable() {
194 @Override
195 public void run() {
196 fo.cancel(false);
197 }
198 };
199
200 Runnable r2 = new Runnable() {
201 @Override
202 public void run() {
203 fo.onError(ex);
204 }
205 };
206
207 TestHelper.race(r1, r2);
208 }
209 } finally {
210 RxJavaPlugins.reset();
211 }
212 }
213
214 @Test
215 public void onCompleteCancelRace() {
216 RxJavaPlugins.setErrorHandler(Functions.emptyConsumer());
217 try {
218 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
219 final FutureObserver<Integer> fo = new FutureObserver<>();
220
221 if (i % 3 == 0) {
222 fo.onSubscribe(Disposable.empty());
223 }
224
225 if (i % 2 == 0) {
226 fo.onNext(1);
227 }
228
229 Runnable r1 = new Runnable() {
230 @Override
231 public void run() {
232 fo.cancel(false);
233 }
234 };
235
236 Runnable r2 = new Runnable() {
237 @Override
238 public void run() {
239 fo.onComplete();
240 }
241 };
242
243 TestHelper.race(r1, r2);
244 }
245 } finally {
246 RxJavaPlugins.reset();
247 }
248 }
249
250 @Test
251 public void onErrorOnComplete() throws Exception {
252 List<Throwable> errors = TestHelper.trackPluginErrors();
253 try {
254 fo.onError(new TestException("One"));
255 fo.onComplete();
256
257 try {
258 fo.get(5, TimeUnit.MILLISECONDS);
259 } catch (ExecutionException ex) {
260 assertTrue(ex.toString(), ex.getCause() instanceof TestException);
261 assertEquals("One", ex.getCause().getMessage());
262 }
263
264 TestHelper.assertUndeliverable(errors, 0, NoSuchElementException.class);
265 } finally {
266 RxJavaPlugins.reset();
267 }
268 }
269
270 @Test
271 public void onCompleteOnError() throws Exception {
272 List<Throwable> errors = TestHelper.trackPluginErrors();
273 try {
274 fo.onComplete();
275 fo.onError(new TestException("One"));
276
277 try {
278 assertNull(fo.get(5, TimeUnit.MILLISECONDS));
279 } catch (ExecutionException ex) {
280 assertTrue(ex.toString(), ex.getCause() instanceof NoSuchElementException);
281 }
282
283 TestHelper.assertUndeliverable(errors, 0, TestException.class);
284 } finally {
285 RxJavaPlugins.reset();
286 }
287 }
288
289 @Test
290 public void onNextCompleteOnError() throws Exception {
291 List<Throwable> errors = TestHelper.trackPluginErrors();
292 try {
293 fo.onNext(1);
294 fo.onComplete();
295 fo.onError(new TestException("One"));
296
297 assertEquals((Integer)1, fo.get(5, TimeUnit.MILLISECONDS));
298
299 TestHelper.assertUndeliverable(errors, 0, TestException.class);
300 } finally {
301 RxJavaPlugins.reset();
302 }
303 }
304
305 @Test
306 public void cancelOnError() throws Exception {
307 List<Throwable> errors = TestHelper.trackPluginErrors();
308 try {
309 fo.cancel(true);
310 fo.onError(new TestException("One"));
311
312 try {
313 fo.get(5, TimeUnit.MILLISECONDS);
314 fail("Should have thrown");
315 } catch (CancellationException ex) {
316 // expected
317 }
318 TestHelper.assertUndeliverable(errors, 0, TestException.class);
319 } finally {
320 RxJavaPlugins.reset();
321 }
322 }
323
324 @Test
325 public void cancelOnComplete() throws Exception {
326 List<Throwable> errors = TestHelper.trackPluginErrors();
327 try {
328 fo.cancel(true);
329 fo.onComplete();
330
331 try {
332 fo.get(5, TimeUnit.MILLISECONDS);
333 fail("Should have thrown");
334 } catch (CancellationException ex) {
335 // expected
336 }
337
338 TestHelper.assertUndeliverable(errors, 0, NoSuchElementException.class);
339 } finally {
340 RxJavaPlugins.reset();
341 }
342 }
343
344 @Test
345 public void onNextThenOnCompleteTwice() throws Exception {
346 fo.onNext(1);
347 fo.onComplete();
348 fo.onComplete();
349
350 assertEquals(1, fo.get(5, TimeUnit.MILLISECONDS).intValue());
351 }
352
353 @Test(expected = InterruptedException.class)
354 public void getInterrupted() throws Exception {
355 Thread.currentThread().interrupt();
356 fo.get();
357 }
358
359 @Test
360 public void completeAsync() throws Exception {
361 Schedulers.single().scheduleDirect(new Runnable() {
362 @Override
363 public void run() {
364 fo.onNext(1);
365 fo.onComplete();
366 }
367 }, 500, TimeUnit.MILLISECONDS);
368
369 assertEquals(1, fo.get().intValue());
370 }
371
372 @Test
373 public void getTimedOut() throws Exception {
374 try {
375 fo.get(1, TimeUnit.NANOSECONDS);
376 fail("Should have thrown");
377 } catch (TimeoutException expected) {
378 assertEquals(timeoutMessage(1, TimeUnit.NANOSECONDS), expected.getMessage());
379 }
380 }
381
382 @Test
383 public void cancelOnSubscribeRace() {
384 for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) {
385 final FutureObserver<Integer> fo = new FutureObserver<>();
386
387 Runnable r = new Runnable() {
388 @Override
389 public void run() {
390 fo.cancel(false);
391 }
392 };
393
394 Disposable d = Disposable.empty();
395
396 TestHelper.race(r, () -> fo.onSubscribe(d));
397 }
398 }
399 }