d4v1de commented on code in PR #20397: URL: https://github.com/apache/kafka/pull/20397#discussion_r2496473371
########## server/src/test/java/org/apache/kafka/server/metrics/MetricsVerbosityControllerTest.java: ########## @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.server.metrics; + +import org.apache.kafka.common.config.AbstractConfig; +import org.apache.kafka.common.config.ConfigDef; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class MetricsVerbosityControllerTest { + + private static AbstractConfig configWith(String raw) { + ConfigDef def = new ConfigDef() + .define(MetricConfigs.METRICS_VERBOSITY_CONFIG, + ConfigDef.Type.STRING, + MetricConfigs.METRICS_VERBOSITY_DEFAULT, + ConfigDef.Importance.LOW, + MetricConfigs.METRICS_VERBOSITY_DOC); + return new AbstractConfig(def, Map.of(MetricConfigs.METRICS_VERBOSITY_CONFIG, raw)); + } + + @BeforeEach + public void resetCache() { + // Ensure a known baseline for the static cache between tests + AbstractConfig reset = configWith("[]"); + MetricsVerbosityController.shouldEmitPartitionMetric(reset, "AnyMetric", "any-topic"); + } + + @Test + public void testEmptyConfigEmitsFalse() { + AbstractConfig conf = configWith("[]"); + assertFalse(MetricsVerbosityController.shouldEmitPartitionMetric(conf, "BytesInPerSec", "car")); + } + + @Test + public void testInvalidJsonEmitsFalse() { + AbstractConfig conf = configWith("not json"); + assertFalse(MetricsVerbosityController.shouldEmitPartitionMetric(conf, "BytesInPerSec", "car")); + } + + @Test + public void testLowLevelIgnored() { + String raw = "[ { \"level\": \"low\", \"names\": \"Bytes.*\", \"filters\": [{\"topics\":[\"car\"]}] } ]"; + AbstractConfig conf = configWith(raw); + assertFalse(MetricsVerbosityController.shouldEmitPartitionMetric(conf, "BytesInPerSec", "car")); + } + + @Test + public void testHighLevelNoTopicsDoesNotMatch() { + String raw = "[ { \"level\": \"high\", \"names\": \"Bytes.*\" } ]"; + AbstractConfig conf = configWith(raw); + assertFalse(MetricsVerbosityController.shouldEmitPartitionMetric(conf, "BytesInPerSec", "car")); + } + + @Test + public void testHighLevelWithTopicsAndNamePatternMatches() { Review Comment: Have you considered using JUnit 5 Parameterized Tests? Example: ```java @ParameterizedTest @CsvSource({ "BytesInPerSec, car, true", "BytesOutPerSec, bus, true", "BytesInPerSec, plane, false", "MessagesInPerSec, car, false" }) public void testHighLevelWithTopicsAndNamePatternMatches(String metricName, String topic, boolean expected) { String raw = "[ { \"level\": \"high\", \"names\": \"Bytes.*\", \"filters\": [{\"topics\":[\"car\",\"bus\"]}] } ]"; AbstractConfig conf = configWith(raw); boolean result = MetricsVerbosityController.shouldEmitPartitionMetric(conf, metricName, topic); assertEquals(expected, result, String.format("Expected %s for metric '%s' on topic '%s'", expected, metricName, topic)); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
