use of com.weibo.api.motan.cluster.Cluster in project motan by weibocom.
the class RefererInvocationHandlerTest method testInvokeException.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvokeException() throws Throwable {
final Cluster cluster = mockery.mock(Cluster.class);
final URL u = new URL("motan", "local", 80, "test");
u.addParameter(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
mockery.checking(new Expectations() {
{
one(cluster).call(with(any(Request.class)));
will(throwException(new MotanBizException("just test", new StackOverflowError())));
allowing(cluster).getUrl();
will(returnValue(u));
}
});
List<Cluster> clus = new ArrayList<Cluster>();
clus.add(cluster);
RefererInvocationHandler handler = new RefererInvocationHandler(String.class, clus);
Method[] methods = String.class.getMethods();
try {
handler.invoke(null, methods[1], null);
} catch (Exception e) {
assertTrue(e instanceof MotanServiceException);
assertTrue(e.getMessage().contains("StackOverflowError"));
}
}
use of com.weibo.api.motan.cluster.Cluster in project motan by weibocom.
the class RefererConfig method initRef.
@SuppressWarnings({ "unchecked", "rawtypes" })
public synchronized void initRef() {
if (initialized.get()) {
return;
}
try {
interfaceClass = (Class) Class.forName(interfaceClass.getName(), true, Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
throw new MotanFrameworkException("ReferereConfig initRef Error: Class not found " + interfaceClass.getName(), e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
if (CollectionUtil.isEmpty(protocols)) {
throw new MotanFrameworkException(String.format("%s RefererConfig is malformed, for protocol not set correctly!", interfaceClass.getName()));
}
checkInterfaceAndMethods(interfaceClass, methods);
clusterSupports = new ArrayList<ClusterSupport<T>>(protocols.size());
List<Cluster<T>> clusters = new ArrayList<Cluster<T>>(protocols.size());
String proxy = null;
ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE);
List<URL> registryUrls = loadRegistryUrls();
String localIp = getLocalHostAddress(registryUrls);
for (ProtocolConfig protocol : protocols) {
Map<String, String> params = new HashMap<String, String>();
params.put(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
params.put(URLParamType.version.getName(), URLParamType.version.getValue());
params.put(URLParamType.refreshTimestamp.getName(), String.valueOf(System.currentTimeMillis()));
collectConfigParams(params, protocol, basicReferer, extConfig, this);
collectMethodConfigParams(params, this.getMethods());
URL refUrl = new URL(protocol.getName(), localIp, MotanConstants.DEFAULT_INT_VALUE, interfaceClass.getName(), params);
ClusterSupport<T> clusterSupport = createClusterSupport(refUrl, configHandler, registryUrls);
clusterSupports.add(clusterSupport);
clusters.add(clusterSupport.getCluster());
proxy = (proxy == null) ? refUrl.getParameter(URLParamType.proxy.getName(), URLParamType.proxy.getValue()) : proxy;
}
ref = configHandler.refer(interfaceClass, clusters, proxy);
initialized.set(true);
}
use of com.weibo.api.motan.cluster.Cluster 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.cluster.Cluster in project motan by weibocom.
the class RefererInvocationHandlerTest method testAsync.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testAsync() {
final Cluster cluster = mockery.mock(Cluster.class);
final URL u = new URL("motan", "local", 80, "test");
u.addParameter(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
final ResponseFuture response = mockery.mock(ResponseFuture.class);
mockery.checking(new Expectations() {
{
one(cluster).call(with(any(Request.class)));
will(returnValue(response));
allowing(cluster).getUrl();
will(returnValue(u));
}
});
List<Cluster> clus = new ArrayList<Cluster>();
clus.add(cluster);
RefererInvocationHandler handler = new RefererInvocationHandler(String.class, clus);
Method method;
try {
method = TestService.class.getMethod("helloAsync", new Class<?>[] {});
ResponseFuture res = (ResponseFuture) handler.invoke(null, method, null);
assertEquals(response, res);
assertTrue((Boolean) RpcContext.getContext().getAttribute(MotanConstants.ASYNC_SUFFIX));
} catch (Throwable e) {
assertTrue(false);
}
}
Aggregations