YongGoose commented on code in PR #7345: URL: https://github.com/apache/incubator-seata/pull/7345#discussion_r2097138312
########## discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/MultiRegistryFactory.java: ########## @@ -48,26 +49,36 @@ public static List<RegistryService> getInstances() { private static List<RegistryService> buildRegistryServices() { List<RegistryService> registryServices = new ArrayList<>(); - String registryTypeNamesStr = - ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_REGISTRY - + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE); + Set<String> processedRegistryTypes = new HashSet<>(); + String registryTypeNamesStr = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig( + ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE); + if (StringUtils.isBlank(registryTypeNamesStr)) { registryTypeNamesStr = RegistryType.File.name(); } String[] registryTypeNames = registryTypeNamesStr.split(Constants.REGISTRY_TYPE_SPLIT_CHAR); + if (registryTypeNames.length > 1) { LOGGER.info("use multi registry center type: {}", registryTypeNamesStr); } + for (String registryTypeName : registryTypeNames) { RegistryType registryType; try { registryType = RegistryType.getType(registryTypeName); } catch (Exception exx) { throw new NotSupportYetException("not support registry type: " + registryTypeName); } + + if (processedRegistryTypes.contains(registryType.name())) { + LOGGER.warn("The duplicate registration center type '{}' was found in the configuration and has been skipped.", registryType.name()); + continue; Review Comment: I saw a test that verifies a method was called once by checking if `instances.size()` equals 1. How about also adding a test to verify that the corresponding log message is properly output? What do you think? ########## discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/RegistryFactoryTest.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.seata.discovery.registry; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.seata.common.ConfigurationKeys; +import org.apache.seata.common.exception.NotSupportYetException; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * The type Registry factory test. + */ +public class RegistryFactoryTest { + + private static final String REGISTRY_TYPE_KEY = + ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE; + + @AfterEach + public void tearDown() { + System.clearProperty(REGISTRY_TYPE_KEY); + } + + /** + * Test getInstance with default config. + */ + @Test + public void testGetInstanceWithDefaultConfig() { + System.setProperty(REGISTRY_TYPE_KEY, RegistryType.File.name()); + + RegistryService instance = RegistryFactory.getInstance(); + Assertions.assertNotNull(instance); + } + + /** + * Test buildRegistryService with invalid registry type. + */ + @Test + public void testGetInstanceOfInvalidRegistryType() { + System.setProperty(REGISTRY_TYPE_KEY, "InvalidRegistryType"); + + Assertions.assertThrows(NotSupportYetException.class, RegistryFactoryTest::invokeBuildRegistryService); + } + + /** + * Test buildRegistryService with blank registry type. + */ + @Test + public void testGetInstancesWithBlankRegistryType() throws Throwable { + System.setProperty(REGISTRY_TYPE_KEY, ""); + + RegistryService instance = invokeBuildRegistryService(); + Assertions.assertEquals(instance.getClass(), FileRegistryServiceImpl.class); + } + + /** + * Use reflection to call the buildRegistryServices method Review Comment: It looks like the comment should refer to `buildRegistryService` instead of `buildRegistryServices`. -- 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: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org