繁体   English   中英

如何进行测试等待Vert.x Verticle部署完成

[英]How can I make tests wait for Vert.x Verticle deployment to be completed

我正在为我的Vert.x应用程序实现测试,但我在使Vert.x以优雅的方式等待Verticle的部署时遇到问题。 这是我的@BeforeClass方法:

    @BeforeClass
public static void before(TestContext context) 
{
    vertx = Vertx.vertx();
    DeploymentOptions options = new DeploymentOptions();

    byte[] encoded;
    JsonObject config;

    try {
        encoded = Files.readAllBytes(Paths.get("src/main/resources/config.json"));
        config = new JsonObject(new String(encoded, Charset.defaultCharset()));

        options.setConfig(config);
        jdbc = JDBCClient.createShared(vertx, config , "PostgreSQL");

        deployVerticle((result) -> loadTestData((result), jdbc), options);

        while (true)
            {
            if (vertx.deploymentIDs().size() > 0)
                break;
            }
    } catch 
    (IOException e) 
    {
        e.printStackTrace();
    }
}

此外,这是deployVerticle和loadTestData方法的实现:

private static void deployVerticle(Handler<AsyncResult<Void>> next, DeploymentOptions options) {


    vertx.deployVerticle(PostgreSQLClientVerticle.class.getName(), options, deployResult -> 
    {
        if (deployResult.succeeded())
        next.handle(Future.succeededFuture());
    });
}
private static void loadTestData(AsyncResult<Void> previousOperation, JDBCClient jdbc)
{
    if (previousOperation.succeeded())
    {
        jdbc.getConnection(connection -> {
            if (connection.succeeded())
            {
                connection.result().query(deleteTestDataGeneration, queryResult -> 
                {
                    connection.result().close();
                });
            }
        });
    }
}

正如您所看到的,现在我在before方法上有一段while (true)来保持进程并确保实际部署了Verticle。 否则,当测试开始运行时,Verticle尚未完全部署,我得到一个NullPointerException试图访问资源。

我尝试了许多不同的方法,比如使用CompositeFuture或使用Future.compose方法使“before tasks”顺序完成并使程序保持完成。 我在完成这些任务时实现了这些任务,但在完成之前一直没有完成。

其中一个问题是,我想,事实deployVerticle方法返回AsyncResultsucceeded == true的每一步之后的“部署过程”是做,而不是当Verticle完全由。

这意味着该过程在一切实际结束之前获得了成功的结果......但这只是一个疯狂的猜测。

结论:我想找到一种方法,在继续执行测试之前等待Verticle完全部署,而不必执行我当前在那里的while (true)循环。

你缺少的是Async async = context.async(); 由此,unittest保留在方法中,直到它没有设置完成。 然后,您可以将您的异步代码编排为:

  • 首先部署Verticle
  • 然后执行loadtestGeneration
  • 将async设置为完成,以便其他unittest方法已经可以访问您的Verticle而不使用nullpointerexception

我也做了一些清理,检查出来:

课前

  @BeforeClass
  public static void before2(TestContext context){
    Async async = context.async();
    vertx = Vertx.vertx();
    DeploymentOptions options = new DeploymentOptions();
    byte[] encoded;
    JsonObject config;

    try {
      encoded = Files.readAllBytes(Paths.get("src/main/resources/config.json"));
      config = new JsonObject(new String(encoded, Charset.defaultCharset()));
      options.setConfig(config);
      jdbc = JDBCClient.createShared(vertx, config , "PostgreSQL");

      deployVerticle2(options)
        .compose(c -> loadTestData2(jdbc))
        .setHandler(h -> {
          if(h.succeeded()){
            async.complete();
          }else{
            context.fail(h.cause());
          }
        });

    } catch (IOException e){
      context.fail(e);
    }
  }

DeployVerticle

  private static Future<Void> deployVerticle2(DeploymentOptions options) {
    Future<Void> future = Future.future();

    vertx.deployVerticle(PostgreSQLClientVerticle.class.getName(), options, deployResult -> {
      if (deployResult.failed()){
        future.fail(deployResult.cause());
      }else {
        future.complete();
      }
    });

    return future;
  }

LoadTestData

  private static Future<Void> loadTestData2(JDBCClient jdbc){
    Future<Void> future = Future.future();

    jdbc.getConnection(connection -> {
      if (connection.succeeded()) {
        connection.result().query(deleteTestDataGeneration, queryResult -> {
          if(queryResult.failed()){
            connection.result().close();
            future.fail(queryResult.cause());
          }else{
            connection.result().close();
            future.complete();
          }
        });
      } else {
        future.fail(connection.cause());
      }
    });

    return future;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM