Repository: apache/kafka
File: clients/src/test/java/org/apache/kafka/clients/consumer/internals/metrics/AsyncConsumerMetricsTest.java (Java)

Code before:
import java.util.Set;

import static org.apache.kafka.clients.consumer.internals.ConsumerUtils.CONSUMER_METRIC_GROUP;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AsyncConsumerMetricsTest {
    private static final long METRIC_VALUE = 123L;

    private final Metrics metrics = new Metrics();
    private AsyncConsumerMetrics consumerMetrics;

    @AfterEach
    public void tearDown() {
        if (consumerMetrics != null) {
            consumerMetrics.close();
        }
        metrics.close();
    }

    @Test
    public void shouldMetricNames() {
        // create
        consumerMetrics = new AsyncConsumerMetrics(metrics);
        Set<MetricName> expectedConsumerMetrics = Set.of(
            metrics.metricName("time-between-network-thread-poll-avg", CONSUMER_METRIC_GROUP),
            metrics.metricName("time-between-network-thread-poll-max", CONSUMER_METRIC_GROUP),
            metrics.metricName("application-event-queue-size", CONSUMER_METRIC_GROUP),
            metrics.metricName("application-event-queue-time-avg", CONSUMER_METRIC_GROUP),
            metrics.metricName("application-event-queue-time-max", CONSUMER_METRIC_GROUP),
            metrics.metricName("application-event-queue-processing-time-avg", CONSUMER_METRIC_GROUP),
            metrics.metricName("application-event-queue-processing-time-max", CONSUMER_METRIC_GROUP),
            metrics.metricName("unsent-requests-queue-size", CONSUMER_METRIC_GROUP),
            metrics.metricName("unsent-requests-queue-time-avg", CONSUMER_METRIC_GROUP),
            metrics.metricName("unsent-requests-queue-time-max", CONSUMER_METRIC_GROUP),
            metrics.metricName("background-event-queue-size", CONSUMER_METRIC_GROUP),
            metrics.metricName("background-event-queue-time-avg", CONSUMER_METRIC_GROUP),
            metrics.metricName("background-event-queue-time-max", CONSUMER_METRIC_GROUP),
            metrics.metricName("background-event-queue-processing-time-avg", CONSUMER_METRIC_GROUP),
            metrics.metricName("background-event-queue-processing-time-max", CONSUMER_METRIC_GROUP)
        );
        expectedConsumerMetrics.forEach(
            metricName -> assertTrue(
                metrics.metrics().containsKey(metricName),
                "Missing metric: " + metricName
            )
        );

        // close
        consumerMetrics.close();

Review comment (suggestion):
I suggest renaming this to `expectedMetrics`. It is specifically NOT the expected consumer metrics :)

Code after:

import java.util.Set;
import java.util.stream.Stream;

import static org.apache.kafka.clients.consumer.internals.ConsumerUtils.CONSUMER_METRIC_GROUP;
import static org.apache.kafka.clients.consumer.internals.ConsumerUtils.CONSUMER_SHARE_METRIC_GROUP;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AsyncConsumerMetricsTest {
    private static final long METRIC_VALUE = 123L;

    private final Metrics metrics = new Metrics();
    private AsyncConsumerMetrics consumerMetrics;

    public static Stream<String> groupNameProvider() {
        return Stream.of(
            CONSUMER_METRIC_GROUP,
            CONSUMER_SHARE_METRIC_GROUP
        );
    }

    @AfterEach
    public void tearDown() {
        if (consumerMetrics != null) {
            consumerMetrics.close();
        }
        metrics.close();
    }

    @ParameterizedTest
    @MethodSource("groupNameProvider")
    public void shouldMetricNames(String groupName) {
        // create
        consumerMetrics = new AsyncConsumerMetrics(metrics, groupName);
        Set<MetricName> expectedMetrics = Set.of(
            metrics.metricName("time-between-network-thread-poll-avg", groupName),
            metrics.metricName("time-between-network-thread-poll-max", groupName),
            metrics.metricName("application-event-queue-size", groupName),
            metrics.metricName("application-event-queue-time-avg", groupName),
            metrics.metricName("application-event-queue-time-max", groupName),
            metrics.metricName("application-event-queue-processing-time-avg", groupName),
            metrics.metricName("application-event-queue-processing-time-max", groupName),
            metrics.metricName("unsent-requests-queue-size", groupName),
            metrics.metricName("unsent-requests-queue-time-avg", groupName),
            metrics.metricName("unsent-requests-queue-time-max", groupName),
            metrics.metricName("background-event-queue-size", groupName),
            metrics.metricName("background-event-queue-time-avg", groupName),
            metrics.metricName("background-event-queue-time-max", groupName),
            metrics.metricName("background-event-queue-processing-time-avg", groupName),