/** * Execute the provided handler, which may contain assertions, possibly from any third-party assertion framework. * Any {@link java.lang.AssertionError} thrown will be caught (and propagated) in order to fulfill potential expected async * completeness. * @param block block of code to be executed * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestContext verify(Handler<Void> block) { delegate.verify(block); return this; }
/** * Execute the provided handler, which may contain assertions, possibly from any third-party assertion framework. * Any {@link java.lang.AssertionError} thrown will be caught (and propagated) in order to fulfill potential expected async * completeness. * @param block block of code to be executed * @return a reference to this, so the API can be used fluently */ public io.vertx.rxjava.ext.unit.TestContext verify(Handler<Void> block) { delegate.verify(block); return this; }
@Test public void success(TestContext context) { context.verify(v -> { count.incrementAndGet(); assertTrue(true); }); } }
@Test public void simpleFail(TestContext context) { context.verify(v -> fail("Testing failure")); } }
@Test public void failWithAsync(TestContext context) { Vertx vertx = Vertx.vertx(); Async async = context.async(); vertx.runOnContext(v -> { try { context.verify(v2 -> fail("Testing async failure")); } finally { async.complete(); } }); async.await(); } }
@Test public void shouldBindExistingServer(TestContext context) { vertx = Vertx.vertx(new VertxOptions() .setMetricsOptions(new MicrometerMetricsOptions() .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true)) .setEnabled(true))); Router router = Router.router(vertx); router.route("/custom").handler(routingContext -> { PrometheusMeterRegistry prometheusRegistry = (PrometheusMeterRegistry) BackendRegistries.getDefaultNow(); String response = prometheusRegistry.scrape(); routingContext.response().end(response); }); vertx.createHttpServer().requestHandler(router).exceptionHandler(context.exceptionHandler()).listen(8081); Async async = context.async(); PrometheusTestHelper.tryConnect(vertx, context, 8081, "localhost", "/custom", body -> { context.verify(v -> assertThat(body.toString()) .contains("vertx_http_")); async.complete(); }); async.awaitSuccess(10000); }
@Test public void shouldPublishPercentileStats(TestContext context) throws Exception { vertx = Vertx.vertx(new VertxOptions() .setMetricsOptions(new MicrometerMetricsOptions() .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true) .setPublishQuantiles(true) .setStartEmbeddedServer(true) .setEmbeddedServerOptions(new HttpServerOptions().setPort(9090))) .addLabels(Label.LOCAL, Label.HTTP_PATH, Label.REMOTE) .setEnabled(true))); Async async = context.async(); PrometheusTestHelper.tryConnect(vertx, context, 9090, "localhost", "/metrics", body -> { context.verify(v -> assertThat(body.toString()) .contains("vertx_http_client_responseTime_seconds_bucket")); async.complete(); }); async.awaitSuccess(10000); } }
@Test public void shouldStartEmbeddedServer(TestContext context) throws Exception { vertx = Vertx.vertx(new VertxOptions() .setMetricsOptions(new MicrometerMetricsOptions() .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true) .setStartEmbeddedServer(true) .setEmbeddedServerOptions(new HttpServerOptions().setPort(9090))) .addLabels(Label.LOCAL, Label.HTTP_PATH, Label.REMOTE) .setEnabled(true))); Async async = context.async(); PrometheusTestHelper.tryConnect(vertx, context, 9090, "localhost", "/metrics", body -> { context.verify(v -> assertThat(body.toString()) .contains("vertx_http_client_requests{local=\"?\",method=\"GET\",path=\"/metrics\",remote=\"localhost:9090\",} 1.0") .doesNotContain("vertx_http_client_responseTime_seconds_bucket")); async.complete(); }); async.awaitSuccess(10000); }
@Test public void shouldExcludeCategory(TestContext context) { vertx = Vertx.vertx(new VertxOptions() .setMetricsOptions(new MicrometerMetricsOptions() .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true) .setStartEmbeddedServer(true) .setEmbeddedServerOptions(new HttpServerOptions().setPort(9090))) .addDisabledMetricsCategory(MetricsDomain.HTTP_SERVER) .addLabels(Label.LOCAL, Label.REMOTE) .setEnabled(true))); Async async = context.async(); PrometheusTestHelper.tryConnect(vertx, context, 9090, "localhost", "/metrics", body -> { context.verify(v -> assertThat(body.toString()) .contains("vertx_http_client_connections{local=\"?\",remote=\"localhost:9090\",} 1.0") .doesNotContain("vertx_http_server_connections{local=\"0.0.0.0:9090\",remote=\"_\",} 1.0")); async.complete(); }); async.awaitSuccess(10000); }
InfluxDbTestHelper.simulateInfluxServer(vertxForSimulatedServer, context, 8087, body -> { try { context.verify(w -> assertThat(body) .contains("vertx_eventbus_handlers,address=test-eb,metric_type=gauge value=1")); } finally {
@Test public void shouldExposeEventBusMetrics(TestContext context) { vertx = Vertx.vertx(new VertxOptions() .setMetricsOptions(new MicrometerMetricsOptions() .setPrometheusOptions(new VertxPrometheusOptions().setEnabled(true) .setStartEmbeddedServer(true) .setEmbeddedServerOptions(new HttpServerOptions().setPort(9090))) .addLabels(Label.EB_ADDRESS) .setEnabled(true))); // Send something on the eventbus and wait til it's received Async asyncEB = context.async(); vertx.eventBus().consumer("test-eb", msg -> asyncEB.complete()); vertx.eventBus().publish("test-eb", "test message"); asyncEB.awaitSuccess(15000); // Read metrics on HTTP endpoint for eventbus metrics Async async = context.async(); PrometheusTestHelper.tryConnect(vertx, context, 9090, "localhost", "/metrics", body -> { context.verify(v -> assertThat(body.toString()) .contains("vertx_eventbus_published_total{address=\"test-eb\",side=\"local\",} 1.0", "vertx_eventbus_received_total{address=\"test-eb\",side=\"local\",} 1.0", "vertx_eventbus_handlers{address=\"test-eb\",} 1.0", "vertx_eventbus_delivered_total{address=\"test-eb\",side=\"local\",} 1.0", "vertx_eventbus_processingTime_seconds_count{address=\"test-eb\",} 1.0")); async.complete(); }); async.awaitSuccess(15000); }