-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from SkyeBeFreeman/main
test:add junit test to polaris-circuitbreaker.
- Loading branch information
Showing
12 changed files
with
380 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...ava/com/tencent/cloud/polaris/circuitbreaker/PolarisFeignClientAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
...ava/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignBeanPostProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...encent/cloud/polaris/circuitbreaker/feign/PolarisFeignBlockingLoadBalancerClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.