You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/06/04 02:03:02 UTC

[incubator-servicecomb-java-chassis] 02/06: [SCB-617] uninit config center source when shutdown

This is an automated email from the ASF dual-hosted git repository.

wujimin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit ac505e483163dc1b8cc62057dc4967a8bbbdb541
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon May 28 10:16:37 2018 +0800

    [SCB-617] uninit config center source when shutdown
---
 .../servicecomb/core/CseApplicationListener.java   |  3 ++
 .../org/apache/servicecomb/core/SCBEngine.java     |  6 ++-
 .../org/apache/servicecomb/core/TestSCBEngine.java | 13 ++++-
 .../core/TestCseApplicationListener.java           | 56 ++++++++++++++++++++++
 4 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
index 3658687..28b6e73 100644
--- a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
+++ b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java
@@ -28,6 +28,7 @@ import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
 import org.springframework.context.ApplicationEvent;
 import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextClosedEvent;
 import org.springframework.context.event.ContextRefreshedEvent;
 import org.springframework.context.support.AbstractApplicationContext;
 import org.springframework.core.Ordered;
@@ -71,6 +72,8 @@ public class CseApplicationListener
       }
 
       SCBEngine.getInstance().init();
+    } else if (event instanceof ContextClosedEvent) {
+      SCBEngine.getInstance().uninit();
     }
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
index d30eb3c..0616c25 100644
--- a/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
+++ b/core/src/main/java/org/apache/servicecomb/core/SCBEngine.java
@@ -20,6 +20,7 @@ import java.util.Collection;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.apache.servicecomb.config.ConfigUtil;
 import org.apache.servicecomb.core.BootListener.BootEvent;
 import org.apache.servicecomb.core.BootListener.EventType;
 import org.apache.servicecomb.core.definition.loader.SchemaListenerManager;
@@ -256,7 +257,10 @@ public class SCBEngine {
     VertxUtils.blockCloseVertxByName("config-center");
     VertxUtils.blockCloseVertxByName("transport");
 
-    //Step 6: notify all component do clean works via AFTER_CLOSE Event
+    //Step 6: destroy config center source
+    ConfigUtil.destroyConfigCenterConfigurationSource();
+
+    //Step 7: notify all component do clean works via AFTER_CLOSE Event
     safeTriggerEvent(EventType.AFTER_CLOSE);
   }
 
diff --git a/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java b/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
index e452e55..f39636a 100644
--- a/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
+++ b/core/src/test/java/org/apache/servicecomb/core/TestSCBEngine.java
@@ -18,7 +18,9 @@
 package org.apache.servicecomb.core;
 
 import java.util.ArrayList;
+import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.servicecomb.config.ConfigUtil;
 import org.apache.servicecomb.core.definition.loader.SchemaListenerManager;
 import org.apache.servicecomb.core.provider.consumer.ConsumerProviderManager;
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
@@ -36,6 +38,8 @@ import org.mockito.Mockito;
 
 import mockit.Expectations;
 import mockit.Injectable;
+import mockit.Mock;
+import mockit.MockUp;
 import mockit.Mocked;
 
 public class TestSCBEngine {
@@ -47,7 +51,6 @@ public class TestSCBEngine {
       @Injectable ConsumerProviderManager consumerProviderManager,
       @Injectable TransportManager transportManager,
       @Injectable AppManager appManager) {
-
     new Expectations(RegistryUtils.class) {
       {
         RegistryUtils.getServiceRegistry().getAppManager();
@@ -56,6 +59,13 @@ public class TestSCBEngine {
         RegistryUtils.destroy();
       }
     };
+    AtomicBoolean configDestroy = new AtomicBoolean();
+    new MockUp<ConfigUtil>() {
+      @Mock
+      void destroyConfigCenterConfigurationSource() {
+        configDestroy.set(true);
+      }
+    };
 
     SchemaListenerManager schemaListenerManager = Mockito.mock(SchemaListenerManager.class);
 
@@ -75,6 +85,7 @@ public class TestSCBEngine {
     engine.uninit();
 
     Assert.assertEquals(SCBStatus.DOWN, engine.getStatus());
+    Assert.assertTrue(configDestroy.get());
   }
 
   @Test
diff --git a/core/src/test/java/org/apache/servicecomb/core/org/apache/servicecomb/core/TestCseApplicationListener.java b/core/src/test/java/org/apache/servicecomb/core/org/apache/servicecomb/core/TestCseApplicationListener.java
new file mode 100644
index 0000000..26d7d78
--- /dev/null
+++ b/core/src/test/java/org/apache/servicecomb/core/org/apache/servicecomb/core/TestCseApplicationListener.java
@@ -0,0 +1,56 @@
+/*
+ * 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.servicecomb.core.org.apache.servicecomb.core;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.servicecomb.core.CseApplicationListener;
+import org.apache.servicecomb.core.SCBEngine;
+import org.apache.servicecomb.core.SCBStatus;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.context.event.ContextClosedEvent;
+
+import mockit.Mock;
+import mockit.MockUp;
+import mockit.Mocked;
+
+public class TestCseApplicationListener {
+  @Test
+  public void onApplicationEvent_close(@Mocked ContextClosedEvent contextClosedEvent) {
+    AtomicInteger count = new AtomicInteger();
+    SCBEngine scbEngine = new SCBEngine() {
+      @Override
+      public synchronized void uninit() {
+        count.incrementAndGet();
+      }
+    };
+    new MockUp<SCBEngine>() {
+      @Mock
+      SCBEngine getInstance() {
+        return scbEngine;
+      }
+    };
+    scbEngine.setStatus(SCBStatus.UP);
+
+    CseApplicationListener listener = new CseApplicationListener();
+
+    listener.onApplicationEvent(contextClosedEvent);
+
+    Assert.assertEquals(1, count.get());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
wujimin@apache.org.