<commit_before>package io.dropwizard.metrics.httpclient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.junit.Before;
import org.junit.Test;

import io.dropwizard.metrics.MetricName;
import io.dropwizard.metrics.MetricRegistry;
import io.dropwizard.metrics.MetricRegistryListener;
import io.dropwizard.metrics.Timer;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

public class InstrumentedHttpClientsTest {
    private final HttpClientMetricNameStrategy metricNameStrategy =
            mock(HttpClientMetricNameStrategy.class);
    private final MetricRegistryListener registryListener =
            mock(MetricRegistryListener.class);
    private final MetricRegistry metricRegistry = new MetricRegistry();
    private final HttpClient client =
            InstrumentedHttpClients.custom(metricRegistry, metricNameStrategy).disableAutomaticRetries().build();

    @Before
    public void setUp() throws Exception {
        metricRegistry.addListener(registryListener);
    }

    @Test
    public void registersExpectedMetricsGivenNameStrategy() throws Exception {
        final HttpGet get = new HttpGet("http://example.com?q=anything");
        final MetricName metricName = MetricName.build("some.made.up.metric.name");

        when(metricNameStrategy.getNameFor(anyString(), any(HttpRequest.class)))
                .thenReturn(metricName);

        client.execute(get);

        verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
    }

    @Test
    public void registersExpectedExceptionMetrics() throws Exception {
        ServerSocket server = new ServerSocket();
        server.bind(new InetSocketAddress("localhost", 0));
        final HttpGet get = new HttpGet("http://localhost:" + server.getLocalPort() + "/");
        final MetricName requestMetricName = MetricName.build("request");
        final MetricName exceptionMetricName = MetricName.build("exception");

        Thread serverThread = new Thread(() -> {
            try {
                final Socket socket = server.accept();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        serverThread.start();

        when(metricNameStrategy.getNameFor(anyString(), any(HttpRequest.class)))
            .thenReturn(requestMetricName);
        when(metricNameStrategy.getNameFor(anyString(), any(Exception.class)))
            .thenReturn(exceptionMetricName);

        try {
            client.execute(get);
            fail();
        } catch (NoHttpResponseException expected) {
            assertThat(metricRegistry.getMeters()).containsKey(new MetricName("exception"));
        } finally {
            serverThread.interrupt();
            serverThread.join();
        }
    }
}<commit_msg>Fixed test to catch IOException instead of just NoHttpResponseException since the test can throw a SocketException: Connection reset (#1054)<commit_after>package io.dropwizard.metrics.httpclient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.junit.Before;
import org.junit.Test;

import io.dropwizard.metrics.MetricName;
import io.dropwizard.metrics.MetricRegistry;
import io.dropwizard.metrics.MetricRegistryListener;
import io.dropwizard.metrics.Timer;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

public class InstrumentedHttpClientsTest {
    private final HttpClientMetricNameStrategy metricNameStrategy =
            mock(HttpClientMetricNameStrategy.class);
    private final MetricRegistryListener registryListener =
            mock(MetricRegistryListener.class);
    private final MetricRegistry metricRegistry = new MetricRegistry();
    private final HttpClient client =
            InstrumentedHttpClients.custom(metricRegistry, metricNameStrategy).disableAutomaticRetries().build();

    @Before
    public void setUp() throws Exception {
        metricRegistry.addListener(registryListener);
    }

    @Test
    public void registersExpectedMetricsGivenNameStrategy() throws Exception {
        final HttpGet get = new HttpGet("http://example.com?q=anything");
        final MetricName metricName = MetricName.build("some.made.up.metric.name");

        when(metricNameStrategy.getNameFor(anyString(), any(HttpRequest.class)))
                .thenReturn(metricName);

        client.execute(get);

        verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
    }

    @Test
    public void registersExpectedExceptionMetrics() throws Exception {
        ServerSocket server = new ServerSocket();
        server.bind(new InetSocketAddress("localhost", 0));
        final HttpGet get = new HttpGet("http://localhost:" + server.getLocalPort() + "/");
        final MetricName requestMetricName = MetricName.build("request");
        final MetricName exceptionMetricName = MetricName.build("exception");

        Thread serverThread = new Thread(() -> {
            try {
                final Socket socket = server.accept();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        serverThread.start();

        when(metricNameStrategy.getNameFor(anyString(), any(HttpRequest.class)))
            .thenReturn(requestMetricName);
        when(metricNameStrategy.getNameFor(anyString(), any(Exception.class)))
            .thenReturn(exceptionMetricName);

        try {
            client.execute(get);
            fail();
        } catch (IOException expected) {
            assertThat(metricRegistry.getMeters()).containsKey(new MetricName("exception"));
        } finally {
            serverThread.interrupt();
            serverThread.join();
        }
    }
}
{'fraction_non_alphanumeric': 0.10142049793798687, 'fraction_numerical': 0.0009164502825721705, 'mean_word_length': 3.353723404255319, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337951', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1651, 'n_words': 336}