architjainjain commented on code in PR #6501:
URL: https://github.com/apache/hive/pull/6501#discussion_r3718341370


##########
ql/src/test/org/apache/hadoop/hive/ql/exec/tez/monitoring/yarnqueue/TestQueueMetricsState.java:
##########
@@ -0,0 +1,322 @@
+/*
+ * 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.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit tests for QueueMetricsState - tests state management logic in 
isolation.
+ * Tests interval registration, circuit breaker, refresh locking, and other 
state logic.
+ */
+public class TestQueueMetricsState {
+
+  @Mock
+  private QueueInfo mockQueueInfo;
+
+  @Mock
+  private QueueStatistics mockQueueStats;
+
+
+  @Before
+  public void setUp() {
+    MockitoAnnotations.openMocks(this);
+    setupMockQueueInfo();
+  }
+
+  private void setupMockQueueInfo() {
+    when(mockQueueStats.getAllocatedMemoryMB()).thenReturn(1024L);
+    when(mockQueueStats.getAvailableMemoryMB()).thenReturn(1024L);
+    when(mockQueueStats.getAllocatedVCores()).thenReturn(4L);
+    when(mockQueueStats.getAvailableVCores()).thenReturn(4L);
+    when(mockQueueStats.getNumAppsRunning()).thenReturn(1L);
+    when(mockQueueStats.getNumAppsPending()).thenReturn(0L);
+    when(mockQueueStats.getAllocatedContainers()).thenReturn(2L);
+    when(mockQueueStats.getPendingContainers()).thenReturn(0L);
+    when(mockQueueInfo.getQueueStatistics()).thenReturn(mockQueueStats);
+    when(mockQueueInfo.getCapacity()).thenReturn(0.5f);
+    when(mockQueueInfo.getCurrentCapacity()).thenReturn(0.25f);
+  }
+
+  @Test
+  public void testConstructorWithNullSnapshot() {
+    QueueMetricsState state = new QueueMetricsState(null, 5000L);
+
+    assertNull("Snapshot should be null when constructed with null", 
state.getSnapshot());
+    assertEquals("Min interval should be set", 5000L, 
state.getMinRefreshIntervalMs());
+  }
+
+  @Test
+  public void testConstructorWithSnapshot() {
+    QueueMetricsSnapshot snapshot = new QueueMetricsSnapshot(mockQueueInfo);
+    QueueMetricsState state = new QueueMetricsState(snapshot, 10000L);
+
+    assertNotNull("Snapshot should not be null", state.getSnapshot());
+    assertEquals("Min interval should be set", 10000L, 
state.getMinRefreshIntervalMs());
+  }
+
+  @Test
+  public void testGetAgeMsReturnsLargeValueInitially() {
+    QueueMetricsState state = new QueueMetricsState(null, 5000L);
+
+    long age = state.getAgeMs();
+
+    // Age should be very large when lastWriteTime = 0 (epoch)
+    assertTrue("Age should be > 1 year in ms", age > 365L * 24 * 60 * 60 * 
1000);
+  }
+
+  @Test
+  public void testApplySnapshotUpdatesSnapshot() {
+    QueueMetricsState state = new QueueMetricsState(null, 10000L);
+    assertNull("Initial snapshot should be null", state.getSnapshot());
+
+    QueueMetricsSnapshot snapshot = new QueueMetricsSnapshot(mockQueueInfo);
+    state.applySnapshot(snapshot, 5000L);
+
+    assertNotNull("Snapshot should be updated", state.getSnapshot());
+    assertEquals("Memory should match", 1.0f, 
state.getSnapshot().getMemoryUsedGB(), 0.01f);
+  }
+
+  @Test
+  public void testApplySnapshotReducesAgeMs() {
+    QueueMetricsState state = new QueueMetricsState(null, 5000L);
+    long initialAge = state.getAgeMs();
+
+    // Spin-wait up to 200ms to ensure time has passed so the age comparison 
is meaningful
+    long deadline = System.currentTimeMillis() + 200;
+    while (state.getAgeMs() <= initialAge && System.currentTimeMillis() < 
deadline) {
+      Thread.onSpinWait(); // Hint to JVM that this is a spin-wait loop
+    }
+
+    QueueMetricsSnapshot snapshot = new QueueMetricsSnapshot(mockQueueInfo);
+    state.applySnapshot(snapshot, 5000L);
+
+    long newAge = state.getAgeMs();
+    assertTrue("Age should be much smaller after apply", newAge < initialAge);
+    assertTrue("Age should be recent (< 1s)", newAge < 1000);
+  }
+
+  @Test
+  public void testApplySnapshotUpdatesMinRefreshInterval() {
+    QueueMetricsState state = new QueueMetricsState(null, 10000L);
+    assertEquals("Initial min interval", 10000L, 
state.getMinRefreshIntervalMs());

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]

Reply via email to