Skip to content

Commit

Permalink
Updated two tests to JUnit 5 and to use mocks instead of actual metri…
Browse files Browse the repository at this point in the history
…cs. Updates to MetricsTestUtil to provide clarity on NPEs.

Signed-off-by: David Venable <dlv@amazon.com>
  • Loading branch information
dlvenable committed Jul 9, 2024
1 parent 77dff3d commit 3a96584
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ public class MetricsTestUtil {

public static synchronized void initMetrics() {
final Set<MeterRegistry> registries = new HashSet<>(Metrics.globalRegistry.getRegistries());
registries.forEach(meterRegistry -> Metrics.globalRegistry.remove(meterRegistry));
registries.forEach(Metrics.globalRegistry::remove);

final List<Meter> meters = new ArrayList<>(Metrics.globalRegistry.getMeters());
meters.forEach(meter -> Metrics.globalRegistry.remove(meter));
meters.forEach(Metrics.globalRegistry::remove);

Metrics.addRegistry(new SimpleMeterRegistry());
}

public static synchronized List<Measurement> getMeasurementList(final String meterName) {
return StreamSupport.stream(getRegistry().find(meterName).meter().measure().spliterator(), false)
final Meter meter = getRegistry().find(meterName).meter();
if(meter == null)
throw new RuntimeException("No metrics meter is available for " + meterName);

return StreamSupport.stream(meter.measure().spliterator(), false)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,33 @@

import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroup;
import io.micrometer.core.instrument.Measurement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opensearch.dataprepper.metrics.MetricNames;
import org.opensearch.dataprepper.metrics.MetricsTestUtil;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.peerforwarder.HashRing;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.CompletableFuture;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.function.ToDoubleFunction;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.opensearch.dataprepper.peerforwarder.discovery.PeerListProvider.PEER_ENDPOINTS;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class DnsPeerListProviderTest {

private static final String ENDPOINT_1 = "10.1.1.1";
Expand All @@ -39,43 +42,40 @@ public class DnsPeerListProviderTest {
Endpoint.of(ENDPOINT_1),
Endpoint.of(ENDPOINT_2)
);
private static final String COMPONENT_SCOPE = "testComponentScope";
private static final String COMPONENT_ID = "testComponentId";

@Mock
private DnsAddressEndpointGroup dnsAddressEndpointGroup;

@Mock
private HashRing hashRing;

@Mock
private PluginMetrics pluginMetrics;

private CompletableFuture completableFuture;

private DnsPeerListProvider dnsPeerListProvider;

@Before
@BeforeEach
public void setup() {
MetricsTestUtil.initMetrics();
completableFuture = CompletableFuture.completedFuture(null);
when(dnsAddressEndpointGroup.whenReady()).thenReturn(completableFuture);

pluginMetrics = PluginMetrics.fromNames(COMPONENT_ID, COMPONENT_SCOPE);
dnsPeerListProvider = new DnsPeerListProvider(dnsAddressEndpointGroup, pluginMetrics);
}

@Test(expected = NullPointerException.class)
@Test
public void testDefaultListProviderWithNullHostname() {
new DnsPeerListProvider(null, pluginMetrics);
assertThrows(NullPointerException.class, () -> new DnsPeerListProvider(null, pluginMetrics));
}

@Test(expected = RuntimeException.class)
@Test
public void testConstructWithInterruptedException() throws Exception {
CompletableFuture mockFuture = mock(CompletableFuture.class);
when(mockFuture.get()).thenThrow(new InterruptedException());
when(dnsAddressEndpointGroup.whenReady()).thenReturn(mockFuture);

new DnsPeerListProvider(dnsAddressEndpointGroup, pluginMetrics);
assertThrows(RuntimeException.class, () -> new DnsPeerListProvider(dnsAddressEndpointGroup, pluginMetrics));
}

@Test
Expand All @@ -90,17 +90,27 @@ public void testGetPeerList() {
}

@Test
public void testActivePeerCounter() {
public void testActivePeerCounter_with_list() {
when(dnsAddressEndpointGroup.endpoints()).thenReturn(ENDPOINT_LIST);

final List<Measurement> endpointsMeasures = MetricsTestUtil.getMeasurementList(new StringJoiner(MetricNames.DELIMITER).add(COMPONENT_SCOPE).add(COMPONENT_ID)
.add(PeerListProvider.PEER_ENDPOINTS).toString());
assertEquals(1, endpointsMeasures.size());
final Measurement endpointsMeasure = endpointsMeasures.get(0);
assertEquals(2.0, endpointsMeasure.getValue(), 0);
final ArgumentCaptor<ToDoubleFunction<DnsAddressEndpointGroup>> gaugeFunctionCaptor = ArgumentCaptor.forClass(ToDoubleFunction.class);
verify(pluginMetrics).gauge(eq(PEER_ENDPOINTS), eq(dnsAddressEndpointGroup), gaugeFunctionCaptor.capture());

final ToDoubleFunction<DnsAddressEndpointGroup> gaugeFunction = gaugeFunctionCaptor.getValue();

assertThat(gaugeFunction.applyAsDouble(dnsAddressEndpointGroup), equalTo(2.0));
}

@Test
public void testActivePeerCounter_with_single() {
when(dnsAddressEndpointGroup.endpoints()).thenReturn(Collections.singletonList(Endpoint.of(ENDPOINT_1)));
assertEquals(1.0, endpointsMeasure.getValue(), 0);

final ArgumentCaptor<ToDoubleFunction<DnsAddressEndpointGroup>> gaugeFunctionCaptor = ArgumentCaptor.forClass(ToDoubleFunction.class);
verify(pluginMetrics).gauge(eq(PEER_ENDPOINTS), eq(dnsAddressEndpointGroup), gaugeFunctionCaptor.capture());

final ToDoubleFunction<DnsAddressEndpointGroup> gaugeFunction = gaugeFunctionCaptor.getValue();

assertThat(gaugeFunction.applyAsDouble(dnsAddressEndpointGroup), equalTo(1.0));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,58 @@

package org.opensearch.dataprepper.peerforwarder.discovery;

import io.micrometer.core.instrument.Measurement;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opensearch.dataprepper.metrics.MetricNames;
import org.opensearch.dataprepper.metrics.MetricsTestUtil;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.peerforwarder.HashRing;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringJoiner;

import static org.junit.Assert.assertEquals;
import java.util.function.ToDoubleFunction;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.opensearch.dataprepper.peerforwarder.discovery.PeerListProvider.PEER_ENDPOINTS;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class StaticPeerListProviderTest {

private static final String ENDPOINT_1 = "10.10.0.1";
private static final String ENDPOINT_2 = "10.10.0.2";
private static final List<String> ENDPOINT_LIST = Arrays.asList(ENDPOINT_1, ENDPOINT_2);
private static final String COMPONENT_SCOPE = "testComponentScope";
private static final String COMPONENT_ID = "testComponentId";

@Mock
private HashRing hashRing;

@Mock
private PluginMetrics pluginMetrics;

private StaticPeerListProvider staticPeerListProvider;

@Before
@BeforeEach
public void setup() {
MetricsTestUtil.initMetrics();
pluginMetrics = PluginMetrics.fromNames(COMPONENT_ID, COMPONENT_SCOPE);
staticPeerListProvider = new StaticPeerListProvider(ENDPOINT_LIST, pluginMetrics);
}

@Test(expected = RuntimeException.class)
@Test
public void testListProviderWithEmptyList() {
new StaticPeerListProvider(Collections.emptyList(), pluginMetrics);
assertThrows(RuntimeException.class, () -> new StaticPeerListProvider(Collections.emptyList(), pluginMetrics));
}

@Test(expected = RuntimeException.class)
@Test
public void testListProviderWithNullList() {
new StaticPeerListProvider(null, pluginMetrics);
assertThrows(RuntimeException.class, () -> new StaticPeerListProvider(null, pluginMetrics));
}

@Test
Expand All @@ -65,11 +67,12 @@ public void testListProviderWithNonEmptyList() {

@Test
public void testActivePeerCounter() {
final List<Measurement> endpointsMeasures = MetricsTestUtil.getMeasurementList(
new StringJoiner(MetricNames.DELIMITER).add(COMPONENT_SCOPE).add(COMPONENT_ID).add(PeerListProvider.PEER_ENDPOINTS).toString());
assertEquals(1, endpointsMeasures.size());
final Measurement endpointsMeasure = endpointsMeasures.get(0);
assertEquals(2.0, endpointsMeasure.getValue(), 0);
final ArgumentCaptor<ToDoubleFunction<List<String>>> gaugeFunctionCaptor = ArgumentCaptor.forClass(ToDoubleFunction.class);
verify(pluginMetrics).gauge(eq(PEER_ENDPOINTS), any(List.class), gaugeFunctionCaptor.capture());

final ToDoubleFunction<List<String>> gaugeFunction = gaugeFunctionCaptor.getValue();

assertThat(gaugeFunction.applyAsDouble(ENDPOINT_LIST), equalTo(2.0));
}

@Test
Expand Down

0 comments on commit 3a96584

Please sign in to comment.