From 0f7d2a33dffbbedf526efc82bed65dd7b17cd413 Mon Sep 17 00:00:00 2001 From: jason <2353220944@qq.com> Date: Sun, 22 Dec 2024 13:33:32 +0800 Subject: [PATCH] add UnknownSync action --- .../internals/RemoteConfigRepository.java | 6 ++++ .../internals/RemoteConfigRepositoryTest.java | 31 +++++++++++++++++++ .../apollo/core/enums/ConfigSyncType.java | 2 -- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java index 41cf9cd0..199ed21d 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/internals/RemoteConfigRepository.java @@ -268,7 +268,13 @@ private ApolloConfig loadApolloConfig() { (previousConfig != null) ? previousConfig.getConfigurations() : null; result.setConfigurations( mergeConfigurations(previousConfigurations, result.getConfigurationChanges())); + } else if (configSyncType == ConfigSyncType.UNKNOWN) { + String message = String.format( + "Apollo Config Sync type invalid - configSyncType: %s", + result.getConfigSyncType()); + throw new ApolloConfigException(message, exception); } + } logger.debug("Loaded config for {}: {}", m_namespace, result); diff --git a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java index fc49ebfe..dac8050a 100644 --- a/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java +++ b/apollo-client/src/test/java/com/ctrip/framework/apollo/internals/RemoteConfigRepositoryTest.java @@ -274,6 +274,24 @@ public void testMergeConfigurationWithChangesIsNULL() throws Exception { assertEquals(value2, result.get(key2)); } + @Test(expected = ApolloConfigException.class) + public void testGetRemoteConfigWithUnknownSync() throws Exception { + + ApolloConfig someApolloConfigWithUnknownSync = assembleApolloConfigWithUnknownSync( + new ArrayList<>()); + + when(someResponse.getStatusCode()).thenReturn(200); + when(someResponse.getBody()).thenReturn(someApolloConfigWithUnknownSync); + + RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someAppId, + someNamespace); + + //must stop the long polling before exception occurred + remoteConfigLongPollService.stopLongPollingRefresh(); + + remoteConfigRepository.getConfig(); + } + @Test public void testLoadConfigWithOrderedProperties() throws Exception { String someKey = "someKey"; @@ -504,6 +522,19 @@ private ApolloConfig assembleApolloConfigWithIncrementalSync( return apolloConfig; } + private ApolloConfig assembleApolloConfigWithUnknownSync( + List configurationChanges) { + String someAppId = "appId"; + String someClusterName = "cluster"; + String someReleaseKey = "1"; + ApolloConfig apolloConfig = + new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey); + + apolloConfig.setConfigSyncType(ConfigSyncType.UNKNOWN.getValue()); + apolloConfig.setConfigurationChanges(configurationChanges); + return apolloConfig; + } + public static class MockConfigUtil extends ConfigUtil { @Override diff --git a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java index 4b151441..07e72aac 100644 --- a/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java +++ b/apollo-core/src/main/java/com/ctrip/framework/apollo/core/enums/ConfigSyncType.java @@ -37,8 +37,6 @@ public enum ConfigSyncType { * * @param value the string that matches * @return the matching {@link ConfigSyncType} - * @throws IllegalArgumentException in case the value is empty or there is no - * matching {@link ConfigSyncType} */ public static ConfigSyncType fromString(String value) { if (StringUtils.isEmpty(value)) {