Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix micrometer test #3234

Merged
merged 9 commits into from
Aug 3, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
*/
public final class Main {

static final String PERSONALIZED_GETS_COUNTER_NAME = "personalizedGets";
static final String ALL_GETS_TIMER_NAME = "allGets";

/**
* Cannot be instantiated.
*/
Expand Down Expand Up @@ -61,6 +64,7 @@ static Single<WebServer> startServer() {
// Get webserver config from the "server" section of application.yaml
WebServer server = WebServer.builder(createRouting(config))
.config(config.get("server"))
.port(-1)
.addMediaSupport(JsonpSupport.create())
.build();

Expand Down Expand Up @@ -90,8 +94,8 @@ private static Routing createRouting(Config config) {

MicrometerSupport micrometerSupport = MicrometerSupport.create();
Counter personalizedGetCounter = micrometerSupport.registry()
.counter("personalizedGets");
Timer getTimer = Timer.builder("allGets")
.counter(PERSONALIZED_GETS_COUNTER_NAME);
Timer getTimer = Timer.builder(ALL_GETS_TIMER_NAME)
.publishPercentileHistogram()
.register(micrometerSupport.registry());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
Expand All @@ -47,6 +46,9 @@ public class MainTest {
private static WebServer webServer;
private static WebClient webClient;

private static double expectedPersonalizedGets;
private static double expectedAllGets;

static {
TEST_JSON_OBJECT = JSON_BF.createObjectBuilder()
.add("greeting", "Hola")
Expand All @@ -56,7 +58,7 @@ public class MainTest {
@BeforeAll
public static void startTheServer() {
webServer = Main.startServer()
.await(2, TimeUnit.SECONDS);
.await(10, TimeUnit.SECONDS);

webClient = WebClient.builder()
.baseUri("http://localhost:" + webServer.port())
Expand All @@ -72,25 +74,36 @@ public static void stopServer() {
}
}

@Test
@Order(1)
void testDefaultGreeting() {
private static JsonObject get() {
return get("/greet");
}

private static JsonObject get(String path) {
JsonObject jsonObject = webClient.get()
.path("/greet")
.path(path)
.request(JsonObject.class)
.await();
expectedAllGets++;
return jsonObject;
}

private static JsonObject personalizedGet(String name) {
JsonObject result = get("/greet/" + name);
expectedPersonalizedGets++;
return result;
}

@Test
@Order(1)
void testDefaultGreeting() {
JsonObject jsonObject = get();
Assertions.assertEquals("Hello World!", jsonObject.getString("greeting"));
}

@Test
@Order(2)
void testNamedGreeting() {
JsonObject jsonObject = webClient.get()
.path("/greet/Joe")
.request(JsonObject.class)
.await();

JsonObject jsonObject = personalizedGet("Joe");
Assertions.assertEquals("Hello Joe!", jsonObject.getString("greeting"));
}

Expand All @@ -105,17 +118,12 @@ void testUpdateGreeting() {

Assertions.assertEquals(Http.Status.NO_CONTENT_204, response.status());

JsonObject jsonObject = webClient.get()
.path("/greet/Joe")
.request(JsonObject.class)
.await();

JsonObject jsonObject = personalizedGet("Joe");
Assertions.assertEquals("Hola Joe!", jsonObject.getString("greeting"));
}

@Test
@Order(4)
@Disabled("This test is broken, follow up issue created")
void testMicrometer() {
WebClientResponse response = webClient.get()
.path("/micrometer")
Expand All @@ -128,12 +136,15 @@ void testMicrometer() {
String output = response.content()
.as(String.class)
.await();
Assertions.assertTrue(output.contains("get_seconds_count 2.0"),
"Unable to find expected all-gets timer count 2.0"); // 2 gets; the put is not counted
Assertions.assertTrue(output.contains("get_seconds_sum"),
"Unable to find expected all-gets timer sum");
Assertions.assertTrue(output.contains("personalizedGets_total 1.0"),
"Unable to find expected counter result 1.0");
String expected = Main.ALL_GETS_TIMER_NAME + "_seconds_count " + expectedAllGets;
Assertions.assertTrue(output.contains(expected),
"Unable to find expected all-gets timer count " + expected + "; output is " + output); // all gets; the put
// is not counted
Assertions.assertTrue(output.contains(Main.ALL_GETS_TIMER_NAME + "_seconds_sum"),
"Unable to find expected all-gets timer sum");
expected = Main.PERSONALIZED_GETS_COUNTER_NAME + "_total " + expectedPersonalizedGets;
Assertions.assertTrue(output.contains(expected),
"Unable to find expected counter result " + expected + "; output is " + output);
response.close();
}
}