architjainjain commented on code in PR #6501: URL: https://github.com/apache/hive/pull/6501#discussion_r3718169789
########## ql/src/test/org/apache/hadoop/hive/ql/exec/tez/monitoring/yarnqueue/TestQueueMetricsCache.java: ########## @@ -0,0 +1,340 @@ +/* + * 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.hadoop.hive.ql.exec.tez.monitoring.yarnqueue; + +import org.apache.hadoop.yarn.api.records.QueueInfo; +import org.apache.hadoop.yarn.api.records.QueueStatistics; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +/** + * Test cases for QueueMetricsCache. + */ +public class TestQueueMetricsCache { + + @Mock + private QueueInfo mockQueueInfo; + + @Mock + private QueueStatistics mockQueueStats; + + private AutoCloseable closeable; + private QueueMetricsCache cache; + + @Before + public void setUp() { + closeable = MockitoAnnotations.openMocks(this); + cache = QueueMetricsCache.getInstance(); + // Note: We can't fully clear the cache between tests since it's a singleton, + // but we use unique queue names per test to avoid interference + } + + @After + public void tearDown() throws Exception { + if (closeable != null) { + closeable.close(); + } + } + + @Test + public void testSingletonInstanceConsistency() { + QueueMetricsCache instance1 = QueueMetricsCache.getInstance(); + QueueMetricsCache instance2 = QueueMetricsCache.getInstance(); + + assertNotNull("Instance should not be null", instance1); + assertSame("getInstance should return same instance", instance1, instance2); + } + + @Test + public void testGetReturnsNullForNonExistentQueue() { + String nonExistentQueue = "test-nonexistent-" + System.nanoTime(); + QueueMetricsState state = cache.get(nonExistentQueue); + + assertNull("Should return null for non-existent queue", state); + } + + @Test + public void testGetReturnsNullForNullQueueName() { + QueueMetricsState state = cache.get(null); + + assertNull("Should return null for null queue name", state); + } + + @Test + public void testPutCreatesNewEntry() { + setupMockQueueInfo(); + String queueName = "test-new-entry-" + System.nanoTime(); + QueueMetricsSnapshot snapshot = new QueueMetricsSnapshot(mockQueueInfo); + + // Verify queue doesn't exist yet + assertNull("Queue should not exist initially", cache.get(queueName)); + + // Put creates new entry + cache.put(queueName, snapshot, 5000L); + + QueueMetricsState state = cache.get(queueName); + assertNotNull("Queue state should exist after put", state); + assertNotNull("Snapshot should be available", state.getSnapshot()); + assertEquals("Memory used should match", 1.0f, state.getSnapshot().getMemoryUsedGB(), 0.01f); + } + + @Test + public void testPutUpdatesExistingEntry() { + setupMockQueueInfo(); Review Comment: done -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
