blob: 9207fb349359f45ca805948ca4b510bc72314579 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
// Copyright Yahoo. 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_exceptions_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").readAll();
String expectedMetricName = "jdisc.http.handler.unhandled_exceptions";
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 {}
}
|