diff --git a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/tomcat/TomcatMetricsTest.java b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/tomcat/TomcatMetricsTest.java index d5eba10b76..56e4c23207 100644 --- a/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/tomcat/TomcatMetricsTest.java +++ b/micrometer-core/src/test/java/io/micrometer/core/instrument/binder/tomcat/TomcatMetricsTest.java @@ -59,6 +59,8 @@ * @author Johnny Lim */ class TomcatMetricsTest { + private static final int PROCESSING_TIME_IN_MILLIS = 10; + private SimpleMeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()); private int port; @@ -122,6 +124,14 @@ public Context getContext() { assertThat(registry.get("tomcat.sessions.alive.max").tags(tags).timeGauge().value()).isGreaterThan(1.0); } + private void sleep() { + try { + Thread.sleep(PROCESSING_TIME_IN_MILLIS); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + @Test void mbeansAvailableAfterBinder() throws Exception { TomcatMetrics.monitor(registry, null); @@ -136,7 +146,7 @@ void mbeansAvailableAfterBinder() throws Exception { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { IOUtils.toString(req.getInputStream()); - + sleep(); resp.getOutputStream().write("yes".getBytes()); } }; @@ -162,13 +172,14 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I return null; }); } + @Test void mbeansAvailableBeforeBinder() throws Exception { HttpServlet servlet = new HttpServlet() { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { IOUtils.toString(req.getInputStream()); - + sleep(); resp.getOutputStream().write("yes".getBytes()); } }; @@ -176,6 +187,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I runTomcat(servlet, () -> { TomcatMetrics.monitor(registry, null); + checkMbeansInitialState(); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost post = new HttpPost("http://localhost:" + this.port + "/"); post.setEntity(new StringEntity("you there?")); @@ -236,8 +249,8 @@ private void checkMbeansAfterRequests(long expectedSentBytes) { assertThat(registry.get("tomcat.global.received").functionCounter().count()).isEqualTo(10.0); assertThat(registry.get("tomcat.global.error").functionCounter().count()).isEqualTo(1.0); assertThat(registry.get("tomcat.global.request").functionTimer().count()).isEqualTo(2.0); - assertThat(registry.get("tomcat.global.request").functionTimer().totalTime(TimeUnit.MILLISECONDS)).isGreaterThan(0.0); - assertThat(registry.get("tomcat.global.request.max").timeGauge().value(TimeUnit.MILLISECONDS)).isGreaterThan(0.0); + assertThat(registry.get("tomcat.global.request").functionTimer().totalTime(TimeUnit.MILLISECONDS)).isGreaterThanOrEqualTo(PROCESSING_TIME_IN_MILLIS); + assertThat(registry.get("tomcat.global.request.max").timeGauge().value(TimeUnit.MILLISECONDS)).isGreaterThanOrEqualTo(PROCESSING_TIME_IN_MILLIS); assertThat(registry.get("tomcat.threads.config.max").gauge().value()).isGreaterThan(0.0); assertThat(registry.get("tomcat.threads.busy").gauge().value()).isGreaterThanOrEqualTo(0.0); assertThat(registry.get("tomcat.threads.current").gauge().value()).isGreaterThan(0.0);