Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MetricsFilterTest to use Mockito #4398

Merged
merged 14 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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.monitor.dubbo;

import org.apache.dubbo.rpc.AppResponse;

import java.util.Map;

public class AppResponseBuilder {
private Object result;
private Throwable exception;
private Map<String, String> attachments;
private AppResponse appResponse;

private AppResponseBuilder() {
this.appResponse = new AppResponse();
}

public static AppResponseBuilder create() {
return new AppResponseBuilder();
}

public AppResponse build() {
return new AppResponse(this);
}

public AppResponseBuilder withResult(Object result) {
this.result = result;
return this;
}

public AppResponseBuilder withException(Throwable exception) {
this.exception = exception;
return this;
}

public AppResponseBuilder withAttachments(Map<String, String> attachments) {
this.attachments = attachments;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.monitor.MetricsService;
import org.apache.dubbo.monitor.dubbo.service.DemoService;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
Expand All @@ -37,8 +38,11 @@
import com.alibaba.metrics.common.MetricObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.List;
Expand All @@ -57,31 +61,32 @@
import static org.apache.dubbo.monitor.Constants.METHOD;
import static org.apache.dubbo.monitor.Constants.SERVICE;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;

public class MetricsFilterTest {

private final Invoker<DemoService> serviceInvoker = new Invoker<DemoService>() {
@Override
public Class<DemoService> getInterface() {
return DemoService.class;
}
private Invoker<DemoService> serviceInvoker;

public URL getUrl() {
return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/org.apache.dubbo.monitor.dubbo.service.DemoService");
}
@BeforeEach
void setUp() {
serviceInvoker = mock(Invoker.class);

@Override
public boolean isAvailable() {
return false;
}
given(serviceInvoker.isAvailable()).willReturn(false);
given(serviceInvoker.getInterface()).willReturn(DemoService.class);
given(serviceInvoker.getUrl()).willReturn(getUrl());
given(serviceInvoker.invoke(Mockito.any(Invocation.class))).willReturn(null);
doNothing().when(serviceInvoker).destroy();
}

public Result invoke(Invocation invocation) throws RpcException {
return null;
}
private URL getUrl() {
return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/org.apache.dubbo.monitor.dubbo.service.DemoService");
}

@Override
public void destroy() {
}
};
private void onInvokeReturns(AppResponse response) {
given(serviceInvoker.invoke(Mockito.any(Invocation.class))).willReturn(response);
}

private final Invoker<DemoService> timeoutInvoker = new Invoker<DemoService>() {
@Override
Expand Down Expand Up @@ -115,6 +120,9 @@ public void testConsumerSuccess() throws Exception {
Invocation invocation = new RpcInvocation("sayName", new Class<?>[]{Integer.class}, new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE));
AppResponse response = AppResponseBuilder.create()
.build();
onInvokeReturns(response);
for (int i = 0; i < 100; i++) {
metricsFilter.invoke(serviceInvoker, invocation);
}
Expand All @@ -140,7 +148,10 @@ public void testConsumerTimeout() {
Invocation invocation = new RpcInvocation("timeoutException", null, null);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(timeoutInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE)
.addParameter(TIMEOUT_KEY, 300));
.addParameter(TIMEOUT_KEY, 300));
AppResponse response = AppResponseBuilder.create()
.build();
onInvokeReturns(response);
for (int i = 0; i < 10; i++) {
try {
metricsFilter.invoke(timeoutInvoker, invocation);
Expand Down Expand Up @@ -170,6 +181,9 @@ public void testProviderSuccess() throws Exception {
Invocation invocation = new RpcInvocation("sayName", 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));
AppResponse response = AppResponseBuilder.create()
.build();
onInvokeReturns(response);
for (int i = 0; i < 100; i++) {
metricsFilter.invoke(serviceInvoker, invocation);
}
Expand All @@ -194,7 +208,10 @@ public void testInvokeMetricsService() {
Invocation invocation = new RpcInvocation("sayName", 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));
.addParameter(TIMEOUT_KEY, 300));
AppResponse response = AppResponseBuilder.create()
.build();
onInvokeReturns(response);
for (int i = 0; i < 50; i++) {
try {
metricsFilter.invoke(serviceInvoker, invocation);
Expand Down Expand Up @@ -239,7 +256,10 @@ public void testInvokeMetricsMethodService() {
Invocation echoInvocation = new RpcInvocation("echo", new Class<?>[]{Integer.class}, new Integer[]{1});
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, PROVIDER_SIDE)
.addParameter(TIMEOUT_KEY, 300));
.addParameter(TIMEOUT_KEY, 300));
AppResponse response = AppResponseBuilder.create()
.build();
onInvokeReturns(response);
for (int i = 0; i < 50; i++) {
metricsFilter.invoke(serviceInvoker, sayNameInvocation);
metricsFilter.invoke(serviceInvoker, echoInvocation);
Expand Down Expand Up @@ -282,23 +302,23 @@ public void testInvokeMetricsMethodService() {
}

Assertions.assertEquals(50.0,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_bucket_count"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_bucket_count"));
Assertions.assertEquals(50.0,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_bucket_count"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_bucket_count"));

Assertions.assertEquals(50.0,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("timeoutError_bucket_count"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("timeoutError_bucket_count"));
Assertions.assertEquals(50.0,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("timeoutError_bucket_count"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("timeoutError_bucket_count"));

Assertions.assertEquals(100.0 / 15,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("qps"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("qps"));
Assertions.assertEquals(100.0 / 15,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("qps"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("qps"));

Assertions.assertEquals(50.0 / 100.0,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_rate"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void sayName()").get("success_rate"));
Assertions.assertEquals(50.0 / 100.0,
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_rate"));
methodMetricMap.get("org.apache.dubbo.monitor.dubbo.service.DemoService.void echo(Integer)").get("success_rate"));
}
}