This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 3777209 [ISSUE #2366] clean up optimize [shenyu-loadbalancer] test
code. (#2384)
3777209 is described below
commit 3777209fc76800175df3962213ca1febe57edca6
Author: haibo.duan <[email protected]>
AuthorDate: Wed Nov 17 16:53:18 2021 +0800
[ISSUE #2366] clean up optimize [shenyu-loadbalancer] test code. (#2384)
---
.../loadbalancer/cache/UpstreamCheckTaskTest.java | 32 +++++++++++++++-------
.../factory/LoadBalancerFactoryTest.java | 13 +++++----
.../spi/RoundRobinLoadBalanceTest.java | 13 +++++----
3 files changed, 36 insertions(+), 22 deletions(-)
diff --git
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
index 11bd9ef..da1e7fb 100644
---
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
+++
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/cache/UpstreamCheckTaskTest.java
@@ -40,13 +40,15 @@ public class UpstreamCheckTaskTest {
* Here to set interval with 50s to avoid running the second time.
*/
private UpstreamCheckTask healthCheckTask = new UpstreamCheckTask(50000);
-
+
/**
* Test run.
*/
@Test(timeout = 30000)
public void testRun() {
- // Mock selectorId1~selectorId4 to let it coverage 4 branch of
`HealthCheckTask#check` method
+ /**
+ * Mock selectorId1~selectorId4 to let it coverage 4 branch of
`HealthCheckTask#check` method.
+ */
final String selectorId1 = "s1";
SelectorData selectorData1 = mock(SelectorData.class);
final String selectorId2 = "s2";
@@ -61,10 +63,12 @@ public class UpstreamCheckTaskTest {
when(selectorData3.getId()).thenReturn(selectorId3);
when(selectorData4.getId()).thenReturn(selectorId4);
- // Let it coverage line 165~175
- // We should use powermock or mockito to mock static method of
`UpstreamCheckUtils.checkUrl`,
- // But mocked static method is not valid across thread. Because
`UpstreamCheckUtils.checkUrl` is called in
- // HealthCheckTask inner thread pool, but mocked in current thread. So
we turn to do like below.
+ /**
+ * Let it coverage line 165~175
+ * We should use powermock or mockito to mock static method of
`UpstreamCheckUtils.checkUrl`,
+ * But mocked static method is not valid across thread. Because
`UpstreamCheckUtils.checkUrl` is called in
+ * HealthCheckTask inner thread pool, but mocked in current thread. So
we turn to do like below.
+ */
when(upstream.getUrl()).thenReturn("");
when(upstream.isHealthy()).thenReturn(true).thenReturn(false);
@@ -73,14 +77,22 @@ public class UpstreamCheckTaskTest {
healthCheckTask.triggerAddOne(selectorData3.getId(), upstream);
healthCheckTask.triggerAddOne(selectorData4.getId(), upstream);
healthCheckTask.schedule();
- // Wait for the upstream-health-check thread to start.
+ /**
+ * Wait for the upstream-health-check thread to start.
+ */
Awaitility.await().pollDelay(3, TimeUnit.SECONDS).untilAsserted(() ->
assertFalse(healthCheckTask.getCheckStarted().get()));
assertTrue(healthCheckTask.getUnhealthyUpstream().get(selectorId1).size() > 0);
- // Let it coverage line 151~163
+ /**
+ * Let it coverage line 151~163.
+ */
when(upstream.isHealthy()).thenReturn(false).thenReturn(true);
- // Even if the address could not connect, it will return false, that
mean it will not coverage 151~163.
+ /**
+ * Even if the address could not connect, it will return false, that
mean it will not coverage 151~163.
+ */
when(upstream.getUrl()).thenReturn("http://www.baidu.com");
- // Manually run one time
+ /**
+ * Manually run one time
+ */
healthCheckTask.run();
Awaitility.await().pollDelay(1, TimeUnit.SECONDS).untilAsserted(() ->
assertFalse(healthCheckTask.getCheckStarted().get()));
assertTrue(healthCheckTask.getHealthyUpstream().get(selectorId1).size() > 0);
diff --git
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/factory/LoadBalancerFactoryTest.java
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/factory/LoadBalancerFactoryTest.java
index 82ccff7..da30c7a 100644
---
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/factory/LoadBalancerFactoryTest.java
+++
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/factory/LoadBalancerFactoryTest.java
@@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
@@ -45,11 +46,11 @@ public final class LoadBalancerFactoryTest {
.build())
.collect(Collectors.toList());
Map<String, Integer> countMap = new HashMap<>();
- for (int i = 0; i < 120; i++) {
+ IntStream.range(0, 120).forEach(i -> {
Upstream result = LoadBalancerFactory.selector(upstreamList,
"roundRobin", "");
int count = countMap.getOrDefault(result.getUrl(), 0);
countMap.put(result.getUrl(), ++count);
- }
+ });
Assert.assertEquals(12, countMap.get("upstream-10").intValue());
}
@@ -63,11 +64,11 @@ public final class LoadBalancerFactoryTest {
.build())
.collect(Collectors.toList());
Map<String, Integer> countMap = new HashMap<>();
- for (int i = 0; i < 120; i++) {
+ IntStream.range(0, 120).forEach(i -> {
Upstream result = LoadBalancerFactory.selector(upstreamList,
"roundRobin", "");
int count = countMap.getOrDefault(result.getUrl(), 0);
countMap.put(result.getUrl(), ++count);
- }
+ });
Assert.assertEquals(12, countMap.get("upstream-10").intValue());
}
@@ -81,11 +82,11 @@ public final class LoadBalancerFactoryTest {
.build())
.collect(Collectors.toList());
Map<String, Integer> countMap = new HashMap<>();
- for (int i = 0; i < 120; i++) {
+ IntStream.range(0, 120).forEach(i -> {
Upstream result = LoadBalancerFactory.selector(upstreamList,
"roundRobin", "");
int count = countMap.getOrDefault(result.getUrl(), 0);
countMap.put(result.getUrl(), ++count);
- }
+ });
Assert.assertEquals(12, countMap.get("upstream-10").intValue());
}
}
diff --git
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java
index 0dbfed6..532cadd 100644
---
a/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java
+++
b/shenyu-loadbalancer/src/test/java/org/apache/shenyu/loadbalancer/spi/RoundRobinLoadBalanceTest.java
@@ -25,6 +25,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
@@ -47,11 +48,11 @@ public final class RoundRobinLoadBalanceTest {
RoundRobinLoadBalancer roundRobinLoadBalancer = new
RoundRobinLoadBalancer();
Map<String, Integer> countMap = new HashMap<>();
- for (int i = 0; i < 120; i++) {
+ IntStream.range(0, 120).forEach(i -> {
Upstream result = roundRobinLoadBalancer.select(upstreamList, "");
int count = countMap.getOrDefault(result.getUrl(), 0);
countMap.put(result.getUrl(), ++count);
- }
+ });
Assert.assertEquals(60, countMap.get("upstream-50").intValue());
}
@@ -67,11 +68,11 @@ public final class RoundRobinLoadBalanceTest {
RoundRobinLoadBalancer roundRobinLoadBalancer = new
RoundRobinLoadBalancer();
Map<String, Integer> countMap = new HashMap<>();
- for (int i = 0; i < 120; i++) {
+ IntStream.range(0, 120).forEach(i -> {
Upstream result = roundRobinLoadBalancer.select(upstreamList, "");
int count = countMap.getOrDefault(result.getUrl(), 0);
countMap.put(result.getUrl(), ++count);
- }
+ });
Assert.assertEquals(60, countMap.get("upstream-50").intValue());
}
@@ -87,11 +88,11 @@ public final class RoundRobinLoadBalanceTest {
RoundRobinLoadBalancer roundRobinLoadBalancer = new
RoundRobinLoadBalancer();
Map<String, Integer> countMap = new HashMap<>();
- for (int i = 0; i < 120; i++) {
+ IntStream.range(0, 120).forEach(i -> {
Upstream result = roundRobinLoadBalancer.select(upstreamList, "");
int count = countMap.getOrDefault(result.getUrl(), 0);
countMap.put(result.getUrl(), ++count);
- }
+ });
Assert.assertEquals(60, countMap.get("upstream-50").intValue());
}
}