Vertx Junit5并发超时异常
Vertx Junit5 Concurrent timeout exception
我在 运行 这段代码时遇到异常,我正在关闭 beforeEach 中的 testContext 和测试方法。
The test execution timed out. Make sure your asynchronous code includes calls to either VertxTestContext#completeNow(), VertxTestContext#failNow() or Checkpoint#flag()
java.util.concurrent.TimeoutException: The test execution timed out. Make sure your asynchronous code includes calls to either VertxTestContext#completeNow(), VertxTestContext#failNow() or Checkpoint#flag()
at 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[]
}
编辑:-
我没有在完成方法中完成 testContext。
@AfterEach
public void finish(VertxTestContext testContext) {
System.out.println("after");
vertx.close(testContext.succeeding(response -> {
testContext.completeNow();
}));
}
但即使在那之后,如果我提供了错误的输入,我的断言条件也始终为真。
已解决。
我没有在完成方法中完成 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();
}));
}
我在 运行 这段代码时遇到异常,我正在关闭 beforeEach 中的 testContext 和测试方法。
The test execution timed out. Make sure your asynchronous code includes calls to either VertxTestContext#completeNow(), VertxTestContext#failNow() or Checkpoint#flag() java.util.concurrent.TimeoutException: The test execution timed out. Make sure your asynchronous code includes calls to either VertxTestContext#completeNow(), VertxTestContext#failNow() or Checkpoint#flag() at 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[]
}
编辑:- 我没有在完成方法中完成 testContext。
@AfterEach
public void finish(VertxTestContext testContext) {
System.out.println("after");
vertx.close(testContext.succeeding(response -> {
testContext.completeNow();
}));
}
但即使在那之后,如果我提供了错误的输入,我的断言条件也始终为真。
已解决。
我没有在完成方法中完成 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();
}));
}