Skip to content

Commit

Permalink
optimize unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
carryxyh committed Sep 6, 2018
1 parent ae0b967 commit 5ebab8b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.loadbalance;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -86,11 +87,10 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
for (int i = 0; i < length; i++) {
Invoker<T> invoker = invokers.get(i);

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

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

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

// mock weight is invoker's url.getPort
offsetWeight -= invokers.get(leastIndex).getUrl().getPort();
offsetWeight -= invokers.get(leastIndex).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);

if (offsetWeight <= 0)
return invokers.get(leastIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ public void before() throws Exception {
weightInvoker3 = mock(Invoker.class);

URL url1 = URL.valueOf("test1://0:1/DemoService");
url1 = url1.addParameter(Constants.WEIGHT_KEY, 1);
url1 = url1.addParameter("active", 0);
URL url2 = URL.valueOf("test2://0:9/DemoService");
url2 = url2.addParameter(Constants.WEIGHT_KEY, 9);
url2 = url2.addParameter("active", 0);
URL url3 = URL.valueOf("test3://1:6/DemoService");
url3 = url3.addParameter(Constants.WEIGHT_KEY, 6);
url3 = url3.addParameter("active", 1);

given(weightInvoker1.isAvailable()).willReturn(true);
given(weightInvoker1.getUrl()).willReturn(url1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.loadbalance;

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

// mock weight
int weight = invokers.get(i).getUrl().getPort();
int weight = invokers.get(i).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);

totalWeight += weight; // Sum
if (sameWeight && i > 0
&& weight != invokers.get(i - 1).getUrl().getPort()) {
&& weight != invokers.get(i - 1).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT)) {
sameWeight = false;
}
}
Expand All @@ -116,7 +117,7 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
int offset = random.nextInt(totalWeight);
// Return a invoker based on the random value.
for (int i = 0; i < length; i++) {
offset -= invokers.get(i).getUrl().getPort();
offset -= invokers.get(i).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
if (offset < 0) {
return invokers.get(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.cluster.loadbalance;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.AtomicPositiveInteger;
import org.apache.dubbo.rpc.Invocation;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation
int weightSum = 0;
for (int i = 0; i < length; i++) {

int weight = invokers.get(i).getUrl().getPort();
int weight = invokers.get(i).getUrl().getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);

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

0 comments on commit 5ebab8b

Please sign in to comment.