Debian Codegraph @

src/test/java/io/reactivex/rxjava3/schedulers/FailOnBlockingTest.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.schedulers;
15
16 import java.util.concurrent.TimeUnit;
17
18 import org.junit.Test;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.functions.*;
22 import io.reactivex.rxjava3.internal.functions.Functions;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 public class FailOnBlockingTest extends RxJavaTest {
26
27 @Test
28 public void failComputationFlowableBlockingFirst() {
29
30 try {
31 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
32
33 Flowable.just(1)
34 .subscribeOn(Schedulers.computation())
35 .map(new Function<Integer, Integer>() {
36 @Override
37 public Integer apply(Integer v) throws Exception {
38
39 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingFirst();
40
41 return v;
42 }
43 })
44 .test()
45 .awaitDone(5, TimeUnit.SECONDS)
46 .assertFailure(IllegalStateException.class);
47 } finally {
48 RxJavaPlugins.reset();
49 }
50
51 }
52
53 @Test
54 public void failComputationFlowableBlockingLast() {
55
56 try {
57 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
58
59 Flowable.just(1)
60 .subscribeOn(Schedulers.computation())
61 .map(new Function<Integer, Integer>() {
62 @Override
63 public Integer apply(Integer v) throws Exception {
64
65 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingLast();
66
67 return v;
68 }
69 })
70 .test()
71 .awaitDone(5, TimeUnit.SECONDS)
72 .assertFailure(IllegalStateException.class);
73 } finally {
74 RxJavaPlugins.reset();
75 }
76
77 }
78
79 @Test
80 public void failComputationFlowableBlockingIterable() {
81
82 try {
83 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
84
85 Flowable.just(1)
86 .subscribeOn(Schedulers.computation())
87 .map(new Function<Integer, Integer>() {
88 @Override
89 public Integer apply(Integer v) throws Exception {
90
91 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingIterable().iterator().next();
92
93 return v;
94 }
95 })
96 .test()
97 .awaitDone(5, TimeUnit.SECONDS)
98 .assertFailure(IllegalStateException.class);
99 } finally {
100 RxJavaPlugins.reset();
101 }
102
103 }
104
105 @Test
106 public void failComputationFlowableBlockingSubscribe() {
107
108 try {
109 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
110
111 Flowable.just(1)
112 .subscribeOn(Schedulers.computation())
113 .map(new Function<Integer, Integer>() {
114 @Override
115 public Integer apply(Integer v) throws Exception {
116
117 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingSubscribe();
118
119 return v;
120 }
121 })
122 .test()
123 .awaitDone(5, TimeUnit.SECONDS)
124 .assertFailure(IllegalStateException.class);
125 } finally {
126 RxJavaPlugins.reset();
127 }
128
129 }
130
131 @Test
132 public void failComputationFlowableBlockingSingle() {
133
134 try {
135 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
136
137 Flowable.just(1)
138 .subscribeOn(Schedulers.computation())
139 .map(new Function<Integer, Integer>() {
140 @Override
141 public Integer apply(Integer v) throws Exception {
142
143 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingSingle();
144
145 return v;
146 }
147 })
148 .test()
149 .awaitDone(5, TimeUnit.SECONDS)
150 .assertFailure(IllegalStateException.class);
151 } finally {
152 RxJavaPlugins.reset();
153 }
154
155 }
156
157 @Test
158 public void failComputationFlowableBlockingForEach() {
159
160 try {
161 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
162
163 Flowable.just(1)
164 .subscribeOn(Schedulers.computation())
165 .map(new Function<Integer, Integer>() {
166 @Override
167 public Integer apply(Integer v) throws Exception {
168
169 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingForEach(Functions.emptyConsumer());
170
171 return v;
172 }
173 })
174 .test()
175 .awaitDone(5, TimeUnit.SECONDS)
176 .assertFailure(IllegalStateException.class);
177 } finally {
178 RxJavaPlugins.reset();
179 }
180
181 }
182
183 @Test
184 public void failComputationFlowableBlockingLatest() {
185
186 try {
187 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
188
189 Flowable.just(1)
190 .subscribeOn(Schedulers.computation())
191 .map(new Function<Integer, Integer>() {
192 @Override
193 public Integer apply(Integer v) throws Exception {
194
195 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingLatest().iterator().hasNext();
196
197 return v;
198 }
199 })
200 .test()
201 .awaitDone(5, TimeUnit.SECONDS)
202 .assertFailure(IllegalStateException.class);
203 } finally {
204 RxJavaPlugins.reset();
205 }
206
207 }
208
209 @Test
210 public void failComputationFlowableBlockingNext() {
211
212 try {
213 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
214
215 Flowable.just(1)
216 .subscribeOn(Schedulers.computation())
217 .map(new Function<Integer, Integer>() {
218 @Override
219 public Integer apply(Integer v) throws Exception {
220
221 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingNext().iterator().hasNext();
222
223 return v;
224 }
225 })
226 .test()
227 .awaitDone(5, TimeUnit.SECONDS)
228 .assertFailure(IllegalStateException.class);
229 } finally {
230 RxJavaPlugins.reset();
231 }
232
233 }
234
235 @Test
236 public void failComputationFlowableToFuture() {
237
238 try {
239 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
240
241 Flowable.just(1)
242 .subscribeOn(Schedulers.computation())
243 .map(new Function<Integer, Integer>() {
244 @Override
245 public Integer apply(Integer v) throws Exception {
246
247 Flowable.just(1).delay(10, TimeUnit.SECONDS).toFuture().get();
248
249 return v;
250 }
251 })
252 .test()
253 .awaitDone(5, TimeUnit.SECONDS)
254 .assertFailure(IllegalStateException.class);
255 } finally {
256 RxJavaPlugins.reset();
257 }
258
259 }
260
261 @Test
262 public void failComputationObservableBlockingFirst() {
263
264 try {
265 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
266
267 Observable.just(1)
268 .subscribeOn(Schedulers.computation())
269 .map(new Function<Integer, Integer>() {
270 @Override
271 public Integer apply(Integer v) throws Exception {
272
273 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingFirst();
274
275 return v;
276 }
277 })
278 .test()
279 .awaitDone(5, TimeUnit.SECONDS)
280 .assertFailure(IllegalStateException.class);
281 } finally {
282 RxJavaPlugins.reset();
283 }
284
285 }
286
287 @Test
288 public void failComputationObservableBlockingLast() {
289
290 try {
291 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
292
293 Observable.just(1)
294 .subscribeOn(Schedulers.computation())
295 .map(new Function<Integer, Integer>() {
296 @Override
297 public Integer apply(Integer v) throws Exception {
298
299 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingLast();
300
301 return v;
302 }
303 })
304 .test()
305 .awaitDone(5, TimeUnit.SECONDS)
306 .assertFailure(IllegalStateException.class);
307 } finally {
308 RxJavaPlugins.reset();
309 }
310
311 }
312
313 @Test
314 public void failComputationObservableBlockingIterable() {
315
316 try {
317 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
318
319 Observable.just(1)
320 .subscribeOn(Schedulers.computation())
321 .map(new Function<Integer, Integer>() {
322 @Override
323 public Integer apply(Integer v) throws Exception {
324
325 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingIterable().iterator().next();
326
327 return v;
328 }
329 })
330 .test()
331 .awaitDone(5, TimeUnit.SECONDS)
332 .assertFailure(IllegalStateException.class);
333 } finally {
334 RxJavaPlugins.reset();
335 }
336
337 }
338
339 @Test
340 public void failComputationObservableBlockingSubscribe() {
341
342 try {
343 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
344
345 Observable.just(1)
346 .subscribeOn(Schedulers.computation())
347 .map(new Function<Integer, Integer>() {
348 @Override
349 public Integer apply(Integer v) throws Exception {
350
351 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingSubscribe();
352
353 return v;
354 }
355 })
356 .test()
357 .awaitDone(5, TimeUnit.SECONDS)
358 .assertFailure(IllegalStateException.class);
359 } finally {
360 RxJavaPlugins.reset();
361 }
362
363 }
364
365 @Test
366 public void failComputationObservableBlockingSingle() {
367
368 try {
369 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
370
371 Observable.just(1)
372 .subscribeOn(Schedulers.computation())
373 .map(new Function<Integer, Integer>() {
374 @Override
375 public Integer apply(Integer v) throws Exception {
376
377 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingSingle();
378
379 return v;
380 }
381 })
382 .test()
383 .awaitDone(5, TimeUnit.SECONDS)
384 .assertFailure(IllegalStateException.class);
385 } finally {
386 RxJavaPlugins.reset();
387 }
388
389 }
390
391 @Test
392 public void failComputationObservableBlockingForEach() {
393
394 try {
395 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
396
397 Observable.just(1)
398 .subscribeOn(Schedulers.computation())
399 .map(new Function<Integer, Integer>() {
400 @Override
401 public Integer apply(Integer v) throws Exception {
402
403 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingForEach(Functions.emptyConsumer());
404
405 return v;
406 }
407 })
408 .test()
409 .awaitDone(5, TimeUnit.SECONDS)
410 .assertFailure(IllegalStateException.class);
411 } finally {
412 RxJavaPlugins.reset();
413 }
414
415 }
416
417 @Test
418 public void failComputationObservableBlockingLatest() {
419
420 try {
421 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
422
423 Observable.just(1)
424 .subscribeOn(Schedulers.computation())
425 .map(new Function<Integer, Integer>() {
426 @Override
427 public Integer apply(Integer v) throws Exception {
428
429 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingLatest().iterator().hasNext();
430
431 return v;
432 }
433 })
434 .test()
435 .awaitDone(5, TimeUnit.SECONDS)
436 .assertFailure(IllegalStateException.class);
437 } finally {
438 RxJavaPlugins.reset();
439 }
440
441 }
442
443 @Test
444 public void failComputationObservableBlockingNext() {
445
446 try {
447 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
448
449 Observable.just(1)
450 .subscribeOn(Schedulers.computation())
451 .map(new Function<Integer, Integer>() {
452 @Override
453 public Integer apply(Integer v) throws Exception {
454
455 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingNext().iterator().hasNext();
456
457 return v;
458 }
459 })
460 .test()
461 .awaitDone(5, TimeUnit.SECONDS)
462 .assertFailure(IllegalStateException.class);
463 } finally {
464 RxJavaPlugins.reset();
465 }
466
467 }
468
469 @Test
470 public void failComputationObservableToFuture() {
471
472 try {
473 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
474
475 Observable.just(1)
476 .subscribeOn(Schedulers.computation())
477 .map(new Function<Integer, Integer>() {
478 @Override
479 public Integer apply(Integer v) throws Exception {
480
481 Observable.just(1).delay(10, TimeUnit.SECONDS).toFuture().get();
482
483 return v;
484 }
485 })
486 .test()
487 .awaitDone(5, TimeUnit.SECONDS)
488 .assertFailure(IllegalStateException.class);
489 } finally {
490 RxJavaPlugins.reset();
491 }
492
493 }
494
495 @Test
496 public void failSingleObservableBlockingFirst() {
497
498 try {
499 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
500
501 Observable.just(1)
502 .subscribeOn(Schedulers.single())
503 .map(new Function<Integer, Integer>() {
504 @Override
505 public Integer apply(Integer v) throws Exception {
506
507 Observable.just(1).delay(10, TimeUnit.SECONDS).blockingFirst();
508
509 return v;
510 }
511 })
512 .test()
513 .awaitDone(5, TimeUnit.SECONDS)
514 .assertFailure(IllegalStateException.class);
515 } finally {
516 RxJavaPlugins.reset();
517 }
518
519 }
520
521 @Test
522 public void failSingleSingleBlockingGet() {
523
524 try {
525 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
526
527 Single.just(1)
528 .subscribeOn(Schedulers.single())
529 .map(new Function<Integer, Integer>() {
530 @Override
531 public Integer apply(Integer v) throws Exception {
532
533 Single.just(1).delay(10, TimeUnit.SECONDS).blockingGet();
534
535 return v;
536 }
537 })
538 .test()
539 .awaitDone(5, TimeUnit.SECONDS)
540 .assertFailure(IllegalStateException.class);
541 } finally {
542 RxJavaPlugins.reset();
543 }
544
545 }
546
547 @Test
548 public void failSingleMaybeBlockingGet() {
549
550 try {
551 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
552
553 Maybe.just(1)
554 .subscribeOn(Schedulers.single())
555 .map(new Function<Integer, Integer>() {
556 @Override
557 public Integer apply(Integer v) throws Exception {
558
559 Maybe.just(1).delay(10, TimeUnit.SECONDS).blockingGet();
560
561 return v;
562 }
563 })
564 .test()
565 .awaitDone(5, TimeUnit.SECONDS)
566 .assertFailure(IllegalStateException.class);
567 } finally {
568 RxJavaPlugins.reset();
569 }
570
571 }
572
573 @Test
574 public void failSingleCompletableBlockingGet() {
575
576 try {
577 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
578
579 Completable.complete()
580 .subscribeOn(Schedulers.single())
581 .doOnComplete(new Action() {
582 @Override
583 public void run() throws Exception {
584 Completable.complete().delay(10, TimeUnit.SECONDS).blockingAwait();
585 }
586 })
587 .test()
588 .awaitDone(5, TimeUnit.SECONDS)
589 .assertFailure(IllegalStateException.class);
590 } finally {
591 RxJavaPlugins.reset();
592 }
593
594 }
595
596 @Test
597 public void failSingleCompletableBlockingAwait() {
598
599 try {
600 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
601
602 Completable.complete()
603 .subscribeOn(Schedulers.single())
604 .doOnComplete(new Action() {
605 @Override
606 public void run() throws Exception {
607 Completable.complete().delay(10, TimeUnit.SECONDS).blockingAwait();
608 }
609 })
610 .test()
611 .awaitDone(5, TimeUnit.SECONDS)
612 .assertFailure(IllegalStateException.class);
613 } finally {
614 RxJavaPlugins.reset();
615 }
616
617 }
618
619 @Test
620 public void dontfailIOObservableBlockingFirst() {
621
622 try {
623 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
624
625 Observable.just(1)
626 .subscribeOn(Schedulers.io())
627 .map(new Function<Integer, Integer>() {
628 @Override
629 public Integer apply(Integer v) throws Exception {
630 return Observable.just(2).delay(100, TimeUnit.MILLISECONDS).blockingFirst();
631 }
632 })
633 .test()
634 .awaitDone(5, TimeUnit.SECONDS)
635 .assertResult(2);
636 } finally {
637 RxJavaPlugins.reset();
638 }
639
640 }
641
642 @Test
643 public void failWithCustomHandler() {
644 try {
645 RxJavaPlugins.setOnBeforeBlocking(new BooleanSupplier() {
646 @Override
647 public boolean getAsBoolean() throws Exception {
648 return true;
649 }
650 });
651 RxJavaPlugins.setFailOnNonBlockingScheduler(true);
652
653 Flowable.just(1)
654 .map(new Function<Integer, Integer>() {
655 @Override
656 public Integer apply(Integer v) throws Exception {
657
658 Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingLast();
659
660 return v;
661 }
662 })
663 .test()
664 .awaitDone(5, TimeUnit.SECONDS)
665 .assertFailure(IllegalStateException.class);
666
667 } finally {
668 RxJavaPlugins.reset();
669 }
670
671 Flowable.just(1)
672 .map(new Function<Integer, Integer>() {
673 @Override
674 public Integer apply(Integer v) throws Exception {
675 return Flowable.just(2).delay(100, TimeUnit.MILLISECONDS).blockingLast();
676 }
677 })
678 .test()
679 .awaitDone(5, TimeUnit.SECONDS)
680 .assertResult(2);
681 }
682 }