From 5484ab5eb746f46369441803165c072005993bbf Mon Sep 17 00:00:00 2001 From: Arjun Singh Bora Date: Thu, 21 Nov 2024 22:51:57 -0800 Subject: [PATCH] add tests --- .../com/linkedin/venice/utils/UtilsTest.java | 118 +++++++++++++++++- .../pushmonitor/AbstractPushMonitor.java | 2 +- .../control/RealTimeTopicSwitcherTest.java | 1 - 3 files changed, 115 insertions(+), 6 deletions(-) diff --git a/internal/venice-common/src/test/java/com/linkedin/venice/utils/UtilsTest.java b/internal/venice-common/src/test/java/com/linkedin/venice/utils/UtilsTest.java index 79f2fca4d5..09a6b7a7f2 100644 --- a/internal/venice-common/src/test/java/com/linkedin/venice/utils/UtilsTest.java +++ b/internal/venice-common/src/test/java/com/linkedin/venice/utils/UtilsTest.java @@ -1,16 +1,19 @@ package com.linkedin.venice.utils; -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.expectThrows; -import static org.testng.Assert.fail; +import static org.mockito.Mockito.*; +import static org.testng.Assert.*; import com.linkedin.venice.exceptions.VeniceException; +import com.linkedin.venice.meta.HybridStoreConfig; +import com.linkedin.venice.meta.Store; +import com.linkedin.venice.meta.StoreInfo; +import com.linkedin.venice.meta.Version; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -19,6 +22,7 @@ import java.util.TreeMap; import org.testng.Assert; import org.testng.annotations.Test; +import org.testng.collections.Lists; /** @@ -229,4 +233,110 @@ public void testResolveKafkaUrlForSepTopic() { Assert.assertEquals(Utils.resolveKafkaUrlForSepTopic(originalKafkaUrlForSep), originalKafkaUrl); Assert.assertEquals(Utils.resolveKafkaUrlForSepTopic(originalKafkaUrl), originalKafkaUrl); } + + @Test + void testGetRealTimeTopicNameWithStore() { + Store mockStore = mock(Store.class); + List mockVersions = Collections.singletonList(mock(Version.class)); + HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class); + + when(mockStore.getName()).thenReturn("TestStore"); + when(mockStore.getVersions()).thenReturn(mockVersions); + when(mockStore.getCurrentVersion()).thenReturn(1); + when(mockStore.getHybridStoreConfig()).thenReturn(mockHybridConfig); + + when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic"); + + String result = Utils.getRealTimeTopicName(mockStore); + assertEquals("RealTimeTopic", result); + } + + @Test + void testGetRealTimeTopicNameWithStoreInfo() { + StoreInfo mockStoreInfo = mock(StoreInfo.class); + List mockVersions = Collections.singletonList(mock(Version.class)); + HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class); + + when(mockStoreInfo.getName()).thenReturn("TestStore"); + when(mockStoreInfo.getVersions()).thenReturn(mockVersions); + when(mockStoreInfo.getCurrentVersion()).thenReturn(1); + when(mockStoreInfo.getHybridStoreConfig()).thenReturn(mockHybridConfig); + + when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic"); + + String result = Utils.getRealTimeTopicName(mockStoreInfo); + assertEquals("RealTimeTopic", result); + } + + @Test + void testGetRealTimeTopicNameWithHybridConfig() { + HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class); + + when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic"); + String result = Utils.getRealTimeTopicName("TestStore", Collections.EMPTY_LIST, 1, mockHybridConfig); + + assertEquals("RealTimeTopic", result); + } + + @Test + void testGetRealTimeTopicNameWithoutHybridConfig() { + String result = Utils.getRealTimeTopicName("TestStore", Collections.EMPTY_LIST, 0, null); + assertEquals("TestStore" + Version.REAL_TIME_TOPIC_SUFFIX, result); + } + + @Test + void testGetRealTimeTopicNameWithConflictingVersions() { + Version mockVersion1 = mock(Version.class); + Version mockVersion2 = mock(Version.class); + HybridStoreConfig mockConfig1 = mock(HybridStoreConfig.class); + HybridStoreConfig mockConfig2 = mock(HybridStoreConfig.class); + + when(mockVersion1.isHybrid()).thenReturn(true); + when(mockVersion2.isHybrid()).thenReturn(true); + when(mockVersion1.getHybridStoreConfig()).thenReturn(mockConfig1); + when(mockVersion2.getHybridStoreConfig()).thenReturn(mockConfig2); + when(mockConfig1.getRealTimeTopicName()).thenReturn("RealTimeTopic1"); + when(mockConfig2.getRealTimeTopicName()).thenReturn("RealTimeTopic2"); + + String result = Utils.getRealTimeTopicName("TestStore", Lists.newArrayList(mockVersion1, mockVersion2), 1, null); + assertTrue(result.equals("RealTimeTopic1") || result.equals("RealTimeTopic2")); + } + + @Test + void testGetRealTimeTopicNameWithExceptionHandling() { + Version mockVersion1 = mock(Version.class); + Version mockVersion2 = mock(Version.class); + + when(mockVersion1.isHybrid()).thenReturn(true); + when(mockVersion1.getHybridStoreConfig()).thenThrow(new VeniceException("Test Exception")); + + when(mockVersion2.isHybrid()).thenReturn(false); + + String result = Utils.getRealTimeTopicName("TestStore", Lists.newArrayList(mockVersion1, mockVersion2), 1, null); + assertEquals("TestStore" + Version.REAL_TIME_TOPIC_SUFFIX, result); + } + + @Test + void testGetRealTimeTopicNameWithVersion() { + Version mockVersion = mock(Version.class); + HybridStoreConfig mockHybridConfig = mock(HybridStoreConfig.class); + + when(mockVersion.getHybridStoreConfig()).thenReturn(mockHybridConfig); + when(mockVersion.getStoreName()).thenReturn("TestStore"); + when(mockHybridConfig.getRealTimeTopicName()).thenReturn("RealTimeTopic"); + + String result = Utils.getRealTimeTopicName(mockVersion); + assertEquals("RealTimeTopic", result); + } + + @Test(expectedExceptions = VeniceException.class) + void testGetRealTimeTopicNameWithNonHybridVersion() { + // Mocking the Version object + Version mockVersion = mock(Version.class); + + // Mock setup to trigger the exception path + when(mockVersion.getHybridStoreConfig()).thenReturn(null); + + Utils.getRealTimeTopicName(mockVersion); + } } diff --git a/services/venice-controller/src/main/java/com/linkedin/venice/pushmonitor/AbstractPushMonitor.java b/services/venice-controller/src/main/java/com/linkedin/venice/pushmonitor/AbstractPushMonitor.java index c545e0fd5c..891f852e14 100644 --- a/services/venice-controller/src/main/java/com/linkedin/venice/pushmonitor/AbstractPushMonitor.java +++ b/services/venice-controller/src/main/java/com/linkedin/venice/pushmonitor/AbstractPushMonitor.java @@ -933,7 +933,7 @@ protected void checkWhetherToStartBufferReplayForHybrid(OfflinePushStatus offlin try { String newStatusDetails; realTimeTopicSwitcher.switchToRealTimeTopic( - Utils.getRealTimeTopicName(version), + Utils.getRealTimeTopicName(store), offlinePushStatus.getKafkaTopic(), store, aggregateRealTimeSourceKafkaUrl, diff --git a/services/venice-controller/src/test/java/com/linkedin/venice/ingestion/control/RealTimeTopicSwitcherTest.java b/services/venice-controller/src/test/java/com/linkedin/venice/ingestion/control/RealTimeTopicSwitcherTest.java index fab744a712..19d3485b01 100644 --- a/services/venice-controller/src/test/java/com/linkedin/venice/ingestion/control/RealTimeTopicSwitcherTest.java +++ b/services/venice-controller/src/test/java/com/linkedin/venice/ingestion/control/RealTimeTopicSwitcherTest.java @@ -139,7 +139,6 @@ public void testSendVersionSwap() { Store mockStore = mock(Store.class); when(mockStore.getName()).thenReturn(storeName); - String realTimeTopicName = Utils.getRealTimeTopicName(mockStore); Version version1 = new VersionImpl(storeName, 1, "push1"); Version version2 = new VersionImpl(storeName, 2, "push2"); version2.setViewConfigs(viewConfigs);