Spring 发生故障时重试逻辑-弹簧反应器
当Spring 发生故障时重试逻辑-弹簧反应器,spring,project-reactor,Spring,Project Reactor,当 public Mono<List<Transaction>> get(String id) { return class .get(id).log() .retryWhen(throwableFlux -> throwableFlux) .zipWith(Flux.range(min, max + 1), (error, retr
public Mono<List<Transaction>> get(String id) {
return class
.get(id).log()
.retryWhen(throwableFlux -> throwableFlux)
.zipWith(Flux.range(min, max + 1), (error, retry) -> new RetryException(error, retry))
.flatMap(retryException -> {
if(retryException.getRetries() == max + 1) {
throw Exceptions.propagate(retryException.getThrowable());
} else if (isClientException(retryException.getThrowable())){
return Flux.empty();
}
return Mono.delay(Duration.ofMinutes( new Double(multiplier * retryException.getRetries()).longValue()));
}));
}
您的意思是测试通过
retryWhen
应用的RetryHelper
当包含序列时,您当然可以使用StepVerifier
来测试这样一个retry,是的。您还可以在retryWhen
之前,使用与doOnSubscribe
耦合的AtomicLong
检查(重新)订阅的数量(这将有助于断言对正在重试的源所做的订阅的数量)
请注意,我们刚刚为
retryWhen
和repeatWhen
添加了这样一个生成器实用程序,但在中(目前在3.1.0.BUILD-SNAPSHOT中)这就是我测试此代码的方法
FirstStep.expectSubscription().expectNoEvent(java.time.Duration.ofMinutes(1)).expectNoEvent(Duration.ofMinutes(3)).verifyError()
我们本可以使用上面的等待(持续时间为1天),但
expectNoEvent的好处是保证不会发生任何事情
早该如此
哎呀。我只是扩展了代码。我可以用stepverifier.withVirtualTime测试我的代码。对于特定条件,我返回一个Flux.empty()-(无操作)。有没有关于如何测试的建议?测试一个空但有限的流量就像测试
verifyComplete()
一样简单,之前的预期不低于expectSubscription
FirstStep.verifyComplete()或type.expectSubscription().verifyComplete()在DefaultStepVerifierBuilder.pollTaskEventOrComplete.ah中,两者都陷入了无限for循环。糟糕,如果您使用虚拟时间,您可能仍然需要通过然后等待或expectNoEvent
(我想您在链中添加了延迟和重试,由于其他操作符,这些延迟和重试恰好以空结束?)对没错。因为如果不是因为某种类型的错误而使时钟提前,我就没有调用expectNoEvent。谢谢。在retryWhen
行中没有附加的右括号吗?您正在将zipWith.flatMap应用于throwableFlux
,对吗?
FirstStep.expectSubscription().expectNoEvent(java.time.Duration.ofMinutes(1)).expectNoEvent(Duration.ofMinutes(3)).verifyError()