diff --git a/sofa-serverless-runtime/sofa-serverless-base-plugin/pom.xml b/sofa-serverless-runtime/sofa-serverless-base-plugin/pom.xml index 86591aa31..88af56b27 100644 --- a/sofa-serverless-runtime/sofa-serverless-base-plugin/pom.xml +++ b/sofa-serverless-runtime/sofa-serverless-base-plugin/pom.xml @@ -31,6 +31,16 @@ com.alipay.sofa.serverless sofa-serverless-common + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + test + diff --git a/sofa-serverless-runtime/sofa-serverless-base-plugin/src/main/java/com/alipay/sofa/serverless/plugin/BaseRuntimeAutoConfiguration.java b/sofa-serverless-runtime/sofa-serverless-base-plugin/src/main/java/com/alipay/sofa/serverless/plugin/BaseRuntimeAutoConfiguration.java index 44452f45f..6c94b978b 100644 --- a/sofa-serverless-runtime/sofa-serverless-base-plugin/src/main/java/com/alipay/sofa/serverless/plugin/BaseRuntimeAutoConfiguration.java +++ b/sofa-serverless-runtime/sofa-serverless-base-plugin/src/main/java/com/alipay/sofa/serverless/plugin/BaseRuntimeAutoConfiguration.java @@ -29,7 +29,7 @@ public class BaseRuntimeAutoConfiguration { @Bean - @ConditionalOnNotMasterBiz + // @ConditionalOnNotMasterBiz public ApplicationContextEventHandler applicationContextEventHandler() { return new ApplicationContextEventHandler(); } diff --git a/sofa-serverless-runtime/sofa-serverless-base-plugin/src/test/java/com/alipay/sofa/serverless/plugin/manager/handler/BizUninstallEventHandlerTest.java b/sofa-serverless-runtime/sofa-serverless-base-plugin/src/test/java/com/alipay/sofa/serverless/plugin/manager/handler/BizUninstallEventHandlerTest.java new file mode 100644 index 000000000..f53a047de --- /dev/null +++ b/sofa-serverless-runtime/sofa-serverless-base-plugin/src/test/java/com/alipay/sofa/serverless/plugin/manager/handler/BizUninstallEventHandlerTest.java @@ -0,0 +1,77 @@ +/* + * 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 com.alipay.sofa.serverless.plugin.manager.handler; + +import com.alipay.sofa.ark.spi.event.biz.BeforeBizStartupEvent; +import com.alipay.sofa.ark.spi.event.biz.BeforeBizStopEvent; +import com.alipay.sofa.ark.spi.model.Biz; +import com.alipay.sofa.serverless.common.BizRuntimeContext; +import com.alipay.sofa.serverless.common.BizRuntimeContextRegistry; +import com.alipay.sofa.serverless.plugin.BaseRuntimeAutoConfiguration; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.WebApplicationType; +import org.springframework.context.ConfigurableApplicationContext; + +import java.util.Properties; + +@RunWith(MockitoJUnitRunner.class) +public class BizUninstallEventHandlerTest { + private static final String bizName = "eventHandlerTest"; + @Mock + private Biz biz; + + private ConfigurableApplicationContext ctx; + + private SpringApplication springApplication; + + @Before + public void before() { + Properties properties = new Properties(); + properties.setProperty("spring.application.name", bizName); + + springApplication = new SpringApplication(BaseRuntimeAutoConfiguration.class); + springApplication.setDefaultProperties(properties); + + springApplication.setWebApplicationType(WebApplicationType.NONE); + // ctx = springApplication.run(); + } + + @Test + public void handleEvent() { + Mockito.when(biz.getBizName()).thenReturn(bizName); + Mockito.when(biz.getBizClassLoader()).thenReturn(this.getClass().getClassLoader()); + + BeforeBizStartupEventHandler beforeBizStartupEventHandler = new BeforeBizStartupEventHandler(); + beforeBizStartupEventHandler.handleEvent(new BeforeBizStartupEvent(biz)); + + ctx = springApplication.run(); + + BizRuntimeContext bizRuntimeContext = BizRuntimeContextRegistry.getBizRuntimeContext(biz); + Assert.assertEquals(bizRuntimeContext.getRootApplicationContext(), ctx); + + BizUninstallEventHandler bizUninstallEventHandler = new BizUninstallEventHandler(); + bizUninstallEventHandler.handleEvent(new BeforeBizStopEvent(biz)); + Assert.assertFalse(ctx.isActive()); + } +} \ No newline at end of file diff --git a/sofa-serverless-runtime/sofa-serverless-common/pom.xml b/sofa-serverless-runtime/sofa-serverless-common/pom.xml index 40f4ad0eb..6c7b62479 100644 --- a/sofa-serverless-runtime/sofa-serverless-common/pom.xml +++ b/sofa-serverless-runtime/sofa-serverless-common/pom.xml @@ -33,6 +33,11 @@ org.springframework.boot spring-boot-starter-actuator + + org.springframework.boot + spring-boot-starter-test + test + junit junit diff --git a/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/ConditionalTest.java b/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/ConditionalTest.java new file mode 100644 index 000000000..01f2cce0a --- /dev/null +++ b/sofa-serverless-runtime/sofa-serverless-common/src/test/java/com/alipay/sofa/serverless/common/ConditionalTest.java @@ -0,0 +1,61 @@ +/* + * 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 com.alipay.sofa.serverless.common; + +import com.alipay.sofa.serverless.common.environment.ConditionalOnMasterBiz; +import com.alipay.sofa.serverless.common.environment.ConditionalOnNotMasterBiz; +import com.alipay.sofa.serverless.common.exception.BizRuntimeException; +import com.alipay.sofa.serverless.common.exception.ErrorCodes; +import org.junit.Test; +import org.springframework.boot.context.annotation.UserConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +public class ConditionalTest { + private final ApplicationContextRunner runner = new ApplicationContextRunner() + .withConfiguration(UserConfigurations + .of(ConditionalAutoConfiguration.class)); + + @Test + public void testConditional() { + runner.run(context -> { + context.assertThat().doesNotHaveBean("nonMasterBizRuntimeException"); + context.assertThat().hasBean("masterBizRuntimeException"); + }); + } + + /** + * @author mingmen + * @date 2023/6/14 + */ + @Configuration + public static class ConditionalAutoConfiguration { + + @Bean + @ConditionalOnNotMasterBiz + public BizRuntimeException nonMasterBizRuntimeException() { + return new BizRuntimeException(ErrorCodes.SpringContextManager.E100001, "error test."); + } + + @Bean + @ConditionalOnMasterBiz + public BizRuntimeException masterBizRuntimeException() { + return new BizRuntimeException(ErrorCodes.SpringContextManager.E100002, "error test."); + } + } +} \ No newline at end of file