Skip to content

Commit

Permalink
1.RpcActuatorAutoConfiguration autoconf after ReadinessAutoConfiguration
Browse files Browse the repository at this point in the history
2.update unit tests
  • Loading branch information
致节 committed Feb 26, 2024
1 parent 1c62a9b commit d4ff594
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
public class HealthCheckProviderConfigDelayRegisterChecker implements
ProviderConfigDelayRegisterChecker {

private ReadinessCheckListener readinessCheckListener;
private final ReadinessCheckListener readinessCheckListener;

public HealthCheckProviderConfigDelayRegisterChecker(ReadinessCheckListener readinessCheckListener) {
this.readinessCheckListener = readinessCheckListener;
}

@Override
public boolean allowRegister() {
return !ReadinessState.REFUSING_TRAFFIC.equals(readinessCheckListener.getReadinessState());
return ReadinessState.REFUSING_TRAFFIC != readinessCheckListener.getReadinessState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ void customProviderConfigContainerForEnableDelayRegister() {
void customProviderConfigContainerForProviderConfigDelayRegisterChecker() {
this.contextRunner.withUserConfiguration(ProviderConfigDelayRegisterCheckerConfiguration.class)
.run(context -> {
ProviderConfigContainer providerConfigContainer = context.getBean(ProviderConfigContainer.class);
List<ProviderConfigDelayRegisterChecker> checkers =
context.getBeanProvider(ProviderConfigDelayRegisterChecker.class).stream().toList();
providerConfigContainer.getProviderConfigDelayRegisterCheckerList();
assertThat(checkers.size()).isEqualTo(1);
assertThat(checkers.get(0).allowRegister()).isTrue();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,14 @@ public void publishAllProviderConfig() {

private void doDelayPublishProviderConfig(ProviderConfig providerConfig,
List<ProviderConfigDelayRegisterChecker> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"default.proxy": "javassist",
"rpc.config.order": 1000,
"consumer.address.wait": 1,
"provider.delay": 5000
"provider.delay": 2000
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
http://sofastack.io/schema/sofaboot http://sofastack.io/schema/sofaboot.xsd">

<bean id="delayRegisterServiceImpl" class="com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterServiceImpl"/>
<sofa:service ref="delayRegisterServiceImpl" interface="com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService">
<bean id="customReadinessCallBack" class="com.alipay.sofa.smoke.tests.rpc.delayregister.DelayRegisterTestsBase.CustomReadinessCallBack"/>

<sofa:service ref="delayRegisterServiceImpl" interface="com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService" unique-id="${test-uniqueId}">
<sofa:binding.bolt/>
</sofa:service>
<sofa:reference jvm-first="false" id="delayRegisterService" interface="com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService">
<sofa:reference jvm-first="false" id="delayRegisterService" interface="com.alipay.sofa.smoke.tests.rpc.boot.bean.delayregister.DelayRegisterService" unique-id="${test-uniqueId}">
<sofa:binding.bolt/>
</sofa:reference>

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,52 +16,61 @@
*/
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
*/
@SpringBootTest(classes = RpcSofaBootApplication.class, properties = {
"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();
}
}
}

0 comments on commit d4ff594

Please sign in to comment.