UT: add Polaris LoadBalancer unit test (#373)

pull/378/head
lingxiao,wu 2 years ago committed by GitHub
parent f481588c48
commit 2439ebb0ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,5 +33,6 @@
- [docs:update logo in README.](https://github.com/Tencent/spring-cloud-tencent/pull/358)
- [Refator JacksonUtils and JacksonUtilsTest](https://github.com/Tencent/spring-cloud-tencent/pull/365)
- [docs: Fix javadoc <br /> error](https://github.com/Tencent/spring-cloud-tencent/pull/371)
- [UT: add Polaris LoadBalancer unit test](https://github.com/Tencent/spring-cloud-tencent/pull/373)

@ -55,6 +55,11 @@
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-discovery-client</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

@ -17,13 +17,32 @@
package com.tencent.cloud.polaris.loadbalancer.config;
import com.netflix.client.config.DefaultClientConfigImpl;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.NoOpPing;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.discovery.client.api.DefaultConsumerAPI;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static com.tencent.polaris.test.common.Consts.PORT;
@ -37,10 +56,11 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class PolarisLoadBalancerAutoConfigurationTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class,
PolarisLoadBalancerAutoConfiguration.class,
PolarisContextAutoConfiguration.class))
PolarisContextAutoConfiguration.class,
RibbonAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT)
.withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081");
@ -50,13 +70,50 @@ public class PolarisLoadBalancerAutoConfigurationTest {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(RouterAPI.class);
assertThat(context).hasSingleBean(PolarisLoadBalancerProperties.class);
assertThat(hasSinglePolarisLoadBalancer(context)).isTrue();
});
}
private boolean hasSinglePolarisLoadBalancer(BeanFactory beanFactory) {
SpringClientFactory contextBean = beanFactory.getBean(SpringClientFactory.class);
ILoadBalancer loadBalancer = contextBean.getLoadBalancer(SERVICE_PROVIDER);
return loadBalancer instanceof PolarisLoadBalancer;
}
@Configuration
@EnableAutoConfiguration
static class PolarisRibbonTest {
@Autowired
private SDKContext sdkContext;
@Bean
public IClientConfig iClientConfig() {
DefaultClientConfigImpl config = new DefaultClientConfigImpl();
config.setClientName(SERVICE_PROVIDER);
return config;
}
@Bean
public IRule iRule() {
return new RandomRule();
}
@Bean
public IPing iPing() {
return new NoOpPing();
}
@Bean
public ServerList<Server> serverList() {
return new StaticServerList<>();
}
@Bean
public ConsumerAPI consumerAPI() {
return new DefaultConsumerAPI(sdkContext);
}
}
}

@ -0,0 +1,55 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* 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.tencent.cloud.polaris.loadbalancer.config;
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisRibbonClientConfiguration}.
*
* @author wlx
* @date 2022/7/2 10:36
*/
public class PolarisRibbonClientConfigurationTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner();
@Test
public void testDefaultInitialization() {
this.applicationContextRunner
.withConfiguration(AutoConfigurations.of(
TestApplication.class,
PolarisRibbonClientConfiguration.class))
.run(context -> {
assertThat(context).hasSingleBean(PolarisRibbonClientConfiguration.class);
assertThat(context).hasSingleBean(PolarisLoadBalancer.class);
});
}
@SpringBootApplication
static class TestApplication {
}
}
Loading…
Cancel
Save