fix: use nacos properties in CacheDirUtil (#12186)

This commit is contained in:
shalk(xiao kun) 2024-06-12 09:26:59 +08:00 committed by GitHub
parent d68b904f93
commit 7590a4eb12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -49,7 +49,7 @@ public class CacheDirUtil {
*/
public static String initCacheDir(String namespace, NacosClientProperties properties) {
String jmSnapshotPath = System.getProperty(JM_SNAPSHOT_PATH_PROPERTY);
String jmSnapshotPath = properties.getProperty(JM_SNAPSHOT_PATH_PROPERTY);
String namingCacheRegistryDir = "";
if (properties.getProperty(PropertyKeyConst.NAMING_CACHE_REGISTRY_DIR) != null) {
@ -62,7 +62,7 @@ public class CacheDirUtil {
+ FILE_PATH_NAMING + File.separator + namespace;
} else {
cacheDir =
System.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir
properties.getProperty(USER_HOME_PROPERTY) + File.separator + FILE_PATH_NACOS + namingCacheRegistryDir
+ File.separator + FILE_PATH_NAMING + File.separator + namespace;
}

View File

@ -38,6 +38,14 @@ class CacheDirUtilTest {
assertEquals("/home/admin/nacos/naming/test", actual);
}
@Test
void testInitCacheDirWithDefaultRootAndWithoutCache2() {
NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive();
properties.setProperty("user.home", "/home/test");
String actual = CacheDirUtil.initCacheDir("test", properties);
assertEquals("/home/test/nacos/naming/test", actual);
}
@Test
void testInitCacheDirWithDefaultRootAndWithCache() {
System.setProperty("user.home", "/home/admin");
@ -54,6 +62,14 @@ class CacheDirUtilTest {
assertEquals("/home/snapshot/nacos/naming/test", actual);
}
@Test
void testInitCacheDirWithJmSnapshotPathRootAndWithoutCache2() {
NacosClientProperties properties = NacosClientProperties.PROTOTYPE.derive();
properties.setProperty("JM.SNAPSHOT.PATH", "/home/custom/snapshot");
String actual = CacheDirUtil.initCacheDir("test", properties);
assertEquals("/home/custom/snapshot/nacos/naming/test", actual);
}
@Test
void testInitCacheDirWithJmSnapshotPathRootAndWithCache() {
System.setProperty("user.home", "/home/snapshot");
@ -62,4 +78,4 @@ class CacheDirUtilTest {
String actual = CacheDirUtil.initCacheDir("test", properties);
assertEquals("/home/snapshot/nacos/custom/naming/test", actual);
}
}
}