This is an automated email from the ASF dual-hosted git repository. liubao pushed a commit to branch 2.8.x in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
commit b745257d26586e6fb036389dd6ce8b56ccf14a74 Author: liubao <[email protected]> AuthorDate: Fri Jun 9 11:47:39 2023 +0800 [SCB-2796]fix configuration item and test cases error --- .../servicecomb/core/bootstrap/SCBBootstrap.java | 5 +++ .../servicecomb/serviceregistry/RegistryUtils.java | 40 ++++++++++++++-------- .../servicecomb/serviceregistry/TestRegistry.java | 2 +- .../cache/TestMicroserviceInstanceCache.java | 21 +++++------- .../instance/TestInstanceCacheCheckerMock.java | 2 +- .../TestInstanceCacheCheckerWithoutMock.java | 5 +-- .../registry/TestLocalServiceRegistry.java | 7 ++-- .../registry/TestRemoteServiceRegistry.java | 9 +++-- .../transport/rest/servlet/TestRestServlet.java | 3 +- .../rest/vertx/TestRestServerVerticle.java | 2 ++ .../rest/vertx/TestVertxRestDispatcher.java | 2 ++ 11 files changed, 58 insertions(+), 40 deletions(-) diff --git a/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBBootstrap.java b/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBBootstrap.java index 1967e5609..8fc096f26 100644 --- a/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBBootstrap.java +++ b/core/src/main/java/org/apache/servicecomb/core/bootstrap/SCBBootstrap.java @@ -26,4 +26,9 @@ public class SCBBootstrap { DiscoveryManager.INSTANCE.init(); return new SCBEngineForTest(); } + + public static void resetSCBEngineForTest() { + RegistrationManager.INSTANCE.destroy(); + DiscoveryManager.INSTANCE.destroy(); + } } diff --git a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java index e0e36cb41..fe16cd4b6 100644 --- a/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java +++ b/service-registry/registry-service-center/src/main/java/org/apache/servicecomb/serviceregistry/RegistryUtils.java @@ -57,13 +57,19 @@ import com.google.common.eventbus.Subscribe; import com.netflix.config.DynamicPropertyFactory; public final class RegistryUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistryUtils.class); + public static final String SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED + = "servicecomb.service.registry.repeated.initialization.allowed"; + /** * The default ServiceRegistry instance */ private static volatile ServiceRegistry serviceRegistry; + private static volatile boolean running = false; + private static final Map<String, ServiceRegistry> EXTRA_SERVICE_REGISTRIES = new LinkedHashMap<>(); private static AggregateServiceRegistryCache aggregateServiceRegistryCache; @@ -74,10 +80,10 @@ public final class RegistryUtils { public static synchronized void init() { if (serviceRegistry != null) { if (DynamicPropertyFactory.getInstance() - .getBooleanProperty("servicecomb.service.registry.initialization.notAllowed", true).get()) { - throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice."); + .getBooleanProperty(SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED, false).get()) { + return; } - return; + throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice."); } initializeServiceRegistriesWithConfig(ConfigUtil.createLocalConfig()); @@ -85,14 +91,6 @@ public final class RegistryUtils { initAggregateServiceRegistryCache(); } - @VisibleForTesting - public static synchronized void reset() { - if (serviceRegistry != null) { - serviceRegistry.destroy(); - serviceRegistry = null; - } - } - private static void initAggregateServiceRegistryCache() { ArrayList<ServiceRegistry> serviceRegistries = new ArrayList<>(); executeOnEachServiceRegistry(serviceRegistries::add); @@ -123,17 +121,31 @@ public final class RegistryUtils { } public static void run() { + if (running) { + if (DynamicPropertyFactory.getInstance() + .getBooleanProperty(SERVICECOMB_SERVICE_REGISTRY_REPEATED_INITIALIZATION_ALLOWED, false).get()) { + return; + } + throw new IllegalStateException("Registry has already bean initialized and not allowed to initialize twice."); + } executeOnEachServiceRegistry(ServiceRegistry::run); } public static void destroy() { executeOnEachServiceRegistry(ServiceRegistry::destroy); + running = false; + if (serviceRegistry != null) { + serviceRegistry = null; + } + EXTRA_SERVICE_REGISTRIES.clear(); } + @VisibleForTesting public static ServiceRegistry getServiceRegistry() { return serviceRegistry; } + @VisibleForTesting public static void setServiceRegistry(ServiceRegistry serviceRegistry) { RegistryUtils.serviceRegistry = serviceRegistry; initAggregateServiceRegistryCache(); @@ -266,8 +278,8 @@ public final class RegistryUtils { } public static void executeOnEachServiceRegistry(Consumer<ServiceRegistry> action) { - if (null != getServiceRegistry()) { - action.accept(getServiceRegistry()); + if (null != serviceRegistry) { + action.accept(serviceRegistry); } if (!EXTRA_SERVICE_REGISTRIES.isEmpty()) { EXTRA_SERVICE_REGISTRIES.forEach((k, v) -> action.accept(v)); @@ -309,7 +321,7 @@ public final class RegistryUtils { public static ServiceRegistry getServiceRegistry(String registryName) { if (ServiceRegistry.DEFAULT_REGISTRY_NAME.equals(registryName)) { - return getServiceRegistry(); + return serviceRegistry; } return EXTRA_SERVICE_REGISTRIES.get(registryName); diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistry.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistry.java index 4c63986e9..1c0e96640 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistry.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/TestRegistry.java @@ -81,7 +81,7 @@ public class TestRegistry { @AfterEach public void tearDown() throws Exception { - RegistryUtils.reset(); + RegistryUtils.destroy(); } @SuppressWarnings("deprecation") diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/cache/TestMicroserviceInstanceCache.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/cache/TestMicroserviceInstanceCache.java index cdd84019d..b838cd86b 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/cache/TestMicroserviceInstanceCache.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/cache/TestMicroserviceInstanceCache.java @@ -20,30 +20,32 @@ package org.apache.servicecomb.serviceregistry.cache; import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.registry.DiscoveryManager; +import org.apache.servicecomb.registry.api.registry.Microservice; +import org.apache.servicecomb.registry.api.registry.MicroserviceInstance; import org.apache.servicecomb.registry.cache.MicroserviceInstanceCache; import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.ServiceRegistry; -import org.apache.servicecomb.registry.api.registry.Microservice; -import org.apache.servicecomb.registry.api.registry.MicroserviceInstance; import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; -import org.junit.AfterClass; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import mockit.Expectations; import mockit.Mock; import mockit.MockUp; import mockit.Mocked; -import org.junit.jupiter.api.Assertions; public class TestMicroserviceInstanceCache { @Before public void setup() { ConfigUtil.installDynamicConfig(); + RegistryUtils.init(); } - @AfterClass - public static void classTeardown() { + @After + public void teardown() { + RegistryUtils.destroy(); ArchaiusUtils.resetConfig(); } @@ -75,12 +77,7 @@ public class TestMicroserviceInstanceCache { public void testGetOrCreateMicroserviceInstance(@Mocked ServiceRegistry serviceRegistry, @Mocked ServiceRegistryClient client, @Mocked MicroserviceInstance instance) { - new MockUp<RegistryUtils>() { - @Mock - ServiceRegistry getServiceRegistry() { - return serviceRegistry; - } - }; + RegistryUtils.setServiceRegistry(serviceRegistry); new Expectations() { { serviceRegistry.getServiceRegistryClient(); diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerMock.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerMock.java index db28af29e..771bbcfe1 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerMock.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerMock.java @@ -73,7 +73,7 @@ public class TestInstanceCacheCheckerMock { @After public void tearDown() throws Exception { ArchaiusUtils.resetConfig(); - RegistryUtils.reset(); + RegistryUtils.destroy(); } private Holder<MicroserviceInstances> createFindServiceInstancesResult() { diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java index 2e71578f7..8d833905f 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/diagnosis/instance/TestInstanceCacheCheckerWithoutMock.java @@ -69,7 +69,7 @@ public class TestInstanceCacheCheckerWithoutMock { @After public void tearDown() throws Exception { ArchaiusUtils.resetConfig(); - RegistryUtils.reset(); + RegistryUtils.destroy(); } @Test @@ -102,7 +102,8 @@ public class TestInstanceCacheCheckerWithoutMock { MicroserviceVersionRule microserviceVersionRule = DiscoveryManager.INSTANCE.getAppManager() .getOrCreateMicroserviceVersionRule(appId, microserviceName, DefinitionConst.VERSION_RULE_ALL); - Assertions.assertEquals(microserviceName, microserviceVersionRule.getLatestMicroserviceVersion().getMicroserviceName()); + Assertions.assertEquals(microserviceName, + microserviceVersionRule.getLatestMicroserviceVersion().getMicroserviceName()); InstanceCacheSummary instanceCacheSummary = checker.check(); diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestLocalServiceRegistry.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestLocalServiceRegistry.java index bf2dcbe11..c81dd2a5a 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestLocalServiceRegistry.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestLocalServiceRegistry.java @@ -28,6 +28,8 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import com.netflix.config.ConcurrentCompositeConfiguration; import com.netflix.config.ConcurrentMapConfiguration; @@ -36,9 +38,6 @@ import com.netflix.config.DynamicPropertyFactory; import mockit.Deencapsulation; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; - public class TestLocalServiceRegistry { private static final AbstractConfiguration inMemoryConfig = new ConcurrentMapConfiguration(); @@ -67,7 +66,7 @@ public class TestLocalServiceRegistry { @AfterEach public void tearDown() throws Exception { - RegistryUtils.reset(); + RegistryUtils.destroy(); } @Test diff --git a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java index f46999b6a..bce8f3c37 100644 --- a/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java +++ b/service-registry/registry-service-center/src/test/java/org/apache/servicecomb/serviceregistry/registry/TestRemoteServiceRegistry.java @@ -23,13 +23,15 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.configuration.Configuration; import org.apache.servicecomb.config.ConfigUtil; import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils; -import org.apache.servicecomb.serviceregistry.event.ShutdownEvent; import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.ServiceRegistry; import org.apache.servicecomb.serviceregistry.client.LocalServiceRegistryClientImpl; import org.apache.servicecomb.serviceregistry.client.ServiceRegistryClient; import org.apache.servicecomb.serviceregistry.config.ServiceRegistryConfig; +import org.apache.servicecomb.serviceregistry.event.ShutdownEvent; import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import com.google.common.eventbus.EventBus; @@ -38,9 +40,6 @@ import mockit.Injectable; import mockit.Mock; import mockit.MockUp; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; - public class TestRemoteServiceRegistry { static class TestingRemoteServiceRegistry extends RemoteServiceRegistry { public TestingRemoteServiceRegistry(EventBus eventBus, ServiceRegistryConfig serviceRegistryConfig, @@ -56,7 +55,7 @@ public class TestRemoteServiceRegistry { @AfterEach public void tearDown() throws Exception { - RegistryUtils.reset(); + RegistryUtils.destroy(); } @Test diff --git a/transports/transport-rest/transport-rest-servlet/src/test/java/org/apache/servicecomb/transport/rest/servlet/TestRestServlet.java b/transports/transport-rest/transport-rest-servlet/src/test/java/org/apache/servicecomb/transport/rest/servlet/TestRestServlet.java index 01855f89f..b4f2a0a9b 100644 --- a/transports/transport-rest/transport-rest-servlet/src/test/java/org/apache/servicecomb/transport/rest/servlet/TestRestServlet.java +++ b/transports/transport-rest/transport-rest-servlet/src/test/java/org/apache/servicecomb/transport/rest/servlet/TestRestServlet.java @@ -29,11 +29,11 @@ import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import mockit.Deencapsulation; import mockit.Mock; import mockit.MockUp; -import org.junit.jupiter.api.Assertions; public class TestRestServlet { private RestServlet restservlet = null; @@ -51,6 +51,7 @@ public class TestRestServlet { public void tearDown() { restservlet = null; SCBEngine.getInstance().destroy(); + SCBBootstrap.resetSCBEngineForTest(); ArchaiusUtils.resetConfig(); } diff --git a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java index 57a6a2ac1..684a6de69 100644 --- a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java +++ b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestRestServerVerticle.java @@ -30,6 +30,7 @@ import org.apache.servicecomb.core.transport.AbstractTransport; import org.apache.servicecomb.foundation.common.Holder; import org.apache.servicecomb.foundation.common.net.URIEndpointObject; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; +import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.After; @@ -75,6 +76,7 @@ public class TestRestServerVerticle { startPromise = null; SCBEngine.getInstance().destroy(); ArchaiusUtils.resetConfig(); + RegistryUtils.destroy(); } @Test diff --git a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java index 3a98723c5..0c6f85f17 100644 --- a/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java +++ b/transports/transport-rest/transport-rest-vertx/src/test/java/org/apache/servicecomb/transport/rest/vertx/TestVertxRestDispatcher.java @@ -42,6 +42,7 @@ import org.apache.servicecomb.core.transport.TransportManager; import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils; import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx; import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx; +import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.swagger.invocation.exception.InvocationException; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -96,6 +97,7 @@ public class TestVertxRestDispatcher { public void teardown() { SCBEngine.getInstance().destroy(); ArchaiusUtils.resetConfig(); + RegistryUtils.destroy(); } @Test
