Java Code Examples for io.vertx.ext.unit.Async#await()

The following examples show how to use io.vertx.ext.unit.Async#await() . 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: DeploymentManagerTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoPortsAvailable(TestContext context) {
  JsonObject conf = new JsonObject();
  conf.put("port_start", "9231");
  conf.put("port_end", "9231");
  DeploymentManagerTest.this.createDeploymentManager(context, new EnvStoreNull(), conf);
  Async async = context.async();
  LaunchDescriptor descriptor = new LaunchDescriptor();
  descriptor.setExec(
          "java -Dport=%p -jar "
          + "../okapi-test-module/target/unknown.jar");
  DeploymentDescriptor dd = new DeploymentDescriptor("2", "sid", descriptor);
  dm.deploy(dd, res -> {
    context.assertFalse(res.succeeded());
    context.assertEquals("all ports in use", res.cause().getMessage());
    async.complete();
  });
  async.await();
}
 
Example 2
Source File: FreeMarkerTemplateTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateHandlerNoExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));

  engine.render(context, "somedir/test-freemarker-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template2.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 3
Source File: FreeMarkerTemplateTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateHandlerOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template2.ftl"));

  engine.render(context, "somedir/test-freemarker-template2.ftl", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template2.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 4
Source File: ConnectionTestBase.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseWithErrorInProgress(TestContext ctx) {
  Async async = ctx.async(2);
  connect(ctx.asyncAssertSuccess(conn -> {
    conn.query("SELECT whatever from DOES_NOT_EXIST").execute(ctx.asyncAssertFailure(err -> {
      ctx.assertEquals(2, async.count());
      async.countDown();
    }));
    conn.closeHandler(v -> {
      ctx.assertEquals(1, async.count());
      async.countDown();
    });
    conn.close();
  }));
  async.await();
}
 
Example 5
Source File: PebbleTemplateTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = PebbleTemplateEngine.create(vertx, "beb");

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/test-pebble-template2.peb"));

  engine.render(context, "somedir/test-pebble-template2", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Cheerio badger and foxRequest path is /test-pebble-template2.peb", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 6
Source File: ConnectionTestBase.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseWithQueryInProgress(TestContext ctx) {
  Async async = ctx.async(2);
  connect(ctx.asyncAssertSuccess(conn -> {
    conn.query("SELECT id, message from immutable").execute(ctx.asyncAssertSuccess(result -> {
      ctx.assertEquals(2, async.count());
      ctx.assertEquals(12, result.size());
      async.countDown();
    }));
    conn.closeHandler(v -> {
      ctx.assertEquals(1, async.count());
      async.countDown();
    });
    conn.close();
  }));
  async.await();
}
 
Example 7
Source File: CircuitBreakerSecuredJdbcClientTest.java    From prebid-server-java with Apache License 2.0 6 votes vote down vote up
@Test
public void executeQueryShouldReturnExceptionIfCircuitIsHalfOpenedAndQueryFails(TestContext context) {
    // given
    givenExecuteQueryReturning(singletonList(
            Future.failedFuture(new RuntimeException("exception1"))));

    // when
    final Async async = context.async();
    jdbcClient.executeQuery("query", emptyList(), identity(), timeout) // 1 call
            .recover(ignored -> jdbcClient.executeQuery("query", emptyList(), identity(), timeout)) // 2 call
            .setHandler(ignored -> vertx.setTimer(201L, id -> async.complete()));
    async.await();

    final Future<?> future = jdbcClient.executeQuery("query", emptyList(), identity(), timeout); // 3 call

    // then
    future.setHandler(context.asyncAssertFailure(exception -> {
        assertThat(exception).isInstanceOf(RuntimeException.class).hasMessage("exception1");

        verify(wrappedJdbcClient, times(2))
                .executeQuery(any(), any(), any(), any()); // invoked only on 1 & 3 calls
    }));
}
 
Example 8
Source File: HandlebarsTemplateTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateOnClasspath(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  engine.render(context, "somedir/test-handlebars-template2.hbs", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 9
Source File: FreeMarkerTemplateTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateHandlerOnFileSystem(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox");

  context.put("context", new JsonObject().put("path", "/test-freemarker-template3.ftl"));

  engine.render(context, "src/test/filesystemtemplates/test-freemarker-template3.ftl", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Hello badger and fox\nRequest path is /test-freemarker-template3.ftl\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 10
Source File: MqttServerEndpointStatusTest.java    From vertx-mqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void disconnectedByClient(TestContext context) {

  Async async = context.async();

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
    client.connect();
    client.disconnect();

    // give more time to the MqttClient to update its connection state
    this.vertx.setTimer(1000, t1 -> {
      async.complete();
    });

    async.await();

    context.assertTrue(!client.isConnected() && !this.endpoint.isConnected());

  } catch (MqttException e) {
    context.assertTrue(false);
    e.printStackTrace();
  }
}
 
Example 11
Source File: RockerTemplateEngineTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateHandlerChangeExtension(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = RockerTemplateEngine.create("rocker.raw");

  final JsonObject context = new JsonObject()
    .put("foo", "badger")
    .put("bar", "fox")
    .put("context", new JsonObject().put("path", "/TestRockerTemplate3"));

  engine.render(context, "somedir/TestRockerTemplate3", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("\nCheerio badger and fox\nRequest path is /TestRockerTemplate3\n", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 12
Source File: PebbleTemplateTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void acceptLanguageHeaderShouldBeUsedWhenSet(TestContext should) {
  final Async test = should.async();

  Locale.setDefault(Locale.US);
  final TemplateEngine engine = PebbleTemplateEngine.create(vertx);

  engine.render(new JsonObject().put("lang", "de-DE"), "src/test/filesystemtemplates/test-pebble-template-i18n.peb", render2 -> {
    should.assertTrue(render2.succeeded());
    should.assertEquals("Hallo", render2.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 13
Source File: InjectionTest.java    From spring-vertx-ext with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy and undeploy a verticle
 * @throws IOException
 */
@Test
public void testStaticDeploymentComponentScan(TestContext tc) throws IOException {
    Async async = tc.async();
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(SpringInjectionComponentScanTestVerticleStatic.class.getCanonicalName(), ch -> {
        tc.assertTrue(ch.succeeded());
        tc.assertNotNull(ch.result());
        vertx.undeploy(ch.result(), chu -> {
            tc.assertTrue(chu.succeeded());
            async.complete();
        });
    });
    async.await();
}
 
Example 14
Source File: InjectionTest.java    From spring-vertx-ext with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy and undeploy a verticle
 * @throws IOException
 */
@Test
public void testStaticDeployment(TestContext tc) throws IOException {
    Async async = tc.async();
    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(SpringInjectionTestVerticleStatic.class.getCanonicalName(), ch -> {
        tc.assertTrue(ch.succeeded());
        tc.assertNotNull(ch.result());
        vertx.undeploy(ch.result(), chu -> {
            tc.assertTrue(chu.succeeded());
            async.complete();
        });
    });
    async.await();
}
 
Example 15
Source File: PostgresClientITBase.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
/**
 * Execute all statements, ignore any failure.
 */
public static void executeIgnore(TestContext context, String ... sqlStatements) {
  for (String sql : sqlStatements) {
    Async async = context.async();
    postgresClient().execute(sql, reply -> {
      async.complete();
    });
    async.await();
  }
}
 
Example 16
Source File: HandlebarsTemplateTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testTemplateJsonObjectResolver(TestContext should) {
  final Async test = should.async();
  TemplateEngine engine = HandlebarsTemplateEngine.create(vertx);

  JsonObject json = new JsonObject();
  json.put("bar", new JsonObject().put("one", "badger").put("two", "fox"));

  engine.render(new JsonObject().put("foo", json), "src/test/filesystemtemplates/test-handlebars-template4.hbs", render -> {
    should.assertTrue(render.succeeded());
    should.assertEquals("Goodbye badger and fox", render.result().toString());
    test.complete();
  });
  test.await();
}
 
Example 17
Source File: MemMapResultRangeJsonVerticleTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)

    public void testArrayResultRangeJson(TestContext context) {
        HttpClient httpClient = vertx.createHttpClient();
        JsonArray jsonArray = new JsonArray();
        jsonArray.add(0);
        Async async2 = context.async();
        HttpClientRequest req = httpClient.post(port, "localhost", "/array/range/0/2/json")
                .handler(handler -> {
                    handler.bodyHandler(body -> {
                        byte[] npyArray = body.getBytes();
                        System.out.println("Found numpy array bytes with length " + npyArray.length);
                        System.out.println("Contents: " + new String(npyArray));
                        JsonArray jsonArray1 = new JsonArray(new String(npyArray));
                        double[] arrContent = new double[jsonArray1.size()];
                        for (int i = 0; i < jsonArray1.size(); i++) {
                            arrContent[i] = jsonArray1.getDouble(i);
                        }

                        INDArray arrFromNumpy = Nd4j.create(arrContent);
                        context.assertEquals(Nd4j.create(new double[]{1, 2}), arrFromNumpy);
                        System.out.println(arrFromNumpy);
                        async2.complete();
                    });

                    handler.exceptionHandler(exception -> {
                        context.fail(exception.getCause());
                        async2.complete();
                    });

                }).putHeader("Content-Type", "application/json")
                .putHeader("Content-Length", String.valueOf(0))
                .write("");

        async2.await();
    }
 
Example 18
Source File: PostgresClientTransactionsIT.java    From raml-module-builder with Apache License 2.0 4 votes vote down vote up
private void rollback(TestContext context) {
  PostgresClient c1 = PostgresClient.getInstance(vertx, tenant);
  Async async = context.async();
  c1.startTx( handler -> {
    if(handler.succeeded()){
      SimplePojo z = new SimplePojo();
      z.setId("1");
      z.setName("me");
      c1.update(handler,
        table, z, "jsonb", "where (jsonb->>'name') = 'd'", true, reply -> {
          if(reply.succeeded()){
            c1.rollbackTx(handler, done -> {
              if(done.succeeded()){
                c1.select("SELECT jsonb->>'name' FROM " + table, reply2 -> {
                  if (! reply2.succeeded()) {
                    context.fail(reply2.cause());
                  }
                  else{
                    try {
                      String name = reply2.result().iterator().next().getString(0);
                      context.assertEquals("me", name, "Name property should not have been changed");
                    } catch (Exception e) {
                       e.printStackTrace();
                       context.fail(e.getMessage());
                    }
                    async.complete();
                  }
                });
              }
              else{
                context.fail(done.cause());
              }
            });
          }
          else{
            context.fail(reply.cause());
          }
        });
    }
    else{
      context.fail(handler.cause());
    }
  });
  async.await(5000 /* ms */);
  c1.closeClient(context.asyncAssertSuccess());
}
 
Example 19
Source File: SessionAwareWebClientTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testRequestIsPrepared(TestContext context) {
  prepareServer(context, req -> {
    req.response().headers().add("set-cookie", ServerCookieEncoder.STRICT.encode(new DefaultCookie("test", "toast")));
  });

  Consumer<HttpRequest<Buffer>> check = r -> {
    Async async = context.async();
    Cookie c = new DefaultCookie("test", "localhost");
    c.setPath("/");
    client.cookieStore().remove(c);
    r.send(ar -> {
      async.complete();
      validate(context, client.cookieStore().get(false, "localhost", "/"),
          new String[] { "test" }, new String[] { "toast" });
    });
    async.await();
  };

  check.accept(client.delete("/"));
  check.accept(client.delete("localhost", "/"));
  check.accept(client.delete(PORT, "localhost", "/"));
  check.accept(client.deleteAbs("http://localhost/"));
  check.accept(client.get("/"));
  check.accept(client.get("localhost", "/"));
  check.accept(client.get(PORT, "localhost", "/"));
  check.accept(client.getAbs("http://localhost/"));
  check.accept(client.head("/"));
  check.accept(client.head("localhost", "/"));
  check.accept(client.head(PORT, "localhost", "/"));
  check.accept(client.headAbs("http://localhost/"));
  check.accept(client.patch("/"));
  check.accept(client.patch("localhost", "/"));
  check.accept(client.patch(PORT, "localhost", "/"));
  check.accept(client.patchAbs("http://localhost/"));
  check.accept(client.post("/"));
  check.accept(client.post("localhost", "/"));
  check.accept(client.post(PORT, "localhost", "/"));
  check.accept(client.postAbs("http://localhost/"));
  check.accept(client.put("/"));
  check.accept(client.put("localhost", "/"));
  check.accept(client.put(PORT, "localhost", "/"));
  check.accept(client.putAbs("http://localhost/"));
  check.accept(client.request(HttpMethod.GET, new RequestOptions()));
  check.accept(client.request(HttpMethod.GET, "/"));
  check.accept(client.request(HttpMethod.GET, "localhost", "/"));
  check.accept(client.request(HttpMethod.GET, PORT, "localhost", "/"));
  check.accept(client.requestAbs(HttpMethod.GET, "http://localhost/"));
}
 
Example 20
Source File: ConditionalLoggerTest.java    From prebid-server-java with Apache License 2.0 4 votes vote down vote up
private void doWait(TestContext context, long timeout) {
    final Async async = context.async();
    vertx.setTimer(timeout, id -> async.complete());
    async.await();
}