Skip to content

Commit

Permalink
[WFLY-11594] Prepare tests for MicroProfile Metrics issues 42 and 43
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Terem authored and marekkopecky committed Feb 4, 2019
1 parent 5b1677c commit 9e77ec1
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 4 deletions.
Expand Up @@ -88,7 +88,7 @@ public static double getMetricValueFromJSONOutput(String jsonContent, String cou
*/
public static double getMetricSubValueFromJSONOutput(String jsonContent, String value, String counterName) {
try (
JsonReader jsonReader = Json.createReader(new StringReader(jsonContent))
JsonReader jsonReader = Json.createReader(new StringReader(jsonContent))
) {
JsonObject payload = (JsonObject) jsonReader.readObject().get(value);
JsonNumber count = payload.getJsonNumber(counterName);
Expand Down
Expand Up @@ -20,7 +20,7 @@
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.test.integration.microprofile.metrics.application.resource;
package org.wildfly.test.integration.microprofile.metrics;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
Expand Down
Expand Up @@ -61,7 +61,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.integration.microprofile.metrics.application.resource.ResourceSimple;
import org.wildfly.test.integration.microprofile.metrics.application.resource.TestApplication;
import org.wildfly.test.integration.microprofile.metrics.TestApplication;

/**
* Test application metrics that are provided by a deployment.
Expand Down
Expand Up @@ -41,7 +41,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.integration.microprofile.metrics.application.resource.ResourceMeteredTimed;
import org.wildfly.test.integration.microprofile.metrics.application.resource.TestApplication;
import org.wildfly.test.integration.microprofile.metrics.TestApplication;


/**
Expand Down
@@ -0,0 +1,98 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.test.integration.microprofile.metrics.metadata;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.hornetq.utils.json.JSONObject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ContainerResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.integration.microprofile.metrics.TestApplication;
import org.wildfly.test.integration.microprofile.metrics.metadata.resources.MicroProfileMetricsCounterResource;

import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.wildfly.test.integration.microprofile.metrics.MetricsHelper.getJSONMetrics;


/**
* Regression test for SmallRye Metrics issue:
* https://github.com/smallrye/smallrye-metrics/issues/43
* Counter - registry.counter(metadata) returns IllegalArgumentException on second invocation of the endpoint.
*/
@RunWith(Arquillian.class)
@RunAsClient
public class MicroProfileMetricsCounterMultipleInvocationsTestCase {


/**
* ManagementClient provides management port and hostname
*/
@ContainerResource
ManagementClient managementClient;

/**
* Prepare deployment
*/
@Deployment
public static Archive<?> deploy() {
WebArchive war = ShrinkWrap.create(WebArchive.class, MicroProfileMetricsCounterMultipleInvocationsTestCase.class.getSimpleName() + ".war")
.addClasses(TestApplication.class)
.addClasses(MicroProfileMetricsCounterResource.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return war;
}

/**
* Perform 5 invocations on an endpoint. After first invocation, pre-existing counter should be used.
*/
@Test
public void testMultipleInvocationsOfEndpoint(@ArquillianResource URL url) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
for (int i = 1; i <= 5; i++) {
URL endpointURL = new URL(url.toExternalForm() + "microprofile-metrics-app/counter/hello");
HttpGet httpGet = new HttpGet(endpointURL.toExternalForm());
CloseableHttpResponse resp = client.execute(httpGet);
assertEquals(200, resp.getStatusLine().getStatusCode());
String content = EntityUtils.toString(resp.getEntity());
assertTrue("'Hello World!' message is expected, but was: " + content, content.contains("Hello World!"));
}
}

String jsonString = getJSONMetrics(managementClient, "application/helloCounter", true);
JSONObject jsonObject = new JSONObject(jsonString);
int helloCounter = jsonObject.getInt("helloCounter");

Assert.assertTrue("Expected value of 'helloCounter' is 5, but was " + helloCounter, helloCounter == 5);
}
}
@@ -0,0 +1,117 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.test.integration.microprofile.metrics.metadata;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.as.arquillian.api.ContainerResource;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.test.integration.microprofile.metrics.TestApplication;
import org.wildfly.test.integration.microprofile.metrics.metadata.resources.MicroProfileMetricsHistogramResource;

import java.net.URL;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.wildfly.test.integration.microprofile.metrics.MetricsHelper.getJSONMetrics;
import static org.wildfly.test.integration.microprofile.metrics.MetricsHelper.getMetricSubValueFromJSONOutput;

/**
* Regression test for SmallRye Metrics issue:
* https://github.com/smallrye/smallrye-metrics/issues/42
* Histogram - registry.histogram(metadata) returns IllegalArgumentException on second invocation of the endpoint.
*/
@RunWith(Arquillian.class)
@RunAsClient
public class MicroProfileMetricsHistogramMultipleInvocationsTestCase {


/**
* Json metrics
*/
private static String json;

/**
* ManagementClient provides management port and hostname
*/
@ContainerResource
ManagementClient managementClient;

/**
* Prepare deployment
*/
@Deployment
public static Archive<?> deploy() {
WebArchive war = ShrinkWrap.create(WebArchive.class, MicroProfileMetricsHistogramMultipleInvocationsTestCase.class.getSimpleName() + ".war")
.addClasses(TestApplication.class)
.addClasses(MicroProfileMetricsHistogramResource.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
return war;
}

/**
* Perform 10 invocations on an endpoint. After first invocation, pre-existing histogram should be used.
*/
@Test
public void testMultipleInvocationsOfEndpoint(@ArquillianResource URL url) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 2; j++) { // use each number from 1 to 5 twice
URL endpointURL = new URL(url.toExternalForm() + "microprofile-metrics-app/histogram/hello/" + i);
HttpGet httpGet = new HttpGet(endpointURL.toExternalForm());
CloseableHttpResponse resp = client.execute(httpGet);
assertEquals(200, resp.getStatusLine().getStatusCode());
String content = EntityUtils.toString(resp.getEntity());
assertTrue("'Hello World!' message is expected, but was: " + content, content.contains("Hello World!"));
}
}
}

json = getJSONMetrics(managementClient, "application/helloHistogram", true);

checkValue("min", 1.0);
checkValue("max", 5.0);
checkValue("mean", 3.0);
checkValue("stddev", 1.4142135623730951);
checkValue("count", 10.0);
checkValue("p50", 3.0);
checkValue("p75", 4.0);
checkValue("p95", 5.0);
checkValue("p98", 5.0);
checkValue("p99", 5.0);
checkValue("p999", 5.0);
}

private static void checkValue(String key, Double expectedValue) {
Double realValue = getMetricSubValueFromJSONOutput(json, "helloHistogram", key);
Assert.assertTrue("Expected value of '" + key + "' is " + expectedValue + ", but was " + realValue +".", expectedValue.equals(realValue));
}
}
@@ -0,0 +1,45 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.test.integration.microprofile.metrics.metadata.resources;

import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/counter")
public class MicroProfileMetricsCounterResource {

@Inject
MetricRegistry registry;

@GET
@Path("/hello")
public Response hello() {
Metadata counterMetadata = new Metadata("helloCounter", MetricType.COUNTER);

// TODO: Remove following line once https://github.com/smallrye/smallrye-metrics/issues/43 is fixed
counterMetadata.setReusable(true); // workaround

registry.counter(counterMetadata).inc();
return Response.ok("Hello World!").build();
}
}
@@ -0,0 +1,49 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.wildfly.test.integration.microprofile.metrics.metadata.resources;

import org.eclipse.microprofile.metrics.Histogram;
import org.eclipse.microprofile.metrics.Metadata;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.eclipse.microprofile.metrics.MetricType;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/histogram")
public class MicroProfileMetricsHistogramResource {

@Inject
MetricRegistry registry;

@GET
@Path("/hello/{n}")
public Response hello(@PathParam("n") String n) {
Metadata histogramMetadata = new Metadata("helloHistogram", MetricType.HISTOGRAM);

// TODO: Remove following line once https://github.com/smallrye/smallrye-metrics/issues/42 is fixed
histogramMetadata.setReusable(true); // workaround

Histogram histogram = registry.histogram(histogramMetadata);
histogram.update(Long.valueOf(n));
return Response.ok("Hello World!").build();
}

}

0 comments on commit 9e77ec1

Please sign in to comment.