io.reactivex.Flowable.blockingFirst()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(415)

本文整理了Java中io.reactivex.Flowable.blockingFirst()方法的一些代码示例,展示了Flowable.blockingFirst()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Flowable.blockingFirst()方法的具体详情如下:
包路径:io.reactivex.Flowable
类名称:Flowable
方法名:blockingFirst

Flowable.blockingFirst介绍

[英]Returns the first item emitted by this Flowable, or throws NoSuchElementException if it emits no items. Backpressure: The operator consumes the source Flowable in an unbounded manner (i.e., no backpressure applied to it). Scheduler: blockingFirst does not operate by default on a particular Scheduler. Error handling: If the source signals an error, the operator wraps a checked Exceptioninto RuntimeException and throws that. Otherwise, RuntimeExceptions and Errors are rethrown as they are.
[中]返回此Flowable发出的第一个项,如果它不发出任何项,则抛出NoTouchElementException。背压:操作员以无限制的方式消耗可流动源(即,不施加背压)。调度程序:默认情况下,blockingFirst不会在特定调度程序上运行。错误处理:如果源发出错误信号,操作员将选中的异常包装到RuntimeException中并抛出该异常。否则,运行时异常和错误将按原样重试。

代码示例

代码示例来源:origin: ReactiveX/RxJava

  1. @Override
  2. public Integer apply(Integer v) throws Exception {
  3. Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingFirst();
  4. return v;
  5. }
  6. })

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = TestException.class)
  2. public void firstOnError() {
  3. Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {
  4. @Override
  5. public void subscribe(Subscriber<? super Integer> s) {
  6. s.onSubscribe(new BooleanSubscription());
  7. s.onError(new TestException());
  8. }
  9. });
  10. source.blockingFirst();
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(expected = NoSuchElementException.class)
  2. public void blockingFirstEmpty() {
  3. Flowable.empty().blockingFirst();
  4. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void firstFgnoredCancelAndOnNext() {
  3. Flowable<Integer> source = Flowable.fromPublisher(new Publisher<Integer>() {
  4. @Override
  5. public void subscribe(Subscriber<? super Integer> s) {
  6. s.onSubscribe(new BooleanSubscription());
  7. s.onNext(1);
  8. s.onNext(2);
  9. }
  10. });
  11. assertEquals(1, source.blockingFirst().intValue());
  12. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void blockingFirstNormal() {
  3. assertEquals(1, Flowable.just(1, 2).blockingFirst(3).intValue());
  4. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testListWithBlockingFirstFlowable() {
  3. Flowable<String> f = Flowable.fromIterable(Arrays.asList("one", "two", "three"));
  4. List<String> actual = f.toList().toFlowable().blockingFirst();
  5. Assert.assertEquals(Arrays.asList("one", "two", "three"), actual);
  6. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void timerDelayZero() {
  3. List<Throwable> errors = TestHelper.trackPluginErrors();
  4. try {
  5. for (int i = 0; i < 1000; i++) {
  6. Flowable.timer(0, TimeUnit.MILLISECONDS).blockingFirst();
  7. }
  8. assertTrue(errors.toString(), errors.isEmpty());
  9. } finally {
  10. RxJavaPlugins.reset();
  11. }
  12. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testBackpressureWithNoInitialValueObservable() throws InterruptedException {
  3. Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6);
  4. Flowable<Integer> reduced = source.reduce(sum).toFlowable();
  5. Integer r = reduced.blockingFirst();
  6. assertEquals(21, r.intValue());
  7. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void blockingFirstDefault() {
  3. assertEquals(1, Flowable.<Integer>empty()
  4. .subscribeOn(Schedulers.computation()).blockingFirst(1).intValue());
  5. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void blockingFirst() {
  3. assertEquals(1, Flowable.range(1, 10)
  4. .subscribeOn(Schedulers.computation()).blockingFirst().intValue());
  5. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testBackpressureWithInitialValueFlowable() throws InterruptedException {
  3. Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6);
  4. Flowable<Integer> reduced = source.reduce(0, sum).toFlowable();
  5. Integer r = reduced.blockingFirst();
  6. assertEquals(21, r.intValue());
  7. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testWithFollowingFirstFlowable() {
  3. Flowable<Integer> f = Flowable.just(1, 3, 2, 5, 4);
  4. assertEquals(Arrays.asList(1, 2, 3, 4, 5), f.toSortedList().toFlowable().blockingFirst());
  5. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testElementAtWithIndexOutOfBoundsFlowable() {
  3. assertEquals(-100, Flowable.fromArray(1, 2).elementAt(2).toFlowable().blockingFirst(-100).intValue());
  4. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(timeout = 30000)
  2. public void testIssue1527Flowable() throws InterruptedException {
  3. //https://github.com/ReactiveX/RxJava/pull/1527
  4. Flowable<Integer> source = Flowable.just(1, 2, 3, 4, 5, 6);
  5. Flowable<Integer> reduced = source.reduce(new BiFunction<Integer, Integer, Integer>() {
  6. @Override
  7. public Integer apply(Integer i1, Integer i2) {
  8. return i1 + i2;
  9. }
  10. }).toFlowable();
  11. Integer r = reduced.blockingFirst();
  12. assertEquals(21, r.intValue());
  13. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(timeout = 5000)
  2. public void testIssue1935NoUnsubscribeDownstreamFlowable() {
  3. Flowable<Integer> source = Flowable.just(1).isEmpty()
  4. .flatMapPublisher(new Function<Boolean, Publisher<Integer>>() {
  5. @Override
  6. public Publisher<Integer> apply(Boolean t1) {
  7. return Flowable.just(2).delay(500, TimeUnit.MILLISECONDS);
  8. }
  9. });
  10. assertEquals((Object)2, source.blockingFirst());
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(timeout = 5000)
  2. public void testIssue1935NoUnsubscribeDownstream() {
  3. Flowable<Integer> source = Flowable.just(1).isEmpty()
  4. .flatMapPublisher(new Function<Boolean, Publisher<Integer>>() {
  5. @Override
  6. public Publisher<Integer> apply(Boolean t1) {
  7. return Flowable.just(2).delay(500, TimeUnit.MILLISECONDS);
  8. }
  9. });
  10. assertEquals((Object)2, source.blockingFirst());
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testWithFollowingFirstFlowable() {
  3. Flowable<Integer> f = Flowable.fromArray(1, 3, 5, 6);
  4. Flowable<Boolean> anyEven = f.any(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer i) {
  7. return i % 2 == 0;
  8. }
  9. }).toFlowable();
  10. assertTrue(anyEven.blockingFirst());
  11. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testFollowingFirstFlowable() {
  3. Flowable<Integer> f = Flowable.fromArray(1, 3, 5, 6);
  4. Flowable<Boolean> allOdd = f.all(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer i) {
  7. return i % 2 == 1;
  8. }
  9. })
  10. .toFlowable()
  11. ;
  12. assertFalse(allOdd.blockingFirst());
  13. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test
  2. public void testErrorThrownIssue1685() {
  3. List<Throwable> errors = TestHelper.trackPluginErrors();
  4. try {
  5. FlowableProcessor<Object> processor = ReplayProcessor.create();
  6. Flowable.error(new RuntimeException("oops"))
  7. .materialize()
  8. .delay(1, TimeUnit.SECONDS)
  9. .dematerialize(Functions.<Notification<Object>>identity())
  10. .subscribe(processor);
  11. processor.subscribe();
  12. processor.materialize().blockingFirst();
  13. System.out.println("Done");
  14. TestHelper.assertError(errors, 0, OnErrorNotImplementedException.class);
  15. } finally {
  16. RxJavaPlugins.reset();
  17. }
  18. }

代码示例来源:origin: ReactiveX/RxJava

  1. @Test(timeout = 5000)
  2. public void testIssue1935NoUnsubscribeDownstream() {
  3. Flowable<Integer> source = Flowable.just(1)
  4. .all(new Predicate<Integer>() {
  5. @Override
  6. public boolean test(Integer t1) {
  7. return false;
  8. }
  9. })
  10. .flatMapPublisher(new Function<Boolean, Publisher<Integer>>() {
  11. @Override
  12. public Publisher<Integer> apply(Boolean t1) {
  13. return Flowable.just(2).delay(500, TimeUnit.MILLISECONDS);
  14. }
  15. });
  16. assertEquals((Object)2, source.blockingFirst());
  17. }

相关文章

Flowable类方法