aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/jdisc/ThreadedHttpRequestHandlerTest.java
blob: 8590983d0adfa4543383d787844e10983294ea76 (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
// 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.container.test.MetricMock;
import com.yahoo.jdisc.Metric;
import org.junit.jupiter.api.Test;

import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author bjorncs
 */
public class ThreadedHttpRequestHandlerTest {

    @Test
    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";
        assertTrue(metricMock.innvocations()
                .containsKey(expectedMetricName));
        assertEquals("DummyException",
                ((MetricMock.SimpleMetricContext) metricMock.innvocations().get(expectedMetricName).ctx).dimensions.get("exception"));
    }


    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 {}
}