diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-03-12 12:46:30 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2020-03-12 12:46:30 +0100 |
commit | 0907c88c0b4dec4ad0037c9c2d3dc1f1c61ab4c4 (patch) | |
tree | 662f4260ecb7128910685370291681c0b289d5a7 /container-core | |
parent | 9cb56ed899c987600263764cb00f9baf3e190ea1 (diff) |
Add unit tests that verifies metric is propagated
Diffstat (limited to 'container-core')
-rw-r--r-- | container-core/pom.xml | 5 | ||||
-rw-r--r-- | container-core/src/test/java/com/yahoo/container/jdisc/ThreadedHttpRequestHandlerTest.java | 57 |
2 files changed, 62 insertions, 0 deletions
diff --git a/container-core/pom.xml b/container-core/pom.xml index e51d99c3b78..64e5ebb00d3 100644 --- a/container-core/pom.xml +++ b/container-core/pom.xml @@ -242,6 +242,11 @@ <artifactId>wiremock-standalone</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> diff --git a/container-core/src/test/java/com/yahoo/container/jdisc/ThreadedHttpRequestHandlerTest.java b/container-core/src/test/java/com/yahoo/container/jdisc/ThreadedHttpRequestHandlerTest.java new file mode 100644 index 00000000000..fc86da5719f --- /dev/null +++ b/container-core/src/test/java/com/yahoo/container/jdisc/ThreadedHttpRequestHandlerTest.java @@ -0,0 +1,57 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.jdisc; + +import com.yahoo.jdisc.Metric; +import org.junit.Test; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author bjorncs + */ +public class ThreadedHttpRequestHandlerTest { + + @Test + public void unhandled_exception_metric_is_incremented_if_subclassed_handler_throws_exception() { + MetricMock metricMock = new MetricMock(); + ThreadedHttpRequestHandlerThrowingException handler = new ThreadedHttpRequestHandlerThrowingException(metricMock); + RequestHandlerTestDriver driver = new RequestHandlerTestDriver(handler); + + driver.sendRequest("http://localhost/myhandler"); + String expectedMetricName = "jdisc.http.handler.unhandled_exception"; + assertThat(metricMock.addInvocations) + .containsKey(expectedMetricName); + assertThat(metricMock.addInvocations.get(expectedMetricName).dimensions) + .containsEntry("exception", "DummyException"); + } + + private static class MetricMock implements Metric { + final ConcurrentHashMap<String, SimpleMetricContext> addInvocations = new ConcurrentHashMap<>(); + + @Override public void add(String key, Number val, Context ctx) { + addInvocations.put(key, (SimpleMetricContext)ctx); + } + @Override public void set(String key, Number val, Context ctx) {} + @Override public Context createContext(Map<String, ?> properties) { return new SimpleMetricContext(properties); } + } + + private static class SimpleMetricContext implements Metric.Context { + final Map<String, String> dimensions; + + @SuppressWarnings("unchecked") + SimpleMetricContext(Map<String, ?> dimensions) { this.dimensions = (Map<String, String>)dimensions; } + } + + private static class ThreadedHttpRequestHandlerThrowingException extends ThreadedHttpRequestHandler { + ThreadedHttpRequestHandlerThrowingException(Metric metric) { + super(Executors.newSingleThreadExecutor(), metric); + } + @Override public HttpResponse handle(HttpRequest request) { throw new DummyException(); } + } + + private static class DummyException extends RuntimeException {} +}
\ No newline at end of file |