我正在从该页面学习Reactive Programming
:tutorial。我想知道为什么在下面的示例中,当我期望4?
java.lang.AssertionError: expectation "expectNextCount(4)" failed (expected: count = 4; actual: counted = 1; signal: onComplete())
// TODO Create a StepVerifier that initially requests all values and expect 4 values to be received
StepVerifier requestAllExpectFour(Flux<User> flux) {
return StepVerifier.withVirtualTime(() -> Flux.just(flux))
.expectSubscription()
.thenRequest(4)
.expectNextCount(4)
.expectComplete();
}
我想知道我在这里是否遗漏了什么。
答案 0 :(得分:0)
试试这个
StepVerifier requestAllExpectFour(Flux<User> flux) {
return StepVerifier.withVirtualTime(() -> flux)
.expectSubscription()
.thenRequest(Long.MAX_VALUE)
.expectNextCount(4)
.expectComplete();
}
或者像这样
StepVerifier requestAllExpectFour(Flux<User> flux) {
return StepVerifier.create(flux)
.expectSubscription()
.thenRequest(Long.MAX_VALUE)
.expectNextCount(4)
.expectComplete();
}
答案 1 :(得分:0)
源代码 - &gt; https://github.com/reactor/lite-rx-api-hands-on
问题 - &gt; https://github.com/reactor/reactor-addons/issues/61
指南 - &gt; https://projectreactor.io/docs/core/release/reference/#_manipulating_time
以及下面的代码有效......
StepVerifier requestAllExpectFour(Flux<User> flux) {
List<User> users = new ArrayList<>(Arrays.asList(User.SKYLER, User.JESSE, User.WALTER, User.SAUL));
return StepVerifier.withVirtualTime(() ->
Flux.interval(Duration.ofMillis(100))
.zipWith(Flux.fromIterable(users), (i, user) -> user))
.expectSubscription()
.thenRequest(4)
.thenAwait(Duration.ofMillis(400))
.expectNextCount(4)
.expectComplete();
}
答案 2 :(得分:0)
我认为问题在于您正在通过使用Flux.just向焊剂提供单次发射的焊剂。您可以只应用助焊剂本身:
.withVirtualTime(() -> flux)
答案 3 :(得分:0)
我猜这个练习想要的是你使用构造函数使用请求初始化 StepVerifier,就像这样:
StepVerifier requestAllExpectFour(Flux<User> flux) {
return StepVerifier.create(flux, 4)
.expectNextCount(4)
.expectComplete();
}