我如何进行单元测试 RetryWhen,
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()));
}));
}
如何使用 StepVerifier 来测试此方法?
实现重试逻辑的另一种方法,
throwableFlux.takeWhile(throwable -> !isClientException(throwable))
.flatMap(e -> {
if(count.get() >= max + 1) {
throw Exceptions.propagate(e);
}
LOG.info("Retrying in..");
return Mono.delay(Duration.ofMinutes(new Double(multiplier * count.getAndAdd(1)).longValue()));
});
最佳答案
您的意思是测试通过 retryWhen
应用的 RetryHelper
吗?
你当然可以使用StepVerifier
来测试这样的retryWhen包含序列,是的。您还可以在 retryWhen
之前使用与 doOnSubscribe
耦合的 AtomicLong
来检查(重新)订阅的数量(这将有助于断言对正在重试的源进行的订阅数量)。
请注意,我们刚刚为 retryWhen
和 repeatWhen
添加了这样的构建器实用程序,但在 reactor-extra
project 中(当前在 3.1.0.BUILD-SNAPSHOT 中)
关于spring - 失败时重试逻辑 - Spring Reactor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43883694/