我如何进行单元测试?
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()));
});
您的意思是测试通过retry时
应用的RetryHelper
吗?
您当然可以使用StepVerifier
来测试这样的retry当包含序列时,是的。您还可以通过在retry当
之前使用与doOnSubscribe
耦合的原子龙
来检查(重新)订阅的数量(它将有助于断言对正在重试的源进行的订阅的数量)。
请注意,我们刚刚为retry时
和重复时
添加了这样一个构建器实用程序,但是在reacter-extra
项目中(目前在3.1.0.BUILD-SNAPSHOT中)
这就是我能够测试此代码的方式。
FirstStep.expectSubscription().expectNoEvent(java.time.Duration.ofMinutes(1)).expectNoEvent(Duration.ofMinutes(3)).verifyError()
我们本可以使用上面的thenAwait(Duration. ofDay(1)),但是预期NoEvent的好处是保证没有任何事情比它应该发生的更早发生。
http://projectreactor.io/docs/core/snapshot/reference/docs/index.html#error.handling