@Test
public void testInvokeMetricsService() {

    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[0], new Object[0]);
    RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE)
        .addParameter(TIMEOUT_KEY, 300));
    AppResponse response = AppResponseBuilder.create()
        .build();
    onInvokeReturns(response);
    for (int i = 0; i < 50; i++) {
        try {
            metricsFilter.invoke(serviceInvoker, invocation);
            metricsFilter.invoke(timeoutInvoker, invocation);
        } catch (RpcException e) {
            //ignore
        }
    }
    Protocol protocol = new DubboProtocol();
    URL url = URL.valueOf("dubbo://" + NetUtils.getLocalAddress().getHostName() + ":20880/" + MetricsService.class.getName());
    Invoker<MetricsService> invoker = protocol.refer(MetricsService.class, url);
    invocation = new RpcInvocation("getMetricsByGroup", DemoService.class.getName(), "", new Class<?>[]{String.class}, new Object[]{DUBBO_GROUP});
    try {
        Thread.sleep(5000);
    } catch (Exception e) {
        // ignore
    }
    String resStr = invoker.invoke(invocation).getValue().toString();
    List<MetricObject> metricObjectList = new Gson().fromJson(resStr, new TypeToken<List<MetricObject>>() {
    }.getType());
    Map<String, Object> metricMap = new HashMap<>();
    for (int i = 0; i < metricObjectList.size(); i++) {
        MetricObject object = metricObjectList.get(i);
        String metric = object.getMetric().substring(object.getMetric().lastIndexOf(".") + 1);
        if ((double) object.getValue() > 0.0 && object.getMetricLevel().equals(MetricLevel.MAJOR))
            metricMap.put(metric, object.getValue());
    }
    Assertions.assertEquals(50.0, metricMap.get("success_bucket_count"));
    Assertions.assertEquals(50.0, metricMap.get("timeoutError_bucket_count"));
    Assertions.assertEquals(100.0, metricMap.get("bucket_count"));
    Assertions.assertEquals(100.0 / 5, metricMap.get("qps"));
    Assertions.assertEquals(50.0 / 100.0, metricMap.get("success_rate"));
}