Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WFLY-11594] Prepare tests for MicroProfile Metrics issues 42 and 43
- Loading branch information
1 parent
5b1677c
commit 9e77ec1
Showing
8 changed files
with
313 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
.../microprofile/metrics/metadata/MicroProfileMetricsCounterMultipleInvocationsTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
117 changes: 117 additions & 0 deletions
117
...icroprofile/metrics/metadata/MicroProfileMetricsHistogramMultipleInvocationsTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...tegration/microprofile/metrics/metadata/resources/MicroProfileMetricsCounterResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...gration/microprofile/metrics/metadata/resources/MicroProfileMetricsHistogramResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |