You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by il...@apache.org on 2019/08/20 06:44:31 UTC

[dubbo] branch master updated: remove useless if check of max value of Integer (#4886)

This is an automated email from the ASF dual-hosted git repository.

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new dfa6eb9  remove useless if check of max value of Integer (#4886)
dfa6eb9 is described below

commit dfa6eb914bc2973ad25f9ed56578b28bba822d62
Author: Taosheng Wei <ts...@163.com>
AuthorDate: Tue Aug 20 14:44:26 2019 +0800

    remove useless if check of max value of Integer (#4886)
---
 .../cluster/loadbalance/AbstractLoadBalance.java   |  6 +--
 .../loadbalance/AbstractLoadBalanceTest.java       | 60 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java
index 3ae6403..fed602d 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalance.java
@@ -76,10 +76,7 @@ public abstract class AbstractLoadBalance implements LoadBalance {
             long timestamp = invoker.getUrl().getParameter(TIMESTAMP_KEY, 0L);
             if (timestamp > 0L) {
                 long uptime = System.currentTimeMillis() - timestamp;
-                if (uptime >= Integer.MAX_VALUE) {
-                    return weight;
-                }
-                else if (uptime < 0) {
+                if (uptime < 0) {
                     return 1;
                 }
                 int warmup = invoker.getUrl().getParameter(WARMUP_KEY, DEFAULT_WARMUP);
@@ -90,5 +87,4 @@ public abstract class AbstractLoadBalance implements LoadBalance {
         }
         return Math.max(weight, 0);
     }
-
 }
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalanceTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalanceTest.java
new file mode 100644
index 0000000..678fba7
--- /dev/null
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/AbstractLoadBalanceTest.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 org.apache.dubbo.rpc.cluster.loadbalance;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.HashMap;
+import java.util.List;
+
+import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
+import static org.mockito.BDDMockito.given;
+import static org.mockito.Mockito.mock;
+
+public class AbstractLoadBalanceTest {
+
+    private AbstractLoadBalance balance = new AbstractLoadBalance() {
+        @Override
+        protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
+            return null;
+        }
+    };
+
+    @Test
+    public void testGetWeight() {
+        RpcInvocation invocation = new RpcInvocation();
+        invocation.setMethodName("say");
+
+        Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly());
+        URL url1 = new URL("", "", 0, new HashMap<>());
+        url1 = url1.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - Integer.MAX_VALUE - 1);
+        given(invoker1.getUrl()).willReturn(url1);
+
+        Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly());
+        URL url2 = new URL("", "", 0, new HashMap<>());
+        url2 = url2.addParameter(TIMESTAMP_KEY, System.currentTimeMillis() - 10 * 60 * 1000L - 1);
+        given(invoker2.getUrl()).willReturn(url2);
+
+        Assertions.assertEquals(balance.getWeight(invoker1, invocation), balance.getWeight(invoker2, invocation));
+    }
+}