This is an automated email from the ASF dual-hosted git repository. huxing 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 4c4b931 Refactor MetricsFilterTest to use Mockito (#4398) 4c4b931 is described below commit 4c4b931ebfbfae7aa551342cf0f48c270dfebadd Author: Daniela Marques de Morais <danielamor...@tuta.io> AuthorDate: Wed Jul 3 11:57:06 2019 -0300 Refactor MetricsFilterTest to use Mockito (#4398) * Create constants for 'hash.names' and 'hash.arguments' (#3744) * Fix conflicts of cherry-pick * Add builder class for unit test * Fix builder class * Add apache license --- .../dubbo/monitor/dubbo/AppResponseBuilder.java | 55 +++++++++++++++ .../dubbo/monitor/dubbo/MetricsFilterTest.java | 80 ++++++++++++++-------- 2 files changed, 105 insertions(+), 30 deletions(-) diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/AppResponseBuilder.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/AppResponseBuilder.java new file mode 100644 index 0000000..737572a --- /dev/null +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/AppResponseBuilder.java @@ -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; + } +} diff --git a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java index 47b607d..c31bd4a 100644 --- a/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java +++ b/dubbo-monitor/dubbo-monitor-default/src/test/java/org/apache/dubbo/monitor/dubbo/MetricsFilterTest.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL; 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; @@ -37,8 +38,11 @@ import com.alibaba.metrics.MetricName; 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; @@ -57,31 +61,32 @@ import static org.apache.dubbo.monitor.Constants.DUBBO_PROVIDER_METHOD; 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 @@ -115,6 +120,9 @@ public class MetricsFilterTest { 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); } @@ -140,7 +148,10 @@ public class MetricsFilterTest { 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); @@ -170,6 +181,9 @@ public class MetricsFilterTest { 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); } @@ -194,7 +208,10 @@ public class MetricsFilterTest { 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); @@ -239,7 +256,10 @@ public class MetricsFilterTest { 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); @@ -282,23 +302,23 @@ public class MetricsFilterTest { } 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")); } }