Skip to content

Commit 5ebab8b

Browse files
committed
optimize unit test
1 parent ae0b967 commit 5ebab8b

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveBalanceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.loadbalance;
1818

19+
import org.apache.dubbo.common.Constants;
1920
import org.apache.dubbo.common.URL;
2021
import org.apache.dubbo.rpc.Invocation;
2122
import org.apache.dubbo.rpc.Invoker;
@@ -86,11 +87,10 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
8687
for (int i = 0; i < length; i++) {
8788
Invoker<T> invoker = invokers.get(i);
8889

89-
// mock active is invoker's url.getHost
90-
int active = Integer.valueOf(invoker.getUrl().getHost()); // Active number
90+
// Active number
91+
int active = invoker.getUrl().getParameter("active", Constants.DEFAULT_WEIGHT);
9192

92-
// mock weight is invoker's url.getPort
93-
int afterWarmup = invoker.getUrl().getPort();
93+
int afterWarmup = invoker.getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
9494

9595
if (leastActive == -1 || active < leastActive) { // Restart, when find a invoker having smaller least active value.
9696
leastActive = active; // Record the current least active value
@@ -121,8 +121,8 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
121121
for (int i = 0; i < leastCount; i++) {
122122
int leastIndex = leastIndexs[i];
123123

124-
// mock weight is invoker's url.getPort
125-
offsetWeight -= invokers.get(leastIndex).getUrl().getPort();
124+
offsetWeight -= invokers.get(leastIndex).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
125+
126126
if (offsetWeight <= 0)
127127
return invokers.get(leastIndex);
128128
}

dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LoadBalanceBaseTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ public void before() throws Exception {
159159
weightInvoker3 = mock(Invoker.class);
160160

161161
URL url1 = URL.valueOf("test1://0:1/DemoService");
162+
url1 = url1.addParameter(Constants.WEIGHT_KEY, 1);
163+
url1 = url1.addParameter("active", 0);
162164
URL url2 = URL.valueOf("test2://0:9/DemoService");
165+
url2 = url2.addParameter(Constants.WEIGHT_KEY, 9);
166+
url2 = url2.addParameter("active", 0);
163167
URL url3 = URL.valueOf("test3://1:6/DemoService");
168+
url3 = url3.addParameter(Constants.WEIGHT_KEY, 6);
169+
url3 = url3.addParameter("active", 1);
164170

165171
given(weightInvoker1.isAvailable()).willReturn(true);
166172
given(weightInvoker1.getUrl()).willReturn(url1);

dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RandomLoadBalanceTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.loadbalance;
1818

19+
import org.apache.dubbo.common.Constants;
1920
import org.apache.dubbo.common.URL;
2021
import org.apache.dubbo.rpc.Invocation;
2122
import org.apache.dubbo.rpc.Invoker;
@@ -103,11 +104,11 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
103104
for (int i = 0; i < length; i++) {
104105

105106
// mock weight
106-
int weight = invokers.get(i).getUrl().getPort();
107+
int weight = invokers.get(i).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
107108

108109
totalWeight += weight; // Sum
109110
if (sameWeight && i > 0
110-
&& weight != invokers.get(i - 1).getUrl().getPort()) {
111+
&& weight != invokers.get(i - 1).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT)) {
111112
sameWeight = false;
112113
}
113114
}
@@ -116,7 +117,7 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
116117
int offset = random.nextInt(totalWeight);
117118
// Return a invoker based on the random value.
118119
for (int i = 0; i < length; i++) {
119-
offset -= invokers.get(i).getUrl().getPort();
120+
offset -= invokers.get(i).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
120121
if (offset < 0) {
121122
return invokers.get(i);
122123
}

dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/RoundRobinLoadBalanceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.rpc.cluster.loadbalance;
1818

19+
import org.apache.dubbo.common.Constants;
1920
import org.apache.dubbo.common.URL;
2021
import org.apache.dubbo.common.utils.AtomicPositiveInteger;
2122
import org.apache.dubbo.rpc.Invocation;
@@ -89,7 +90,7 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
8990
int weightSum = 0;
9091
for (int i = 0; i < length; i++) {
9192

92-
int weight = invokers.get(i).getUrl().getPort();
93+
int weight = invokers.get(i).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
9394

9495
maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight
9596
minWeight = Math.min(minWeight, weight); // Choose the minimum weight

0 commit comments

Comments
 (0)