From d4ff594155537c3359db0b8ed09a83665598aeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=B4=E8=8A=82?= Date: Mon, 26 Feb 2024 20:35:29 +0800 Subject: [PATCH] 1.RpcActuatorAutoConfiguration autoconf after ReadinessAutoConfiguration 2.update unit tests --- .../rpc/RpcActuatorAutoConfiguration.java | 3 +- .../RpcActuatorAutoConfigurationTests.java | 29 ------------- ...eckProviderConfigDelayRegisterChecker.java | 4 +- .../rpc/SofaRpcAutoConfigurationTests.java | 3 +- .../container/ProviderConfigContainer.java | 15 +++---- .../META-INF/sofa-rpc/rpc-config.json | 2 +- .../spring/test_only_delay_register.xml | 6 ++- .../delayregister/DelayRegisterFailTests.java | 39 +++++++++++++++++ .../DelayRegisterSuccessTests.java | 39 +++++++++++++++++ ...Tests.java => DelayRegisterTestsBase.java} | 43 +++++++++++-------- 10 files changed, 120 insertions(+), 63 deletions(-) create mode 100644 sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java create mode 100644 sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java rename sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/{DelayRegisterTests.java => DelayRegisterTestsBase.java} (69%) diff --git a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java index 6dfee3c05..792c160d5 100644 --- a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java +++ b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/main/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfiguration.java @@ -16,6 +16,7 @@ */ package com.alipay.sofa.boot.actuator.autoconfigure.rpc; +import com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessAutoConfiguration; import com.alipay.sofa.boot.actuator.health.ReadinessCheckListener; import com.alipay.sofa.boot.actuator.health.ReadinessEndpoint; import com.alipay.sofa.boot.actuator.rpc.HealthCheckProviderConfigDelayRegisterChecker; @@ -37,7 +38,7 @@ * @author huzijie * @version RpcActuatorAutoConfiguration.java, v 0.1 2023年02月08日 4:45 PM huzijie Exp $ */ -@AutoConfiguration(after = SofaRpcAutoConfiguration.class) +@AutoConfiguration(after = { SofaRpcAutoConfiguration.class, ReadinessAutoConfiguration.class }) @ConditionalOnClass(RpcStartApplicationListener.class) @ConditionalOnBean(RpcStartApplicationListener.class) public class RpcActuatorAutoConfiguration { diff --git a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java index 38288495c..a72f858c0 100644 --- a/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java +++ b/sofa-boot-project/sofa-boot-actuator-autoconfigure/src/test/java/com/alipay/sofa/boot/actuator/autoconfigure/rpc/RpcActuatorAutoConfigurationTests.java @@ -17,10 +17,6 @@ package com.alipay.sofa.boot.actuator.autoconfigure.rpc; import com.alipay.sofa.boot.actuator.autoconfigure.health.ReadinessAutoConfiguration; -import com.alipay.sofa.boot.actuator.health.HealthCheckerProcessor; -import com.alipay.sofa.boot.actuator.health.HealthIndicatorProcessor; -import com.alipay.sofa.boot.actuator.health.ReadinessCheckCallbackProcessor; -import com.alipay.sofa.boot.actuator.health.ReadinessCheckListener; import com.alipay.sofa.boot.actuator.rpc.HealthCheckProviderConfigDelayRegisterChecker; import com.alipay.sofa.boot.actuator.rpc.RpcAfterHealthCheckCallback; import com.alipay.sofa.boot.actuator.rpc.SofaRpcEndpoint; @@ -29,8 +25,6 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.context.annotation.Bean; -import org.springframework.context.event.ContextRefreshedEvent; import static org.assertj.core.api.Assertions.assertThat; @@ -94,27 +88,4 @@ void runWhenRpcBeanNotExist() { .doesNotHaveBean(RpcAfterHealthCheckCallback.class) .doesNotHaveBean(SofaRpcEndpoint.class)); } - - static class MockReadinessCheckListenerConfiguration { - - @Bean - public MockReadinessCheckListener mockReadinessCheckListener() { - return new MockReadinessCheckListener(null, null, null); - } - } - - static class MockReadinessCheckListener extends ReadinessCheckListener { - - public MockReadinessCheckListener(HealthCheckerProcessor healthCheckerProcessor, - HealthIndicatorProcessor healthIndicatorProcessor, - ReadinessCheckCallbackProcessor afterReadinessCheckCallbackProcessor) { - super(healthCheckerProcessor, healthIndicatorProcessor, - afterReadinessCheckCallbackProcessor); - } - - @Override - public void onContextRefreshedEvent(ContextRefreshedEvent event) { - // do nothing - } - } } diff --git a/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java b/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java index 9fdd60211..7526ca42d 100644 --- a/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java +++ b/sofa-boot-project/sofa-boot-actuator/src/main/java/com/alipay/sofa/boot/actuator/rpc/HealthCheckProviderConfigDelayRegisterChecker.java @@ -26,7 +26,7 @@ public class HealthCheckProviderConfigDelayRegisterChecker implements ProviderConfigDelayRegisterChecker { - private ReadinessCheckListener readinessCheckListener; + private final ReadinessCheckListener readinessCheckListener; public HealthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener readinessCheckListener) { this.readinessCheckListener = readinessCheckListener; @@ -34,6 +34,6 @@ public HealthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener read @Override public boolean allowRegister() { - return !ReadinessState.REFUSING_TRAFFIC.equals(readinessCheckListener.getReadinessState()); + return ReadinessState.REFUSING_TRAFFIC != readinessCheckListener.getReadinessState(); } } diff --git a/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java b/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java index daf1586cf..8e8ac4cf1 100644 --- a/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java +++ b/sofa-boot-project/sofa-boot-autoconfigure/src/test/java/com/alipay/sofa/boot/autoconfigure/rpc/SofaRpcAutoConfigurationTests.java @@ -399,8 +399,9 @@ void customProviderConfigContainerForEnableDelayRegister() { void customProviderConfigContainerForProviderConfigDelayRegisterChecker() { this.contextRunner.withUserConfiguration(ProviderConfigDelayRegisterCheckerConfiguration.class) .run(context -> { + ProviderConfigContainer providerConfigContainer = context.getBean(ProviderConfigContainer.class); List checkers = - context.getBeanProvider(ProviderConfigDelayRegisterChecker.class).stream().toList(); + providerConfigContainer.getProviderConfigDelayRegisterCheckerList(); assertThat(checkers.size()).isEqualTo(1); assertThat(checkers.get(0).allowRegister()).isTrue(); }); diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java index e8532ce46..6ace6f752 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/container/ProviderConfigContainer.java @@ -163,19 +163,14 @@ public void publishAllProviderConfig() { private void doDelayPublishProviderConfig(ProviderConfig providerConfig, List providerConfigDelayRegisterCheckerList) { - boolean allTrue = true; for (ProviderConfigDelayRegisterChecker providerConfigDelayRegisterChecker : providerConfigDelayRegisterCheckerList) { if (!providerConfigDelayRegisterChecker.allowRegister()) { - allTrue = false; - break; - } - } - if (!allTrue) { - if (LOGGER.isWarnEnabled()) { - LOGGER.warn("service publish failed, interfaceId[" - + providerConfig.getInterfaceId() + "], please check."); + if (LOGGER.isWarnEnabled()) { + LOGGER.warn("service publish failed, interfaceId [{}], please check.", + providerConfig.getInterfaceId()); + } + return; } - return; } doPublishProviderConfig(providerConfig); } diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/META-INF/sofa-rpc/rpc-config.json b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/META-INF/sofa-rpc/rpc-config.json index 8b54a5d40..b9d3d9c98 100644 --- a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/META-INF/sofa-rpc/rpc-config.json +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/META-INF/sofa-rpc/rpc-config.json @@ -2,5 +2,5 @@ "default.proxy": "javassist", "rpc.config.order": 1000, "consumer.address.wait": 1, - "provider.delay": 5000 + "provider.delay": 2000 } diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml index b86770ff4..8f16189f6 100644 --- a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/main/resources/spring/test_only_delay_register.xml @@ -6,10 +6,12 @@ http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd"> - + + + - + diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java new file mode 100644 index 000000000..dfb2aac0e --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterFailTests.java @@ -0,0 +1,39 @@ +/* + * 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.smoke.tests.rpc.delayregister; + +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestPropertySource; + +/** + * Tests for rpc provider delay register when health check failed. + * + * @author huzijie + * @version DelayRegisterFailTests.java, v 0.1 2024年02月26日 20:15 huzijie Exp $ + */ +@TestPropertySource(properties = { "delayregister.healthcheck.result=false", "test-uniqueId=fail" }) +public class DelayRegisterFailTests extends DelayRegisterTestsBase { + + @Test + public void testDelayRegister() throws InterruptedException { + registerFail(); + + Thread.sleep(3000); + + registerFail(); + } +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java new file mode 100644 index 000000000..f46b192b9 --- /dev/null +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterSuccessTests.java @@ -0,0 +1,39 @@ +/* + * 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.smoke.tests.rpc.delayregister; + +import org.junit.jupiter.api.Test; +import org.springframework.test.context.TestPropertySource; + +/** + * Tests for rpc provider delay register when health check success. + * + * @author huzijie + * @version DelayRegisterSuccessTests.java, v 0.1 2024年02月26日 20:15 huzijie Exp $ + */ +@TestPropertySource(properties = { "delayregister.healthcheck.result=true", "test-uniqueId=success" }) +public class DelayRegisterSuccessTests extends DelayRegisterTestsBase { + + @Test + public void testDelayRegister() throws InterruptedException { + registerFail(); + + Thread.sleep(3000); + + registerSuccess(); + } +} diff --git a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTests.java b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTestsBase.java similarity index 69% rename from sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTests.java rename to sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTestsBase.java index 29d17c4e5..c7d642b5b 100644 --- a/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTests.java +++ b/sofa-boot-tests/sofa-boot-smoke-tests/sofa-boot-smoke-tests-rpc/src/test/java/com/alipay/sofa/smoke/tests/rpc/delayregister/DelayRegisterTestsBase.java @@ -16,20 +16,25 @@ */ package com.alipay.sofa.smoke.tests.rpc.delayregister; +import com.alipay.sofa.boot.actuator.health.ReadinessCheckCallback; import com.alipay.sofa.rpc.core.exception.SofaRouteException; import com.alipay.sofa.smoke.tests.rpc.boot.RpcSofaBootApplication; import com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.health.Health; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** + * Base tests for rpc provider delay register. + * * @author chengming * @version DelayRegisterTests.java, v 0.1 2024年02月26日 3:58 PM chengming */ @@ -37,31 +42,35 @@ "sofa.boot.rpc.registry.address=zookeeper://127.0.0.1:2181", "sofa.boot.rpc.enable-auto-publish=true", "sofa.boot.rpc.enable-delay-register=true" }) -@Import(DelayRegisterTests.DelayRegisterConfiguration.class) -public class DelayRegisterTests { - +@Import(DelayRegisterTestsBase.DelayRegisterConfiguration.class) +public class DelayRegisterTestsBase { @Autowired private DelayRegisterService delayRegisterService; - /** - * 延时五秒注册 - */ - @Test - public void testDelayRegister() throws InterruptedException { - // 首次发起调用、期望返回 RPC cannot find service - Assertions.assertThatThrownBy(() -> delayRegisterService.sayHello("hi")).isInstanceOf(SofaRouteException.class). - hasMessageContaining("Cannot get the service address of service [com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService:1.0], please check the registry log."); - // 因为在 META-INF/sofa-rpc/rpc-config.json 配置了5s - Thread.sleep(5000); - - // 等待5s之后可以正常发起调用 + protected void registerSuccess() { String hi = delayRegisterService.sayHello("hi"); assertThat(hi).isEqualTo("hi"); } + protected void registerFail() { + assertThatThrownBy(() -> delayRegisterService.sayHello("hi")).isInstanceOf(SofaRouteException.class). + hasMessageContaining("Cannot get the service address of service"); + } + @Configuration @ImportResource("/spring/test_only_delay_register.xml") static class DelayRegisterConfiguration { } + + public static class CustomReadinessCallBack implements ReadinessCheckCallback { + + @Value("${delayregister.healthcheck.result}") + private boolean result; + + @Override + public Health onHealthy(ApplicationContext applicationContext) { + return result ? Health.up().build() : Health.down().build(); + } + } }