Vertx Junit5并发超时异常

时间:2019-12-02 06:33:21

标签: unit-testing asynchronous vert.x junit5

我在运行此代码时遇到异常,正在用beforeEach和test方法关闭testContext。

  

测试执行超时。确保您的异步代码包括对VertxTestContext#completeNow(),VertxTestContext#failNow()或Checkpoint#flag()的调用   java.util.concurrent.TimeoutException:测试执行超时。确保您的异步代码包括对VertxTestContext#completeNow(),VertxTestContext#failNow()或Checkpoint#flag()的调用       在io.vertx.junit5.VertxExtension.joinActiveTestContexts(VertxExtension.java:230)

@DisplayName("Test Case Workflow")
@ExtendWith(VertxExtension.class)
public class OrchestrationDBVerticleTest {
    // tag::prepare[]
    private Vertx vertx;
    private OrchestrationDBService service;
    public static final String CONFIG_JDBC_URL = "test.jdbc.url";
    public static final String CONFIG_JDBC_DRIVER_CLASS = "test.jdbc.driver_class";
    public static final String CONFIG_JDBC_MAX_POOL_SIZE = "test.jdbc.max_pool_size";

    @BeforeEach
    public void prepare(VertxTestContext testContext) throws InterruptedException {
        vertx = Vertx.vertx();
        JsonObject config = new JsonObject()
                .put("url", vertx.getOrCreateContext().config().getString(CONFIG_JDBC_URL, "jdbc:hsqldb:mem:testdb"))
                .put("driver_class", vertx.getOrCreateContext().config().getString(CONFIG_JDBC_DRIVER_CLASS, "org.hsqldb.jdbcDriver"))
                .put("max_pool_size", vertx.getOrCreateContext().config().getInteger(CONFIG_JDBC_MAX_POOL_SIZE, 30));
        JsonObject dbConfig = new JsonObject().put("jdbcConfig", config);

        vertx.deployVerticle(new OrchestrationDBVerticle(), new DeploymentOptions().setConfig(dbConfig),
                testContext.succeeding(id -> {
                    service = OrchestrationDBService.createProxy(vertx, OrchestrationDBVerticle.CONFIG_ORCHESTRATION_DB_QUEUE);
                    testContext.completeNow();
                }));
    }
    // end::prepare[]

    // tag::finish[]
    @AfterEach
    public void finish(VertxTestContext testContext) {
        System.out.println("after");
        vertx.close();
    }
    // end::finish[]


    // tag::crud[]
    @Test
    public void crud_operations(VertxTestContext testContext) {
//        Checkpoint callProxy = testContext.checkpoint();
        JsonObject jobInput = (new JsonObject()).put("requestInput", new JsonObject().put("test", "test"))
                .put("workflow", "WorkFlowHandler");
        service.saveJobDetails(jobInput, testContext.succeeding(response -> {
            System.out.println("Service Response : " + response);
            Assertions.assertThat(response.toString().contains("IN_QUEUE"));
            testContext.completeNow();
//            callProxy.flag();
        }));
    }
    // end::crud[]
}

编辑:- 我没有在finish方法中完成testContext。

@AfterEach
    public void finish(VertxTestContext testContext) {
        System.out.println("after");
        vertx.close(testContext.succeeding(response -> {
            testContext.completeNow();
        }));
    }

但是即使在那之后,如果我提供了错误的输入,我的断言条件也总是正确的。

1 个答案:

答案 0 :(得分:0)

已解决。

我没有在finish方法中完成testContext。

@AfterEach
    public void finish(VertxTestContext testContext) {
        System.out.println("after");
        vertx.close(testContext.succeeding(response -> {
            testContext.completeNow();
        }));
    }

更新:-验证testContext.verify()中的断言

@Test
    @DisplayName("? Return Exact request what we stored in DB")
    public void crud_operations(VertxTestContext testContext) {
        JsonObject jobInput = (new JsonObject()).put("requestInput", new JsonObject().put("test", "test"))
                .put("workflow", "CaseWorkFlowHandler");
        service.saveJobDetails(jobInput, testContext.succeeding(response -> {
            testContext.verify(() -> {
                Assertions.assertThat(response.getJsonArray("rows").getJsonObject(0).getString("REQUEST_INPUT")).isEqualTo("{\"test\":\"test\"}");
            });
            testContext.completeNow();
        }));
    }