Search in sources :

Example 1 with Cluster

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"));
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) ArrayList(java.util.ArrayList) Cluster(com.weibo.api.motan.cluster.Cluster) Method(java.lang.reflect.Method) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) Test(org.junit.Test)

Example 2 with Cluster

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);
}
Also used : ClusterSupport(com.weibo.api.motan.cluster.support.ClusterSupport) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Cluster(com.weibo.api.motan.cluster.Cluster) URL(com.weibo.api.motan.rpc.URL) ConfigHandler(com.weibo.api.motan.config.handler.ConfigHandler)

Example 3 with Cluster

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));
}
Also used : Expectations(org.jmock.Expectations) ArrayList(java.util.ArrayList) Referer(com.weibo.api.motan.rpc.Referer) Cluster(com.weibo.api.motan.cluster.Cluster) Method(java.lang.reflect.Method) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 4 with Cluster

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);
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) ArrayList(java.util.ArrayList) Cluster(com.weibo.api.motan.cluster.Cluster) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture) Method(java.lang.reflect.Method) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Aggregations

Cluster (com.weibo.api.motan.cluster.Cluster)4 URL (com.weibo.api.motan.rpc.URL)4 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 Expectations (org.jmock.Expectations)3 Test (org.junit.Test)3 Request (com.weibo.api.motan.rpc.Request)2 ClusterSupport (com.weibo.api.motan.cluster.support.ClusterSupport)1 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)1 MotanBizException (com.weibo.api.motan.exception.MotanBizException)1 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)1 Referer (com.weibo.api.motan.rpc.Referer)1 ResponseFuture (com.weibo.api.motan.rpc.ResponseFuture)1