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 f9eed1b5569679eb52cc6231cdfff1115552119f Author: wujimin <wuji...@huawei.com> AuthorDate: Mon Apr 2 11:10:42 2018 +0800 SCB-422 add chassis close event --- .../org/apache/servicecomb/core/BootListener.java | 4 ++- .../servicecomb/core/CseApplicationListener.java | 2 ++ .../core/TestCseApplicationListener.java | 30 ++++++++++++++++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/apache/servicecomb/core/BootListener.java b/core/src/main/java/org/apache/servicecomb/core/BootListener.java index f6e50cc..b3b8755 100644 --- a/core/src/main/java/org/apache/servicecomb/core/BootListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/BootListener.java @@ -28,7 +28,9 @@ public interface BootListener { BEFORE_TRANSPORT, AFTER_TRANSPORT, BEFORE_REGISTRY, - AFTER_REGISTRY + AFTER_REGISTRY, + BEFORE_CLOSE, + AFTER_CLOSE } class BootEvent { 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 5cff9ca..4952f35 100644 --- a/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java +++ b/core/src/main/java/org/apache/servicecomb/core/CseApplicationListener.java @@ -147,7 +147,9 @@ public class CseApplicationListener } } else if (event instanceof ContextClosedEvent) { LOGGER.warn("cse is closing now..."); + triggerEvent(EventType.BEFORE_CLOSE); RegistryUtils.destroy(); + triggerEvent(EventType.AFTER_CLOSE); isInit = false; } } diff --git a/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java b/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java index fb473b7..e5eaec8 100644 --- a/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java +++ b/core/src/test/java/org/apache/servicecomb/core/TestCseApplicationListener.java @@ -22,11 +22,17 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; +import javax.xml.ws.Holder; + +import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.servicecomb.core.BootListener.BootEvent; +import org.apache.servicecomb.core.BootListener.EventType; import org.apache.servicecomb.core.definition.loader.SchemaListenerManager; import org.apache.servicecomb.core.endpoint.AbstractEndpointsCache; import org.apache.servicecomb.core.provider.consumer.ConsumerProviderManager; @@ -39,6 +45,7 @@ import org.apache.servicecomb.foundation.common.utils.ReflectUtils; import org.apache.servicecomb.serviceregistry.RegistryUtils; import org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance; import org.apache.servicecomb.serviceregistry.task.MicroserviceInstanceRegisterTask; +import org.hamcrest.Matchers; import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; @@ -53,6 +60,8 @@ import org.springframework.context.support.AbstractApplicationContext; import mockit.Deencapsulation; import mockit.Expectations; import mockit.Injectable; +import mockit.Mock; +import mockit.MockUp; import mockit.Mocked; public class TestCseApplicationListener { @@ -147,15 +156,26 @@ public class TestCseApplicationListener { } @Test - public void testCseApplicationListenerShutdown(@Injectable ContextClosedEvent event, - @Mocked RegistryUtils ru) { - new Expectations() { - { - RegistryUtils.destroy(); + public void testCseApplicationListenerShutdown(@Mocked ApplicationContext context) throws IllegalAccessException { + Holder<Boolean> destroyHolder = new Holder<>(); + new MockUp<RegistryUtils>() { + @Mock + void destroy() { + destroyHolder.value = true; } }; CseApplicationListener cal = new CseApplicationListener(); + ContextClosedEvent event = new ContextClosedEvent(context); + + List<EventType> eventTypes = new ArrayList<>(); + BootListener bootListener = e -> { + eventTypes.add(e.getEventType()); + }; + FieldUtils.writeField(cal, "bootListenerList", Arrays.asList(bootListener), true); cal.onApplicationEvent(event); + + Assert.assertTrue(destroyHolder.value); + Assert.assertThat(eventTypes, Matchers.contains(EventType.BEFORE_CLOSE, EventType.AFTER_CLOSE)); } @Test -- To stop receiving notification emails like this one, please contact wuji...@apache.org.