Skip to content

Commit

Permalink
br/TACO-137 converted metric handler integration tests to use mocks i…
Browse files Browse the repository at this point in the history
…n hopes it solves a race condition
  • Loading branch information
Brian Radebaugh committed Apr 19, 2018
1 parent ffdd55c commit 2fda754
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xjeffrose.xio.application;

import com.codahale.metrics.MetricRegistry;
import com.google.common.annotations.VisibleForTesting;
import com.xjeffrose.xio.bootstrap.ChannelConfiguration;
import com.xjeffrose.xio.bootstrap.ClientChannelConfiguration;
import com.xjeffrose.xio.bootstrap.ServerChannelConfiguration;
Expand Down Expand Up @@ -33,7 +34,12 @@ public class ApplicationState {
@Getter
private final XioTracing tracing;

@Getter private final MetricRegistry metricRegistry;
@Getter private MetricRegistry metricRegistry;

@VisibleForTesting
public void setMetricRegistry(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
}

private final AtomicReference<IpFilterConfig> ipFilterConfig;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xjeffrose.xio.metric;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
Expand All @@ -22,7 +22,8 @@

public class HttpServerMetricHandlerIntegrationTest {
private Application application = null;
private MetricRegistry metricRegistry = null;
private Meter requestsMeter = null;
private Meter successMeter = null;

@Before
public void before() {
Expand All @@ -32,52 +33,39 @@ public void before() {
"exampleServer", (bs) -> bs.addToPipeline(new SmartHttpPipeline(TestHandler::new)))
.build();

metricRegistry = application.getState().getMetricRegistry();
}

@After
public void stop() {
application.close();
}
MetricRegistry metricRegistry = mock(MetricRegistry.class);

@Test
public void testRequestMetricH1() throws Exception {
InetSocketAddress address = application.instrumentation("exampleServer").boundAddress();
ClientHelper.https(address, Protocol.HTTP_1_1);
ClientHelper.https(address, Protocol.HTTP_1_1);
requestsMeter = mock(Meter.class);
when(metricRegistry.meter("requests")).thenReturn(requestsMeter);
successMeter = mock(Meter.class);
when(metricRegistry.meter("statusClassSuccess")).thenReturn(successMeter);

Meter meter = metricRegistry.meter("requests");
assertEquals(2, meter.getCount());
application.getState().setMetricRegistry(metricRegistry);
}

@Test
public void testRequestMetricH2() throws Exception {
InetSocketAddress address = application.instrumentation("exampleServer").boundAddress();
ClientHelper.https(address, Protocol.HTTP_2, Protocol.HTTP_1_1);
ClientHelper.https(address, Protocol.HTTP_2, Protocol.HTTP_1_1);

Meter meter = metricRegistry.meter("requests");
assertEquals(2, meter.getCount());
@After
public void tearDown() {
application.close();
}

@Test
public void teststatusClassMetricH1() throws Exception {
public void testMetricsH1() throws Exception {
InetSocketAddress address = application.instrumentation("exampleServer").boundAddress();
ClientHelper.https(address, Protocol.HTTP_1_1);
ClientHelper.https(address, Protocol.HTTP_1_1);

Meter meter = metricRegistry.meter("statusClassSuccess");
assertEquals(2, meter.getCount());
verify(requestsMeter, times(2)).mark();
verify(successMeter, times(2)).mark();
}

@Test
public void teststatusClassMetricH2() throws Exception {
public void testMetricsH2() throws Exception {
InetSocketAddress address = application.instrumentation("exampleServer").boundAddress();
ClientHelper.https(address, Protocol.HTTP_2, Protocol.HTTP_1_1);
ClientHelper.https(address, Protocol.HTTP_2, Protocol.HTTP_1_1);

Meter meter = metricRegistry.meter("statusClassSuccess");
assertEquals(2, meter.getCount());
verify(requestsMeter, times(2)).mark();
verify(successMeter, times(2)).mark();
}

private class TestHandler extends SimpleChannelInboundHandler<Request> {
Expand Down

0 comments on commit 2fda754

Please sign in to comment.