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

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

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

Flowable.blockingSingle介绍

[英]If this Flowable completes after emitting a single item, return that item, otherwise throw a NoSuchElementException.

Backpressure: The operator consumes the source Flowable in an unbounded manner (i.e., no backpressure applied to it). Scheduler: blockingSingle 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。
背压:操作员以无限制的方式消耗可流动源(即,不施加背压)。调度程序:blockingSingle默认情况下不会在特定调度程序上运行。错误处理:如果源发出错误信号,操作员将选中的异常包装到RuntimeException中并抛出该异常。否则,运行时异常和错误将按原样重试。

代码示例

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

@Override
  public void run() {
    try {
      // a timeout exception will happen if we don't get a terminal state
      String v = processor.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
      value.set(v);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

@Override
  public void run() {
    try {
      // a timeout exception will happen if we don't get a terminal state
      String v = processor.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
      value.set(v);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

@Override
  public void run() {
    try {
      // a timeout exception will happen if we don't get a terminal state
      String v = processor.timeout(2000, TimeUnit.MILLISECONDS).blockingSingle();
      value.set(v);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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

@Override
  public Integer apply(Integer v) throws Exception {
    Flowable.just(1).delay(10, TimeUnit.SECONDS).blockingSingle();
    return v;
  }
})

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

/**
 * We expect IllegalStateException to pass thru map.
 */
@Test(expected = IllegalStateException.class)
public void testErrorPassesThruMap2() {
  Flowable.error(new IllegalStateException()).map(new Function<Object, Object>() {
    @Override
    public Object apply(Object i) {
      return i;
    }
  }).blockingSingle();
}

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

@Test
public void testSingleDoesNotRequestMoreThanItNeedsToEmitItem() {
  final AtomicLong request = new AtomicLong();
  Flowable.just(1).doOnRequest(new LongConsumer() {
    @Override
    public void accept(long n) {
      request.addAndGet(n);
    }
  }).blockingSingle();
  // FIXME single now triggers fast-path
  assertEquals(Long.MAX_VALUE, request.get());
}

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

@Test(expected = IllegalArgumentException.class)
public void testTakeWithError() {
  Flowable.fromIterable(Arrays.asList(1, 2, 3)).take(1)
  .map(new Function<Integer, Integer>() {
    @Override
    public Integer apply(Integer t1) {
      throw new IllegalArgumentException("some error");
    }
  }).blockingSingle();
}

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

@Test
public void testSynchronousNext() {
  assertEquals(1, BehaviorProcessor.createDefault(1).take(1).blockingSingle().intValue());
  assertEquals(2, BehaviorProcessor.createDefault(2).blockingIterable().iterator().next().intValue());
  assertEquals(3, BehaviorProcessor.createDefault(3).blockingNext().iterator().next().intValue());
}

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

@Test
public void testDoOnEach() {
  final AtomicReference<String> r = new AtomicReference<String>();
  String output = Flowable.just("one").doOnNext(new Consumer<String>() {
    @Override
    public void accept(String v) {
      r.set(v);
    }
  }).blockingSingle();
  assertEquals("one", output);
  assertEquals("one", r.get());
}

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

@Test
public void testDoOnCompleted() {
  final AtomicBoolean r = new AtomicBoolean();
  String output = Flowable.just("one").doOnComplete(new Action() {
    @Override
    public void run() {
      r.set(true);
    }
  }).blockingSingle();
  assertEquals("one", output);
  assertTrue(r.get());
}

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

@Test
public void testSwitchWhenEmpty() throws Exception {
  final Flowable<Integer> flowable = Flowable.<Integer>empty()
      .switchIfEmpty(Flowable.fromIterable(Arrays.asList(42)));
  assertEquals(42, flowable.blockingSingle().intValue());
}

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

@Test
public void testSynchronousNext() {
  assertEquals(1, BehaviorProcessor.createDefault(1).take(1).blockingSingle().intValue());
  assertEquals(2, BehaviorProcessor.createDefault(2).blockingIterable().iterator().next().intValue());
  assertEquals(3, BehaviorProcessor.createDefault(3).blockingNext().iterator().next().intValue());
}

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

@Test
public void fromIterable() {
  ArrayList<String> items = new ArrayList<String>();
  items.add("one");
  items.add("two");
  items.add("three");
  assertEquals((Long)3L, Flowable.fromIterable(items).count().blockingGet());
  assertEquals("two", Flowable.fromIterable(items).skip(1).take(1).blockingSingle());
  assertEquals("three", Flowable.fromIterable(items).takeLast(1).blockingSingle());
}

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

@Test
public void testElementAtFlowable() {
  assertEquals(2, Flowable.fromArray(1, 2).elementAt(1).toFlowable().blockingSingle()
      .intValue());
}

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

@Test
public void reduceInts() {
  Flowable<Integer> f = Flowable.just(1, 2, 3);
  int value = f.reduce(new BiFunction<Integer, Integer, Integer>() {
    @Override
    public Integer apply(Integer t1, Integer t2) {
      return t1 + t2;
    }
  }).toFlowable().blockingSingle();
  assertEquals(6, value);
}

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

@Test
public void reduceIntsFlowable() {
  Flowable<Integer> f = Flowable.just(1, 2, 3);
  int value = f.reduce(new BiFunction<Integer, Integer, Integer>() {
    @Override
    public Integer apply(Integer t1, Integer t2) {
      return t1 + t2;
    }
  }).toFlowable().blockingSingle();
  assertEquals(6, value);
}

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

@Test
public void fromArityArgs3() {
  Flowable<String> items = Flowable.just("one", "two", "three");
  assertEquals((Long)3L, items.count().blockingGet());
  assertEquals("two", items.skip(1).take(1).blockingSingle());
  assertEquals("three", items.takeLast(1).blockingSingle());
}

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

@Test
public void testConcatOuterBackpressure() {
  assertEquals(1,
      (int) Flowable.<Integer> empty()
          .concatWith(Flowable.just(1))
          .take(1)
          .blockingSingle());
}

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

@Test
public void fromArray() {
  String[] items = new String[] { "one", "two", "three" };
  assertEquals((Long)3L, Flowable.fromArray(items).count().blockingGet());
  assertEquals("two", Flowable.fromArray(items).skip(1).take(1).blockingSingle());
  assertEquals("three", Flowable.fromArray(items).takeLast(1).blockingSingle());
}

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

@Test
public void fromArityArgs1() {
  Flowable<String> items = Flowable.just("one");
  assertEquals((Long)1L, items.count().blockingGet());
  assertEquals("one", items.takeLast(1).blockingSingle());
}

相关文章

Flowable类方法