use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ClusterSupportTest method testNotify.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testNotify() {
// 先利用registry1 通知新增2个url
final int urlsCount = 5;
final List<URL> serviceUrls1 = new ArrayList<URL>();
for (int i = 0; i < urlsCount; i++) {
URL url = new URL(MotanConstants.PROTOCOL_MOTAN, localAddress, 1000 + i, IHello.class.getName());
url.addParameter(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_SERVICE);
serviceUrls1.add(url);
}
final URL reg1Url = new URL("reg_protocol_1", NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
final URL reg2Url = new URL("reg_protocol_2", NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
mockery.checking(new Expectations() {
{
for (int i = 0; i < urlsCount; i++) {
URL serviceUrl = serviceUrls1.get(i).createCopy();
URL refererUrl = serviceUrls1.get(i).createCopy();
String application = serviceUrl.getParameter(URLParamType.application.getName(), URLParamType.application.getValue());
String module = serviceUrl.getParameter(URLParamType.module.getName(), URLParamType.module.getValue());
refererUrl.addParameters(serviceUrl.getParameters());
refererUrl.addParameter(URLParamType.application.getName(), application);
refererUrl.addParameter(URLParamType.module.getName(), module);
refererUrl.addParameter(URLParamType.check.getName(), "false");
atLeast(1).of(protocol).refer(IHello.class, refererUrl, serviceUrl);
will(returnValue(mockReferer(refererUrl)));
atLeast(1).of(mockReferer(refererUrl)).getUrl();
will(returnValue(serviceUrls1.get(i)));
atLeast(1).of(mockReferer(refererUrl)).getServiceUrl();
will(returnValue(serviceUrls1.get(i)));
}
for (int i = 0; i < 3; i++) {
atLeast(1).of(mockReferer(serviceUrls1.get(i))).destroy();
}
atLeast(1).of(registries.get(regProtocol1)).getUrl();
will(returnValue(reg1Url));
atLeast(1).of(registries.get(regProtocol2)).getUrl();
will(returnValue(reg2Url));
}
});
List copy = new ArrayList<URL>();
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 2)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
// 再利用registry1 通知有三个
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 3)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 3);
// 再利用registr1 通知有0个
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 0)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 3);
// 再利用registr1 通知有2个,少了一个
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(1, 3)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
// 再利用registry1 通知有三个
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(0, 3)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 3);
// 利用registry2,通知有2个
clusterSupport.notify(registries.get(regProtocol2).getUrl(), copy(copy, serviceUrls1.subList(3, 5)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 5);
// 再利用registr1 通知有2个,少了一个
clusterSupport.notify(registries.get(regProtocol1).getUrl(), copy(copy, serviceUrls1.subList(1, 3)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 4);
// 再利用registr1 通知有registry1没有url了
clusterSupport.notify(registries.get(regProtocol1).getUrl(), null);
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
List<Referer<IHello>> oldReferers = clusterSupport.getCluster().getReferers();
// 利用registry2,通知有2个
clusterSupport.notify(registries.get(regProtocol2).getUrl(), copy(copy, serviceUrls1.subList(3, 5)));
Assert.assertEquals(clusterSupport.getCluster().getReferers().size(), 2);
for (Referer<IHello> referer : clusterSupport.getCluster().getReferers()) {
if (!oldReferers.contains(referer)) {
Assert.assertTrue(false);
}
}
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class RefererInvocationHandlerTest method testLocalMehtod.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testLocalMehtod() throws Exception {
final Cluster cluster = mockery.mock(Cluster.class);
final URL u = new URL("motan", "local", 80, "test");
final List<Referer> referers = new ArrayList<Referer>();
final Referer referer = mockery.mock(Referer.class);
referers.add(referer);
mockery.checking(new Expectations() {
{
allowing(cluster).getUrl();
will(returnValue(u));
allowing(referer).getUrl();
will(returnValue(u));
allowing(referer).isAvailable();
will(returnValue(true));
allowing(cluster).getReferers();
will(returnValue(referers));
}
});
RefererInvocationHandler handler = new RefererInvocationHandler(TestService.class, cluster);
//local method
Method method = TestServiceImpl.class.getMethod("toString", new Class<?>[] {});
assertTrue(handler.isLocalMethod(method));
try {
String result = (String) handler.invoke(null, method, null);
assertEquals("{protocol:motan[motan://local:80/test?group=default_rpc, available:true]}", result);
} catch (Throwable e) {
assertTrue(false);
}
method = TestServiceImpl.class.getMethod("hashCode", new Class<?>[] {});
assertTrue(handler.isLocalMethod(method));
//remote method
method = TestServiceImpl.class.getMethod("hello", new Class<?>[] {});
assertFalse(handler.isLocalMethod(method));
method = TestServiceImpl.class.getMethod("equals", new Class<?>[] { Object.class });
assertFalse(handler.isLocalMethod(method));
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class RoundRobinLoadBalanceTest method testSelect.
public void testSelect() {
Request request = mockery.mock(Request.class);
mockery.checking(new Expectations() {
{
for (int i = 0; i < referers.size(); i++) {
if (i % 2 == 0) {
atLeast(0).of(referers.get(i)).isAvailable();
will(returnValue(true));
} else {
atLeast(0).of(referers.get(i)).isAvailable();
will(returnValue(false));
}
}
}
});
Referer<IHello> ref = roundRobinLoadBalance.select(request);
for (int i = 0; i < referers.size(); i++) {
if (i % 2 == 1) {
assertNotSame(ref, referers.get(i));
}
}
List<Referer<IHello>> refHolder = new ArrayList<Referer<IHello>>();
roundRobinLoadBalance.selectToHolder(request, refHolder);
assertEquals(refHolder.size(), referers.size() / 2);
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class FailoverHaStrategyTest method testCallWithOneError.
public void testCallWithOneError() {
final DefaultRequest request = new DefaultRequest();
request.setMethodName(IWorld.class.getMethods()[0].getName());
request.setArguments(new Object[] {});
request.setInterfaceName(IHello.class.getSimpleName());
request.setParamtersDesc("void");
final Response response = mockery.mock(Response.class);
final URL url = URL.valueOf("motan%3A%2F%2F10.209.128.244%3A8000%2Fcom.weibo.api.motan.protocol.example.IWorld%3Fprotocol%3Dmotan%26export%3Dmotan%3A8000%26application%3Dapi%26module%3Dtest%26check%3Dtrue%26refreshTimestamp%3D1373275099717%26methodconfig.world%28void%29.retries%3D1%26id%3Dmotan%26methodconfig.world%28java.lang.String%29.retries%3D1%26methodconfig.world%28java.lang.String%2Cboolean%29.retries%3D1%26nodeType%3Dservice%26group%3Dwangzhe-test-yf%26shareChannel%3Dtrue%26&");
mockery.checking(new Expectations() {
{
for (Referer<IWorld> ref : referers) {
atLeast(0).of(ref).call(request);
will(returnValue(response));
atLeast(0).of(ref).isAvailable();
will(returnValue(true));
atLeast(0).of(ref).getUrl();
will(returnValue(url));
atLeast(1).of(ref).destroy();
}
one(referers.get(0)).call(request);
will(throwException(new MotanServiceException("mock throw exception when call")));
one(referers.get(1)).call(request);
will(returnValue(response));
}
});
failoverHaStrategy.call(request, loadBalance);
}
use of com.weibo.api.motan.rpc.Referer in project motan by weibocom.
the class ActiveWeightLoadBalanceTest method check.
private static void check(List<Referer> referersHolder) {
int prefix = 0;
for (Referer aReferersHolder : referersHolder) {
Assert.assertTrue(aReferersHolder.isAvailable());
Assert.assertTrue(aReferersHolder.activeRefererCount() > prefix);
prefix = aReferersHolder.activeRefererCount();
}
}
Aggregations