apoorvmittal10 commented on code in PR #14699:
URL: https://github.com/apache/kafka/pull/14699#discussion_r1395166691


##########
core/src/test/java/kafka/server/ClientMetricsManagerTest.java:
##########
@@ -0,0 +1,921 @@
+/*
+ * 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 kafka.server;
+
+import kafka.metrics.ClientMetricsConfigs;
+import kafka.metrics.ClientMetricsInstance;
+import kafka.metrics.ClientMetricsTestUtils;
+import kafka.server.ClientMetricsManager.SubscriptionInfo;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.message.GetTelemetrySubscriptionsRequestData;
+import org.apache.kafka.common.message.PushTelemetryRequestData;
+import org.apache.kafka.common.protocol.Errors;
+import org.apache.kafka.common.record.CompressionType;
+import org.apache.kafka.common.requests.GetTelemetrySubscriptionsRequest;
+import org.apache.kafka.common.requests.GetTelemetrySubscriptionsResponse;
+import org.apache.kafka.common.requests.PushTelemetryRequest;
+import org.apache.kafka.common.requests.PushTelemetryRequest.Builder;
+import org.apache.kafka.common.requests.PushTelemetryResponse;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.net.UnknownHostException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class ClientMetricsManagerTest {
+
+    private ClientMetricsManager clientMetricsManager;
+
+    @BeforeEach
+    public void setUp() {
+        clientMetricsManager = new ClientMetricsManager();
+    }
+
+    @Test
+    public void testUpdateSubscription() {
+        assertTrue(clientMetricsManager.subscriptions().isEmpty());
+
+        long previousEpoch = 
clientMetricsManager.lastSubscriptionUpdateEpoch();
+        clientMetricsManager.updateSubscription("sub-1", 
ClientMetricsTestUtils.defaultProperties());
+
+        assertEquals(1, clientMetricsManager.subscriptions().size());
+        assertNotNull(clientMetricsManager.subscriptionInfo("sub-1"));
+
+        SubscriptionInfo subscriptionInfo = 
clientMetricsManager.subscriptionInfo("sub-1");
+        Set<String> metrics = subscriptionInfo.metrics();
+
+        // Validate metrics.
+        assertEquals(ClientMetricsTestUtils.DEFAULT_METRICS.split(",").length, 
metrics.size());
+        
Arrays.stream(ClientMetricsTestUtils.DEFAULT_METRICS.split(",")).forEach(metric 
->
+            assertTrue(metrics.contains(metric)));
+        // Validate push interval.
+        
assertEquals(ClientMetricsTestUtils.defaultProperties().getProperty(ClientMetricsConfigs.PUSH_INTERVAL_MS),
+            String.valueOf(subscriptionInfo.intervalMs()));
+
+        // Validate match patterns.
+        
assertEquals(ClientMetricsTestUtils.DEFAULT_CLIENT_MATCH_PATTERNS.size(),
+            subscriptionInfo.matchPattern().size());
+        ClientMetricsTestUtils.DEFAULT_CLIENT_MATCH_PATTERNS.forEach(pattern 
-> {
+            String[] split = pattern.split("=");
+            assertTrue(subscriptionInfo.matchPattern().containsKey(split[0]));
+            assertEquals(split[1], 
subscriptionInfo.matchPattern().get(split[0]).pattern());
+        });
+        assertTrue(clientMetricsManager.lastSubscriptionUpdateEpoch() > 
previousEpoch);
+    }
+
+    @Test
+    public void testUpdateSubscriptionWithEmptyProperties() {
+        assertTrue(clientMetricsManager.subscriptions().isEmpty());
+        long previousEpoch = 
clientMetricsManager.lastSubscriptionUpdateEpoch();
+        clientMetricsManager.updateSubscription("sub-1", new Properties());
+        // No subscription should be added as the properties are empty.
+        assertEquals(0, clientMetricsManager.subscriptions().size());
+        assertEquals(previousEpoch, 
clientMetricsManager.lastSubscriptionUpdateEpoch());

Review Comment:
   `clientMetricsManager` is reset to new ClientMetricsManager before each test 
so parallel execution of tests should not affect individual tests. In this 
case, we are testing that empty properties request should not make any changes 
    in the subscription and subscription update time as empty properties 
request should be ignored if no existing subscription with same name exists. 
Please let me know if I missed anything here. 



-- 
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: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to