AbstractLoadBalance implements LoadBalance { int getWeight(Invoker<?> invoker, Invocation invocation) { int weight; URL url = invoker.getUrl(); if (REGISTRY_SERVICE_REFERENCE_PATH.equals(url.getServiceInterface())) { weight = url.getParameter(REGISTRY_KEY + "." + WEIGHT_KEY, DEFAULT_WEIGHT); } else { weight = url.getMethodParameter(invocation.getMethodName(), WEIGHT_KEY, DEFAULT_WEIGHT); if (weight > 0) { long timestamp = invoker.getUrl().getParameter(TIMESTAMP_KEY, 0L); if (timestamp > 0L) { long uptime = System.currentTimeMillis() - timestamp; if (uptime < 0) { return 1; } int warmup = invoker.getUrl().getParameter(WARMUP_KEY, DEFAULT_WARMUP); if (uptime > 0 && uptime < warmup) { weight = calculateWarmupWeight((int)uptime, warmup, weight); } } } } return Math.max(weight, 0); }  @Override Invoker<T> select(List<Invoker<T>> invokers, URL url, Invocation invocation);  }
@Test public void testGetWeight() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("say"); Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly()); URL url1 = new URL("", "", 0, "DemoService", 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, "DemoService", 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)); }
@Test public void testGetRegistryWeight() { RpcInvocation invocation = new RpcInvocation(); invocation.setMethodName("say"); Invoker invoker1 = mock(Invoker.class, Mockito.withSettings().stubOnly()); URL url1 = new URL("", "", 0, "DemoService", new HashMap<>()); url1 = url1.addParameter(REGISTRY_KEY + "." + WEIGHT_KEY, 10); given(invoker1.getUrl()).willReturn(url1); Invoker invoker2 = mock(Invoker.class, Mockito.withSettings().stubOnly()); URL url2 = new URL("", "", 0, "org.apache.dubbo.registry.RegistryService", new HashMap<>()); url2 = url2.addParameter(REGISTRY_KEY + "." + WEIGHT_KEY, 20); given(invoker2.getUrl()).willReturn(url2); Assertions.assertEquals(100, balance.getWeight(invoker1, invocation)); Assertions.assertEquals(20, balance.getWeight(invoker2, invocation)); }