Java Code Examples for io.vertx.junit5.VertxTestContext#causeOfFailure()

The following examples show how to use io.vertx.junit5.VertxTestContext#causeOfFailure() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Examples.java    From vertx-junit5 with Apache License 2.0 5 votes vote down vote up
@Test
void start_http_server() throws Throwable {
  VertxTestContext testContext = new VertxTestContext();

  vertx.createHttpServer()
    .requestHandler(req -> req.response().end())
    .listen(16969)
    .onComplete(testContext.succeedingThenComplete()); // <1>

  assertThat(testContext.awaitCompletion(5, TimeUnit.SECONDS)).isTrue(); // <2>
  if (testContext.failed()) {  // <3>
    throw testContext.causeOfFailure();
  }
}
 
Example 2
Source File: HealthServiceIntegrationTest.java    From cassandra-sidecar with Apache License 2.0 4 votes vote down vote up
@DisplayName("Down on startup, then comes up")
@Test
public void testDownHostTurnsOn() throws Throwable
{
    VertxTestContext testContext = new VertxTestContext();
    BoundCluster bc = injector.getInstance(BoundCluster.class);
    BoundNode node = bc.node(0);
    HealthCheck check = injector.getInstance(HealthCheck.class);
    HealthService service = injector.getInstance(HealthService.class);
    Server server = injector.getInstance(Server.class);

    try
    {
        WebClient client = WebClient.create(vertx);
        long start = System.currentTimeMillis();
        client.get(port, "localhost", "/api/v1/__health")
              .as(BodyCodec.string())
              .send(testContext.succeeding(response -> testContext.verify(() ->
              {
                  assertEquals(503, response.statusCode());

                  node.start();
                  while ((System.currentTimeMillis() - start) < (1000 * 60 * 2) && !check.get())
                      Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
                  service.refreshNow();
                  client.get(port, "localhost", "/api/v1/__health")
                        .as(BodyCodec.string())
                        .send(testContext.succeeding(upResponse -> testContext.verify(() ->
                        {
                            assertEquals(200, upResponse.statusCode());
                            testContext.completeNow();
                        })));
              })));
        assertTrue(testContext.awaitCompletion(125, TimeUnit.SECONDS));
        if (testContext.failed())
        {
            throw testContext.causeOfFailure();
        }
    }
    finally
    {
        service.stop();
        server.close();
    }
}