zonghaishang commented on a change in pull request #2172: Optimize 
leastActiveSelect and weight test case
URL: https://github.com/apache/incubator-dubbo/pull/2172#discussion_r219379208
 
 

 ##########
 File path: 
dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/loadbalance/LeastActiveBalanceTest.java
 ##########
 @@ -39,4 +48,119 @@ public void testLeastActiveLoadBalance_select() {
         }
     }
 
+    private List<Invoker<LoadBalanceBaseTest>> invokers = new 
ArrayList<Invoker<LoadBalanceBaseTest>>();
+    private Invoker<LoadBalanceBaseTest> invoker1;
+    private Invoker<LoadBalanceBaseTest> invoker2;
+
+    @Before
+    public void before() throws Exception {
+        invoker1 = mock(Invoker.class);
+        invoker2 = mock(Invoker.class);
+        invoker3 = mock(Invoker.class);
+
+        URL url1 = URL.valueOf("test1://0:1/DemoService");
+        URL url2 = URL.valueOf("test2://0:9/DemoService");
+        URL url3 = URL.valueOf("test3://1:6/DemoService");
+
+        given(invoker1.isAvailable()).willReturn(true);
+        given(invoker1.getUrl()).willReturn(url1);
+
+        given(invoker2.isAvailable()).willReturn(true);
+        given(invoker2.getUrl()).willReturn(url2);
+
+        given(invoker3.isAvailable()).willReturn(true);
+        given(invoker3.getUrl()).willReturn(url3);
+
+        invokers.add(invoker1);
+        invokers.add(invoker2);
+        invokers.add(invoker3);
+    }
+
+    @Test
+    public void testSelect() {
+        int sumInvoker1 = 0;
+        int sumInvoker2 = 0;
+        for (int i = 0; i < 100000; i++) {
+            MyLeastActiveLoadBalance lb = new MyLeastActiveLoadBalance();
+            Invoker selected = lb.select(invokers, null, null);
+
+            if (selected.getUrl().getProtocol().equals("test1")) {
+                sumInvoker1++;
+            }
+
+            if (selected.getUrl().getProtocol().equals("test2")) {
+                sumInvoker2++;
+            }
+            // never select invoker3 because it's active is more than invoker1 
and invoker2
+            Assert.assertTrue("select is not the least active one", 
!selected.getUrl().getProtocol().equals("test3"));
+        }
+
+        // the sumInvoker1 : sumInvoker2 approximately equal to 1: 9
+        System.out.println(sumInvoker1);
+        System.out.println(sumInvoker2);
+    }
+
+    class MyLeastActiveLoadBalance extends AbstractLoadBalance {
 
 Review comment:
   Could you reuse existing code? 
   
   
org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveBalanceTest#testLeastActiveLoadBalance_select:
   ```
   public void testLeastActiveLoadBalance_select() {
       ...
       Map<Invoker, AtomicLong> counter = getInvokeCounter(runs, 
LeastActiveLoadBalance.NAME);
       ...
   ```
   Load your repaired code this way:
   
   
`ExtensionLoader.getExtensionLoader(LoadBalance.class).getExtension(LeastActiveLoadBalance.NAME)`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to