Skip to content

Commit

Permalink
Try to fix unit test for DumpAllProcessorTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Jan 13, 2025
1 parent 74f1388 commit dd546ff
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.BeanUtils;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
Expand All @@ -70,41 +72,41 @@ class DumpAllProcessorTest {

MockedStatic<DynamicDataSource> dynamicDataSourceMockedStatic;

MockedStatic<PropertyUtil> propertyUtilMockedStatic;

@Mock
ConfigInfoPersistService configInfoPersistService;

MockedStatic<EnvUtil> envUtilMockedStatic;
MockEnvironment environment;

private String mockMem = "tmpmocklimitfile.txt";

int cachedAllDumpPageSize;

@BeforeEach
void init() throws Exception {
environment = new MockEnvironment();
EnvUtil.setEnvironment(environment);
environment.setProperty("memory_limit_file_path", mockMem);
EnvUtil.setNacosHomePath(System.getProperty("user.home"));
environment.setProperty(CommonConstant.NACOS_PLUGIN_DATASOURCE_LOG, "false");
dynamicDataSourceMockedStatic = Mockito.mockStatic(DynamicDataSource.class);
envUtilMockedStatic = Mockito.mockStatic(EnvUtil.class);
propertyUtilMockedStatic = Mockito.mockStatic(PropertyUtil.class);
propertyUtilMockedStatic.when(PropertyUtil::getAllDumpPageSize).thenReturn(100);
cachedAllDumpPageSize = PropertyUtil.getAllDumpPageSize();
AtomicInteger allDumpPageSize = (AtomicInteger) ReflectionTestUtils.getField(PropertyUtil.class,
"allDumpPageSize");
allDumpPageSize.set(100);
dumpAllProcessor = new DumpAllProcessor(configInfoPersistService);
when(EnvUtil.getNacosHome()).thenReturn(System.getProperty("user.home"));
when(EnvUtil.getProperty(eq(CommonConstant.NACOS_PLUGIN_DATASOURCE_LOG), eq(Boolean.class), eq(false))).thenReturn(false);
dynamicDataSourceMockedStatic.when(DynamicDataSource::getInstance).thenReturn(dynamicDataSource);

when(dynamicDataSource.getDataSource()).thenReturn(dataSourceService);

dumpService = new ExternalDumpService(configInfoPersistService, null, null, configInfoGrayPersistService, null);

dumpAllProcessor = new DumpAllProcessor(configInfoPersistService);
envUtilMockedStatic.when(() -> EnvUtil.getProperty(eq("memory_limit_file_path"), eq("/sys/fs/cgroup/memory/memory.limit_in_bytes")))
.thenReturn(mockMem);

}

@AfterEach
void after() throws Exception {
AtomicInteger allDumpPageSize = (AtomicInteger) ReflectionTestUtils.getField(PropertyUtil.class,
"allDumpPageSize");
allDumpPageSize.set(cachedAllDumpPageSize);
dynamicDataSourceMockedStatic.close();
envUtilMockedStatic.close();
propertyUtilMockedStatic.close();
}

private ConfigInfoWrapper createNewConfig(int id) {
Expand Down Expand Up @@ -137,7 +139,8 @@ void testDumpAllOnStartUp() throws Exception {
page.setPageItems(list);

Mockito.when(configInfoPersistService.findConfigMaxId()).thenReturn(2L);
Mockito.when(configInfoPersistService.findAllConfigInfoFragment(0, PropertyUtil.getAllDumpPageSize(), true)).thenReturn(page);
Mockito.when(configInfoPersistService.findAllConfigInfoFragment(0, PropertyUtil.getAllDumpPageSize(), true))
.thenReturn(page);

// For config 1, assign a latter time, to make sure that it would be updated.
// For config 2, assign an earlier time, to make sure that it is not be updated.
Expand All @@ -146,10 +149,12 @@ void testDumpAllOnStartUp() throws Exception {
long latterTimestamp = timestamp + 999;
long earlierTimestamp = timestamp - 999;
String encryptedDataKey = "testEncryptedDataKey";
ConfigCacheService.dumpWithMd5(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant(),
configInfoWrapper1.getContent(), md51, latterTimestamp, "json", encryptedDataKey);
ConfigCacheService.dumpWithMd5(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant(),
configInfoWrapper2.getContent(), md52, earlierTimestamp, "json", encryptedDataKey);
ConfigCacheService.dumpWithMd5(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant(), configInfoWrapper1.getContent(), md51, latterTimestamp, "json",
encryptedDataKey);
ConfigCacheService.dumpWithMd5(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant(), configInfoWrapper2.getContent(), md52, earlierTimestamp, "json",
encryptedDataKey);

DumpAllTask dumpAllTask = new DumpAllTask(true);

Expand All @@ -158,24 +163,29 @@ void testDumpAllOnStartUp() throws Exception {

//Check cache
CacheItem contentCache1 = ConfigCacheService.getContentCache(
GroupKey2.getKey(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant()));
GroupKey2.getKey(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant()));
assertEquals(md51, contentCache1.getConfigCache().getMd5());
// check if config1 is updated
assertTrue(timestamp < contentCache1.getConfigCache().getLastModifiedTs());
//check disk
String contentFromDisk1 = ConfigDiskServiceFactory.getInstance()
.getContent(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant());
.getContent(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant());
assertEquals(configInfoWrapper1.getContent(), contentFromDisk1);

//Check cache
CacheItem contentCache2 = ConfigCacheService.getContentCache(
GroupKey2.getKey(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant()));
assertEquals(MD5Utils.md5Hex(configInfoWrapper2.getContent(), "UTF-8"), contentCache2.getConfigCache().getMd5());
GroupKey2.getKey(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant()));
assertEquals(MD5Utils.md5Hex(configInfoWrapper2.getContent(), "UTF-8"),
contentCache2.getConfigCache().getMd5());
// check if config2 is updated
assertEquals(timestamp, contentCache2.getConfigCache().getLastModifiedTs());
//check disk
String contentFromDisk2 = ConfigDiskServiceFactory.getInstance()
.getContent(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant());
.getContent(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant());
assertEquals(configInfoWrapper2.getContent(), contentFromDisk2);
}

Expand All @@ -197,19 +207,22 @@ void testDumpAllOnCheckAll() throws Exception {
page.setPageItems(list);

Mockito.when(configInfoPersistService.findConfigMaxId()).thenReturn(2L);
Mockito.when(configInfoPersistService.findAllConfigInfoFragment(0, PropertyUtil.getAllDumpPageSize(), false)).thenReturn(page);
Mockito.when(configInfoPersistService.findAllConfigInfoFragment(0, PropertyUtil.getAllDumpPageSize(), false))
.thenReturn(page);

ConfigInfoWrapper configInfoWrapperSingle1 = new ConfigInfoWrapper();
BeanUtils.copyProperties(configInfoWrapper1, configInfoWrapperSingle1);
configInfoWrapperSingle1.setContent("content123456");
Mockito.when(configInfoPersistService.findConfigInfo(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant())).thenReturn(configInfoWrapperSingle1);
Mockito.when(
configInfoPersistService.findConfigInfo(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant())).thenReturn(configInfoWrapperSingle1);

ConfigInfoWrapper configInfoWrapperSingle2 = new ConfigInfoWrapper();
BeanUtils.copyProperties(configInfoWrapper2, configInfoWrapperSingle2);
configInfoWrapperSingle2.setContent("content123456222");
Mockito.when(configInfoPersistService.findConfigInfo(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant())).thenReturn(configInfoWrapperSingle2);
Mockito.when(
configInfoPersistService.findConfigInfo(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant())).thenReturn(configInfoWrapperSingle2);

// For config 1, assign a latter time, to make sure that it would not be updated.
// For config 2, assign an earlier time, to make sure that it would be updated.
Expand All @@ -218,10 +231,12 @@ void testDumpAllOnCheckAll() throws Exception {
long latterTimestamp = timestamp + 999;
long earlierTimestamp = timestamp - 999;
String encryptedDataKey = "testEncryptedDataKey";
ConfigCacheService.dumpWithMd5(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant(),
configInfoWrapper1.getContent(), md51, latterTimestamp, "json", encryptedDataKey);
ConfigCacheService.dumpWithMd5(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant(),
configInfoWrapper2.getContent(), md52, earlierTimestamp, "json", encryptedDataKey);
ConfigCacheService.dumpWithMd5(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant(), configInfoWrapper1.getContent(), md51, latterTimestamp, "json",
encryptedDataKey);
ConfigCacheService.dumpWithMd5(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant(), configInfoWrapper2.getContent(), md52, earlierTimestamp, "json",
encryptedDataKey);

DumpAllTask dumpAllTask = new DumpAllTask(false);
boolean process = dumpAllProcessor.process(dumpAllTask);
Expand All @@ -230,24 +245,29 @@ void testDumpAllOnCheckAll() throws Exception {

//Check cache
CacheItem contentCache1 = ConfigCacheService.getContentCache(
GroupKey2.getKey(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant()));
GroupKey2.getKey(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant()));
// check if config1 is not updated
assertEquals(md51, contentCache1.getConfigCache().getMd5());
assertEquals(latterTimestamp, contentCache1.getConfigCache().getLastModifiedTs());
//check disk
String contentFromDisk1 = ConfigDiskServiceFactory.getInstance()
.getContent(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(), configInfoWrapper1.getTenant());
.getContent(configInfoWrapper1.getDataId(), configInfoWrapper1.getGroup(),
configInfoWrapper1.getTenant());
assertEquals(configInfoWrapper1.getContent(), contentFromDisk1);

//Check cache
CacheItem contentCache2 = ConfigCacheService.getContentCache(
GroupKey2.getKey(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant()));
GroupKey2.getKey(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant()));
// check if config2 is updated
assertEquals(MD5Utils.md5Hex(configInfoWrapperSingle2.getContent(), "UTF-8"), contentCache2.getConfigCache().getMd5());
assertEquals(MD5Utils.md5Hex(configInfoWrapperSingle2.getContent(), "UTF-8"),
contentCache2.getConfigCache().getMd5());
assertEquals(configInfoWrapper2.getLastModified(), contentCache2.getConfigCache().getLastModifiedTs());
//check disk
String contentFromDisk2 = ConfigDiskServiceFactory.getInstance()
.getContent(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(), configInfoWrapper2.getTenant());
.getContent(configInfoWrapper2.getDataId(), configInfoWrapper2.getGroup(),
configInfoWrapper2.getTenant());
assertEquals(configInfoWrapperSingle2.getContent(), contentFromDisk2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void testUpdate() throws Exception {
e.printStackTrace();
fail(e.getMessage());
}
TimeUnit.SECONDS.sleep(1);
TimeUnit.MILLISECONDS.sleep(1200);
assertEquals(UpdateServiceTraceEvent.class, eventReceivedClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ void testGetLocalAddress() {
System.setProperty(NACOS_SERVER_IP, "1.1.1.1");
System.setProperty(Constants.AUTO_REFRESH_TIME, "100");
try {
assertEquals("1.1.1.1:8848", EnvUtil.getLocalAddress());
EnvUtil.setLocalAddress("testLocalAddress:8848");
assertEquals("testLocalAddress:8848", EnvUtil.getLocalAddress());
} finally {
Expand Down

0 comments on commit dd546ff

Please sign in to comment.