Skip to content

Commit

Permalink
add test for monitor module (apache#1741)
Browse files Browse the repository at this point in the history
  • Loading branch information
htynkn authored and beiwei30 committed May 14, 2018
1 parent 2ff0fcb commit 46e5f8b
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.alibaba.dubbo.monitor.Monitor;
import com.alibaba.dubbo.monitor.MonitorFactory;

import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.RpcInvocation;

import junit.framework.Assert;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
* MonitorFilterTest
*/
Expand Down Expand Up @@ -125,6 +131,20 @@ public void testFilter() throws Exception {
Assert.assertEquals(invocation, lastInvocation);
}

@Test
public void testSkipMonitorIfNotHasKey() {
MonitorFilter monitorFilter = new MonitorFilter();
MonitorFactory mockMonitorFactory = mock(MonitorFactory.class);
monitorFilter.setMonitorFactory(mockMonitorFactory);
Invocation invocation = new RpcInvocation("aaa", new Class<?>[0], new Object[0]);
Invoker invoker = mock(Invoker.class);
given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880?" + Constants.APPLICATION_KEY + "=abc&" + Constants.SIDE_KEY + "=" + Constants.CONSUMER_SIDE));

monitorFilter.invoke(invoker, invocation);

verify(mockMonitorFactory, never()).getMonitor(any(URL.class));
}

@Test
public void testGenericFilter() throws Exception {
MonitorFilter monitorFilter = new MonitorFilter();
Expand All @@ -147,4 +167,17 @@ public void testGenericFilter() throws Exception {
Assert.assertEquals(invocation, lastInvocation);
}

@Test
public void testSafeFailForMonitorCollectFail() {
MonitorFilter monitorFilter = new MonitorFilter();
MonitorFactory mockMonitorFactory = mock(MonitorFactory.class);
Monitor mockMonitor = mock(Monitor.class);
Mockito.doThrow(new RuntimeException()).when(mockMonitor).collect(any(URL.class));

monitorFilter.setMonitorFactory(mockMonitorFactory);
given(mockMonitorFactory.getMonitor(any(URL.class))).willReturn(mockMonitor);
Invocation invocation = new RpcInvocation("aaa", new Class<?>[0], new Object[0]);

monitorFilter.invoke(serviceInvoker, invocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 com.alibaba.dubbo.monitor.dubbo;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.monitor.Monitor;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.ProxyFactory;
import com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;

public class DubboMonitorFactoryTest {
private DubboMonitorFactory dubboMonitorFactory;
@Mock
private ProxyFactory proxyFactory;

@Before
public void setUp() throws Exception {
initMocks(this);
this.dubboMonitorFactory = new DubboMonitorFactory();
this.dubboMonitorFactory.setProtocol(new DubboProtocol());
this.dubboMonitorFactory.setProxyFactory(proxyFactory);
}

@Test
public void testCreateMonitor() {
URL urlWithoutPath = URL.valueOf("http://10.10.10.11");
Monitor monitor = dubboMonitorFactory.createMonitor(urlWithoutPath);
assertThat(monitor, not(nullValue()));

URL urlWithFilterKey = URL.valueOf("http://10.10.10.11/").addParameter(Constants.REFERENCE_FILTER_KEY, "testFilter");
monitor = dubboMonitorFactory.createMonitor(urlWithFilterKey);

assertThat(monitor, not(nullValue()));
ArgumentCaptor<Invoker> invokerArgumentCaptor = ArgumentCaptor.forClass(Invoker.class);
verify(proxyFactory, atLeastOnce()).getProxy(invokerArgumentCaptor.capture());

Invoker invoker = invokerArgumentCaptor.getValue();
assertThat(invoker.getUrl().getParameter(Constants.REFERENCE_FILTER_KEY), containsString("testFilter"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@
import com.alibaba.dubbo.rpc.ProxyFactory;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcException;

import junit.framework.Assert;
import org.hamcrest.CustomMatcher;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.Arrays;
import java.util.List;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
* DubboMonitorTest
*/
Expand Down Expand Up @@ -159,4 +171,71 @@ public void testMonitorFactory() throws Exception {
}
}

@Test
public void testAvailable() {
Invoker invoker = mock(Invoker.class);
MonitorService monitorService = mock(MonitorService.class);

given(invoker.isAvailable()).willReturn(true);
given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:7070?interval=20"));
DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);

assertThat(dubboMonitor.isAvailable(), is(true));
verify(invoker).isAvailable();
}

@Test
public void testSum() {
URL statistics = new URL("dubbo", "10.20.153.11", 0)
.addParameter(MonitorService.APPLICATION, "morgan")
.addParameter(MonitorService.INTERFACE, "MemberService")
.addParameter(MonitorService.METHOD, "findPerson")
.addParameter(MonitorService.CONSUMER, "10.20.153.11")
.addParameter(MonitorService.SUCCESS, 1)
.addParameter(MonitorService.FAILURE, 0)
.addParameter(MonitorService.ELAPSED, 3)
.addParameter(MonitorService.MAX_ELAPSED, 3)
.addParameter(MonitorService.CONCURRENT, 1)
.addParameter(MonitorService.MAX_CONCURRENT, 1);
Invoker invoker = mock(Invoker.class);
MonitorService monitorService = mock(MonitorService.class);

given(invoker.getUrl()).willReturn(URL.valueOf("dubbo://127.0.0.1:7070?interval=20"));
DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);

dubboMonitor.collect(statistics);
dubboMonitor.collect(statistics.addParameter(MonitorService.SUCCESS, 3).addParameter(MonitorService.CONCURRENT, 2)
.addParameter(MonitorService.INPUT, 1).addParameter(MonitorService.OUTPUT, 2));
dubboMonitor.collect(statistics.addParameter(MonitorService.SUCCESS, 6).addParameter(MonitorService.ELAPSED, 2));

dubboMonitor.send();

ArgumentCaptor<URL> summaryCaptor = ArgumentCaptor.forClass(URL.class);
verify(monitorService, atLeastOnce()).collect(summaryCaptor.capture());

List<URL> allValues = summaryCaptor.getAllValues();

assertThat(allValues, not(nullValue()));
assertThat(allValues, hasItem(new CustomMatcher<URL>("Monitor count should greater than 1") {
@Override
public boolean matches(Object item) {
URL url = (URL) item;
return Integer.valueOf(url.getParameter(MonitorService.SUCCESS)) > 1;
}
}));
}

@Test
public void testLookUp() {
Invoker invoker = mock(Invoker.class);
MonitorService monitorService = mock(MonitorService.class);

URL queryUrl = URL.valueOf("dubbo://127.0.0.1:7070?interval=20");
given(invoker.getUrl()).willReturn(queryUrl);
DubboMonitor dubboMonitor = new DubboMonitor(invoker, monitorService);

dubboMonitor.lookup(queryUrl);

verify(monitorService).lookup(eq(queryUrl));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 com.alibaba.dubbo.monitor.dubbo;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.monitor.MonitorService;
import org.junit.Assert;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

public class StatisticsTest {
@Test
public void testEquals() {
URL statistics = new URL("dubbo", "10.20.153.10", 0)
.addParameter(MonitorService.APPLICATION, "morgan")
.addParameter(MonitorService.INTERFACE, "MemberService")
.addParameter(MonitorService.METHOD, "findPerson")
.addParameter(MonitorService.CONSUMER, "10.20.153.11")
.addParameter(MonitorService.SUCCESS, 1)
.addParameter(MonitorService.FAILURE, 0)
.addParameter(MonitorService.ELAPSED, 3)
.addParameter(MonitorService.MAX_ELAPSED, 3)
.addParameter(MonitorService.CONCURRENT, 1)
.addParameter(MonitorService.MAX_CONCURRENT, 1);

Statistics statistics1 = new Statistics(statistics);
Statistics statistics2 = new Statistics(statistics);

Assert.assertThat(statistics1, equalTo(statistics1));
Assert.assertThat(statistics1, equalTo(statistics2));

statistics1.setVersion("2");
Assert.assertThat(statistics1, not(equalTo(statistics2)));
Assert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode())));

statistics1.setMethod("anotherMethod");
Assert.assertThat(statistics1, not(equalTo(statistics2)));
Assert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode())));

statistics1.setClient("anotherClient");
Assert.assertThat(statistics1, not(equalTo(statistics2)));
Assert.assertThat(statistics1.hashCode(), not(equalTo(statistics2.hashCode())));
}

@Test
public void testToString() {
Statistics statistics = new Statistics(new URL("dubbo", "10.20.153.10", 0));
statistics.setApplication("demo");
statistics.setMethod("findPerson");
statistics.setServer("10.20.153.10");
statistics.setGroup("unit-test");
statistics.setService("MemberService");
assertThat(statistics.toString(), is("dubbo://10.20.153.10"));

Statistics statisticsWithDetailInfo = new Statistics(new URL("dubbo", "10.20.153.10", 0)
.addParameter(MonitorService.APPLICATION, "morgan")
.addParameter(MonitorService.INTERFACE, "MemberService")
.addParameter(MonitorService.METHOD, "findPerson")
.addParameter(MonitorService.CONSUMER, "10.20.153.11")
.addParameter(MonitorService.GROUP, "unit-test")
.addParameter(MonitorService.SUCCESS, 1)
.addParameter(MonitorService.FAILURE, 0)
.addParameter(MonitorService.ELAPSED, 3)
.addParameter(MonitorService.MAX_ELAPSED, 3)
.addParameter(MonitorService.CONCURRENT, 1)
.addParameter(MonitorService.MAX_CONCURRENT, 1));

Assert.assertThat(statisticsWithDetailInfo.getServer(), equalTo(statistics.getServer()));
Assert.assertThat(statisticsWithDetailInfo.getService(), equalTo(statistics.getService()));
Assert.assertThat(statisticsWithDetailInfo.getMethod(), equalTo(statistics.getMethod()));

Assert.assertThat(statisticsWithDetailInfo.getGroup(), equalTo(statistics.getGroup()));
Assert.assertThat(statisticsWithDetailInfo, not(equalTo(statistics)));
}
}

0 comments on commit 46e5f8b

Please sign in to comment.