Skip to content

Commit

Permalink
Merge branch '1.1.x' into 1.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
shakuzen committed Nov 4, 2019
2 parents cfe898e + 4d33b8f commit 80168a3
Showing 1 changed file with 96 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,34 @@
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.core.instrument.util.IOUtils;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.session.ManagerBase;
import org.apache.catalina.session.StandardSession;
import org.apache.catalina.session.TooManyActiveSessionsException;
import org.apache.catalina.startup.Tomcat;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;


/**
* Tests for {@link TomcatMetrics}.
*
Expand Down Expand Up @@ -95,7 +107,7 @@ public Context getContext() {
}

@Test
void mbeansAvailableAfterBinder() throws LifecycleException, InterruptedException {
void mbeansAvailableAfterBinder() throws Exception {
TomcatMetrics.monitor(registry, null);

CountDownLatch latch = new CountDownLatch(1);
Expand All @@ -104,25 +116,63 @@ void mbeansAvailableAfterBinder() throws LifecycleException, InterruptedExceptio
latch.countDown();
});

Tomcat server = new Tomcat();
try {
StandardHost host = new StandardHost();
host.setName("localhost");
server.setHost(host);
server.setPort(61000);
server.start();
HttpServlet servlet = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
IOUtils.toString(req.getInputStream());

resp.getOutputStream().write("yes".getBytes());
}
};

runTomcat(servlet, () -> {
assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();

assertThat(registry.find("tomcat.global.received").functionCounter()).isNotNull();
} finally {
server.stop();
server.destroy();
}
}
checkMbeansInitialState();

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost post = new HttpPost("http://localhost:61000/");
post.setEntity(new StringEntity("you there?"));
httpClient.execute(post);

httpClient.execute(new HttpGet("http://localhost:61000/nowhere"));
}

checkMbeansAfterRequests();

return null;
});
}
@Test
void mbeansAvailableBeforeBinder() throws LifecycleException {
void mbeansAvailableBeforeBinder() throws Exception {
HttpServlet servlet = new HttpServlet() {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
IOUtils.toString(req.getInputStream());

resp.getOutputStream().write("yes".getBytes());
}
};

runTomcat(servlet, () -> {
TomcatMetrics.monitor(registry, null);

try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost post = new HttpPost("http://localhost:61000/");
post.setEntity(new StringEntity("you there?"));
httpClient.execute(post);

httpClient.execute(new HttpGet("http://localhost:61000/nowhere"));
}

checkMbeansAfterRequests();

return null;
});
}


void runTomcat(HttpServlet servlet, Callable<Void> doWithTomcat) throws Exception {
Tomcat server = new Tomcat();
try {
StandardHost host = new StandardHost();
Expand All @@ -131,12 +181,40 @@ void mbeansAvailableBeforeBinder() throws LifecycleException {
server.setPort(61000);
server.start();

TomcatMetrics.monitor(registry, null);
assertThat(registry.find("tomcat.global.received").functionCounter()).isNotNull();
Context context = server.addContext("/", null);
server.addServlet("/", "servletname", servlet);
context.addServletMappingDecoded("/", "servletname");

doWithTomcat.call();

} finally {
server.stop();
server.destroy();

}
}

private void checkMbeansInitialState() {
assertThat(registry.get("tomcat.global.sent").functionCounter().count()).isEqualTo(0.0);
assertThat(registry.get("tomcat.global.received").functionCounter().count()).isEqualTo(0.0);
assertThat(registry.get("tomcat.global.error").functionCounter().count()).isEqualTo(0.0);
assertThat(registry.get("tomcat.global.request").functionTimer().count()).isEqualTo(0.0);
assertThat(registry.get("tomcat.global.request").functionTimer().totalTime(TimeUnit.MILLISECONDS)).isEqualTo(0.0);
assertThat(registry.get("tomcat.global.request.max").timeGauge().value(TimeUnit.MILLISECONDS)).isEqualTo(0.0);
assertThat(registry.get("tomcat.threads.config.max").gauge().value()).isGreaterThan(0.0);
assertThat(registry.get("tomcat.threads.busy").gauge().value()).isEqualTo(0.0);
assertThat(registry.get("tomcat.threads.current").gauge().value()).isGreaterThan(0.0);
}

private void checkMbeansAfterRequests() {
assertThat(registry.get("tomcat.global.sent").functionCounter().count()).isEqualTo(1119.0);
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.threads.config.max").gauge().value()).isGreaterThan(0.0);
assertThat(registry.get("tomcat.threads.busy").gauge().value()).isEqualTo(0.0);
assertThat(registry.get("tomcat.threads.current").gauge().value()).isGreaterThan(0.0);
}
}

0 comments on commit 80168a3

Please sign in to comment.