Merge pull request #202 from SkyeBeFreeman/main
test:add junit test to polaris-circuitbreaker.pull/206/head
commit
c1b8baf2c3
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.circuitbreaker;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisCircuitBreakerBootstrapConfiguration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisCircuitBreakerBootstrapConfigurationTest {
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(PolarisCircuitBreakerBootstrapConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(PolarisCircuitBreakerBootstrapConfiguration.CircuitBreakerConfigModifier.class);
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.circuitbreaker;
|
||||
|
||||
import com.tencent.cloud.polaris.circuitbreaker.feign.PolarisFeignBeanPostProcessor;
|
||||
import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration;
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisFeignClientAutoConfiguration}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisFeignClientAutoConfigurationTest {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(
|
||||
PolarisContextAutoConfiguration.class,
|
||||
PolarisFeignClientAutoConfiguration.class))
|
||||
.withPropertyValues("spring.cloud.polaris.circuitbreaker.enabled=true");
|
||||
|
||||
@Test
|
||||
public void testDefaultInitialization() {
|
||||
this.contextRunner.run(context -> {
|
||||
assertThat(context).hasSingleBean(ConsumerAPI.class);
|
||||
assertThat(context).hasSingleBean(PolarisFeignBeanPostProcessor.class);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.circuitbreaker.feign;
|
||||
|
||||
import com.tencent.polaris.api.core.ConsumerAPI;
|
||||
import feign.Client;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient;
|
||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisFeignBeanPostProcessor}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisFeignBeanPostProcessorTest {
|
||||
|
||||
private PolarisFeignBeanPostProcessor polarisFeignBeanPostProcessor;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
ConsumerAPI consumerAPI = mock(ConsumerAPI.class);
|
||||
|
||||
polarisFeignBeanPostProcessor = new PolarisFeignBeanPostProcessor(consumerAPI);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPostProcessBeforeInitialization() {
|
||||
BeanFactory beanFactory = mock(BeanFactory.class);
|
||||
doAnswer(invocation -> {
|
||||
Class<?> clazz = invocation.getArgument(0);
|
||||
if (clazz.equals(BlockingLoadBalancerClient.class)) {
|
||||
return mock(BlockingLoadBalancerClient.class);
|
||||
}
|
||||
if (clazz.equals(CachingSpringLoadBalancerFactory.class)) {
|
||||
return mock(CachingSpringLoadBalancerFactory.class);
|
||||
}
|
||||
if (clazz.equals(SpringClientFactory.class)) {
|
||||
return mock(SpringClientFactory.class);
|
||||
}
|
||||
return null;
|
||||
}).when(beanFactory).getBean(any(Class.class));
|
||||
polarisFeignBeanPostProcessor.setBeanFactory(beanFactory);
|
||||
|
||||
// isNeedWrap(bean) == false
|
||||
Object bean1 = new Object();
|
||||
Object bean = polarisFeignBeanPostProcessor.postProcessBeforeInitialization(bean1, "bean1");
|
||||
assertThat(bean).isNotInstanceOfAny(
|
||||
PolarisFeignClient.class,
|
||||
PolarisLoadBalancerFeignClient.class,
|
||||
PolarisFeignBlockingLoadBalancerClient.class);
|
||||
|
||||
// bean instanceOf Client.class
|
||||
Client bean2 = mock(Client.class);
|
||||
bean = polarisFeignBeanPostProcessor.postProcessBeforeInitialization(bean2, "bean2");
|
||||
assertThat(bean).isInstanceOf(PolarisFeignClient.class);
|
||||
|
||||
// bean instanceOf LoadBalancerFeignClient.class
|
||||
LoadBalancerFeignClient bean3 = mock(LoadBalancerFeignClient.class);
|
||||
doReturn(mock(Client.class)).when(bean3).getDelegate();
|
||||
bean = polarisFeignBeanPostProcessor.postProcessBeforeInitialization(bean3, "bean3");
|
||||
assertThat(bean).isInstanceOf(PolarisLoadBalancerFeignClient.class);
|
||||
|
||||
// bean instanceOf FeignBlockingLoadBalancerClient.class
|
||||
FeignBlockingLoadBalancerClient bean4 = mock(FeignBlockingLoadBalancerClient.class);
|
||||
doReturn(mock(Client.class)).when(bean4).getDelegate();
|
||||
bean = polarisFeignBeanPostProcessor.postProcessBeforeInitialization(bean4, "bean4");
|
||||
assertThat(bean).isInstanceOf(PolarisFeignBlockingLoadBalancerClient.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.circuitbreaker.feign;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisFeignBlockingLoadBalancerClient}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisFeignBlockingLoadBalancerClientTest {
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
try {
|
||||
new PolarisFeignBlockingLoadBalancerClient(null, null);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assertions.fail("Exception encountered.", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.circuitbreaker.feign;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test for {@link PolarisLoadBalancerFeignClient}.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisLoadBalancerFeignClientTest {
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
try {
|
||||
new PolarisLoadBalancerFeignClient(null, null, null);
|
||||
}
|
||||
catch (Exception e) {
|
||||
Assertions.fail("Exception encountered.", e);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* 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.circuitbreaker.feign;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* Test application.
|
||||
*
|
||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients
|
||||
public class TestPolarisFeignApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestPolarisFeignApp.class);
|
||||
}
|
||||
|
||||
@FeignClient(name = "feign-service-polaris",
|
||||
fallback = TestPolarisServiceFallback.class)
|
||||
public interface TestPolarisService {
|
||||
|
||||
/**
|
||||
* Get info of service B.
|
||||
*
|
||||
* @return info
|
||||
*/
|
||||
@GetMapping("/example/service/b/info")
|
||||
String info();
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
public static class TestPolarisServiceFallback implements TestPolarisService {
|
||||
|
||||
@Override
|
||||
public String info() {
|
||||
return "trigger the refuse";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue