Unit testing vertx单元测试未执行,超时异常
我有下面的简单重试类。此类将在给定的延迟(delay)下重试给定的承诺几次(retryCount),如果在重试次数内成功,则通过,否则失败Unit testing vertx单元测试未执行,超时异常,unit-testing,xunit,vert.x,Unit Testing,Xunit,Vert.x,我有下面的简单重试类。此类将在给定的延迟(delay)下重试给定的承诺几次(retryCount),如果在重试次数内成功,则通过,否则失败 public class SimpleRetry { private final Vertx vertx; public SimpleRetry(Vertx vertx) { this.vertx = vertx; } public <T> Promise<T> retryWithDelay(Pro
public class SimpleRetry {
private final Vertx vertx;
public SimpleRetry(Vertx vertx) {
this.vertx = vertx;
}
public <T> Promise<T> retryWithDelay(Promise<T> promise, int retryCount, int delay, TimeUnit unit) {
log.debug("Retrying operation with : " + retryCount + " retry count and delay of " + delay);
return execute(promise, retryCount, delay, unit);
}
private <T> Promise<T> execute(Promise<T> promise, int count, int delay, TimeUnit unit) {
Promise<T> newPromise = Promise.promise();
if (count > 0) {
promise.future().onComplete(handler -> {
if (handler.succeeded()) {
log.debug("Retry operation successful");
newPromise.complete(handler.result());
} else {
log.debug("Operation failed, hence retrying again..");
if (delay > 0) {
vertx.setTimer(unit.toMillis(delay), id -> execute(promise, count - 1, delay, unit));
} else {
execute(promise, count - 1, delay, unit);
}
}
});
} else {
log.debug("Retry count exceeded, hence failing the promise..");
newPromise.fail("Retry count exceeded!.");
}
return newPromise;
}
public类SimpleRetry{
私人最终Vertx Vertx;
公共SimpleRetry(Vertx Vertx){
this.vertx=vertx;
}
公共承诺延迟返回(承诺承诺、整数返回计数、整数延迟、时间单位){
log.debug(“使用“+retryCount+”重试计数和“+delay”延迟重试操作);
返回执行(承诺、返回计数、延迟、单位);
}
私有承诺执行(承诺承诺、整数计数、整数延迟、时间单位){
Promise newPromise=Promise.Promise();
如果(计数>0){
promise.future().onComplete(处理程序->{
if(handler.successed()){
log.debug(“重试操作成功”);
newPromise.complete(handler.result());
}否则{
调试(“操作失败,因此再次重试…”);
如果(延迟>0){
setTimer(unit.toMillis(delay),id->execute(promise,count-1,delay,unit));
}否则{
执行(承诺、计数-1、延迟、单位);
}
}
});
}否则{
调试(“超过重试次数,因此无法实现承诺…”);
newPromise.fail(“超过重试次数!”);
}
回报新的承诺;
}
编写下面的测试用例来测试它。但是它没有被执行。相反,它超时了
@RunWith(VertxUnitRunner.class)
public class SimpleRetryTests {
private SimpleRetry simpleRetry;
private Vertx vertx;
private static int count;
@Before
public void setUp(TestContext context){
vertx = Vertx.vertx();
simpleRetry = new SimpleRetry(vertx);
count = 0;
context.asyncAssertSuccess();
}
@Test
public void testSimpleRetryWhichPassesOnFirstTry(TestContext context){
final Async async = context.async();
simpleRetry.retryWithDelay(dummyPromise(1), 10, 10, TimeUnit.MILLISECONDS)
.future().onSuccess(res -> {
System.out.println("Promise completed");
context.asyncAssertSuccess();
async.complete();
}).onFailure(ex -> {
System.out.println("Promise failed : " + ex);
context.asyncAssertFailure();
async.complete();
});
}
//A dummy promise which only passes when called 5 times.
private Promise<Void> dummyPromise(int passCount){
Promise<Void> promise = Promise.promise();
vertx.setTimer(10, id->{
count++;
if(count == passCount) {
promise.complete();
} else {
promise.fail("Error!!");
}
});
return promise;
}
@After
public void tearDown(TestContext context){
simpleRetry = null;
vertx.close(context.asyncAssertSuccess());
}
}
@RunWith(VertxUnitRunner.class)
公共类SimpleRetryTests{
私人单纯形单纯形;
私有Vertx-Vertx;
私有静态整数计数;
@以前
公共无效设置(TestContext上下文){
vertx=vertx.vertx();
simpleRetry=新的simpleRetry(顶点x);
计数=0;
context.asyncAssertSuccess();
}
@试验
public void testSimpleRetryWhichPassesOnFirstTry(TestContext上下文){
final Async Async=context.Async();
simpleRetry.retryWithDelay(dummyPromise(1),10,10,TimeUnit.ms)
.future().onSuccess(res->{
System.out.println(“承诺完成”);
context.asyncAssertSuccess();
async.complete();
}).onFailure(ex->{
System.out.println(“承诺失败:+ex”);
context.asyncAssertFailure();
async.complete();
});
}
//一个只有被调用5次才能通过的虚假承诺。
私人承诺dummyPromise(整数密码){
允诺,允诺;
设置计时器(10,id->{
计数++;
if(count==passCount){
承诺。完成();
}否则{
承诺。失败(“错误!!”);
}
});
回报承诺;
}
@之后
公共void拆卸(TestContext上下文){
simpleRetry=null;
close(context.asyncAssertSuccess());
}
}
我做错了什么?提前谢谢。您误用了
上下文。asyncAssertSuccess()
和上下文。asyncAssertFailure()
它将停止您的代码,例如,您的安装函数将永远不会退出。取出它们,您的测试将运行完毕。非常感谢!