architjainjain commented on code in PR #6501: URL: https://github.com/apache/hive/pull/6501#discussion_r3718133645
########## ql/src/test/org/apache/hadoop/hive/ql/exec/tez/monitoring/yarnqueue/TestNoOpQueueMetricsCollector.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.junit.Test; + +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.assertSame; +import static org.junit.Assert.fail; + +/** + * Test cases for NoOpQueueMetricsCollector (Null Object pattern implementation). + */ +public class TestNoOpQueueMetricsCollector { + + @Test + public void testInstanceIsSingleton() { + NoOpQueueMetricsCollector instance1 = NoOpQueueMetricsCollector.INSTANCE; + NoOpQueueMetricsCollector instance2 = NoOpQueueMetricsCollector.INSTANCE; + + assertNotNull("Instance should not be null", instance1); + assertSame("INSTANCE should return same reference", instance1, instance2); + } + + @Test + public void testIsEnabledReturnsFalse() { + NoOpQueueMetricsCollector collector = NoOpQueueMetricsCollector.INSTANCE; + + assertFalse("isEnabled should return false", collector.isEnabled()); + } + + @Test + public void testGetLatestSnapshotReturnsNull() { + NoOpQueueMetricsCollector collector = NoOpQueueMetricsCollector.INSTANCE; + + assertNull("getLatestSnapshot should return null", collector.getLatestSnapshot()); + } + + @Test + public void testGetQueueNameReturnsEmptyString() { + NoOpQueueMetricsCollector collector = NoOpQueueMetricsCollector.INSTANCE; + + String queueName = collector.getQueueName(); + assertNotNull("Queue name should not be null", queueName); + assertEquals("Queue name should be empty string", "", queueName); + } + + @Test + public void testShutdownIsNoOp() { + NoOpQueueMetricsCollector collector = NoOpQueueMetricsCollector.INSTANCE; + + // Should not throw exception + collector.shutdown(); + // Collector still usable after shutdown - confirms it is truly a no-op + assertFalse("Collector should remain disabled after shutdown", collector.isEnabled()); + assertNull("Snapshot should remain null after shutdown", collector.getLatestSnapshot()); + } + + @Test + public void testShutdownIsIdempotent() { + NoOpQueueMetricsCollector collector = NoOpQueueMetricsCollector.INSTANCE; + + // Multiple calls should all be safe no-ops + collector.shutdown(); + collector.shutdown(); + collector.shutdown(); + + // Verify instance still works after shutdown calls + assertFalse("isEnabled should still return false", collector.isEnabled()); + assertNull("getLatestSnapshot should still return null", collector.getLatestSnapshot()); + } + + + @Test + public void testNullObjectPatternAllowsSafePolymorphism() { + // NoOp collector can be used wherever QueueMetricsCollector is expected + // without null checks + NoOpQueueMetricsCollector collector = NoOpQueueMetricsCollector.INSTANCE; + + // Simulate typical usage pattern + if (collector.isEnabled()) { + // This branch should never execute + fail("NoOp collector should never report as enabled"); + } 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]
