package com.codahale.metrics.httpasyncclient;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricRegistryListener;
import com.codahale.metrics.Timer;
import com.codahale.metrics.httpclient.HttpClientMetricNameStrategy;
import com.codahale.metrics.httpclient.InstrumentedHttpClients;
import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.nio.client.HttpAsyncClient;
import org.junit.Before;
import org.junit.Test;
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 HttpAsyncClient hac;
@Before
public void setUp() throws Exception {
CloseableHttpAsyncClient chac = new InstrumentedNHttpClientBuilder(metricRegistry, metricNameStrategy).build();
chac.start();
hac = chac;
metricRegistry.addListener(registryListener);
}
@Test
public void registersExpectedMetricsGivenNameStrategy() throws Exception {
final HttpGet get = new HttpGet("http://example.com?q=anything");
final String metricName = "some.made.up.metric.name";
when(metricNameStrategy.getNameFor(anyString(), any(HttpRequest.class)))
.thenReturn(metricName);
hac.execute(get,null).get();
verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
}
}