Search in sources :

Example 51 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class ExceptionFilterTest method testConvertToRunTimeException.

@SuppressWarnings("unchecked")
@Test
public void testConvertToRunTimeException() throws Exception {
    ExceptionFilter exceptionFilter = new ExceptionFilter();
    RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { "world" });
    AppResponse mockRpcResult = new AppResponse();
    mockRpcResult.setException(new HessianException("hessian"));
    Result mockAsyncResult = AsyncRpcResult.newDefaultAsyncResult(mockRpcResult, invocation);
    Invoker<DemoService> invoker = mock(Invoker.class);
    when(invoker.invoke(invocation)).thenReturn(mockAsyncResult);
    when(invoker.getInterface()).thenReturn(DemoService.class);
    Result asyncResult = exceptionFilter.invoke(invoker, invocation);
    AppResponse appResponse = (AppResponse) asyncResult.get();
    exceptionFilter.onResponse(appResponse, invoker, invocation);
    Assertions.assertFalse(appResponse.getException() instanceof HessianException);
    Assertions.assertEquals(appResponse.getException().getClass(), RuntimeException.class);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AppResponse(org.apache.dubbo.rpc.AppResponse) DemoService(org.apache.dubbo.rpc.support.DemoService) HessianException(com.alibaba.com.caucho.hessian.HessianException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 52 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class ExceptionFilterTest method testRpcException.

@SuppressWarnings("unchecked")
@Test
public void testRpcException() {
    Logger logger = mock(Logger.class);
    RpcContext.getContext().setRemoteAddress("127.0.0.1", 1234);
    RpcException exception = new RpcException("TestRpcException");
    ExceptionFilter exceptionFilter = new ExceptionFilter();
    RpcInvocation invocation = new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[] { String.class }, new Object[] { "world" });
    Invoker<DemoService> invoker = mock(Invoker.class);
    given(invoker.getInterface()).willReturn(DemoService.class);
    given(invoker.invoke(eq(invocation))).willThrow(exception);
    try {
        exceptionFilter.invoke(invoker, invocation);
    } catch (RpcException e) {
        assertEquals("TestRpcException", e.getMessage());
        exceptionFilter.setLogger(logger);
        exceptionFilter.onError(e, invoker, invocation);
    }
    Mockito.verify(logger).error(eq("Got unchecked and undeclared exception which called by 127.0.0.1. service: " + DemoService.class.getName() + ", method: sayHello, exception: " + RpcException.class.getName() + ": TestRpcException"), eq(exception));
    RpcContext.removeContext();
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcException(org.apache.dubbo.rpc.RpcException) DemoService(org.apache.dubbo.rpc.support.DemoService) Logger(org.apache.dubbo.common.logger.Logger) Test(org.junit.jupiter.api.Test)

Example 53 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class InvokerInvocationHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (method.getDeclaringClass() == Object.class) {
        return method.invoke(invoker, args);
    }
    String methodName = method.getName();
    Class<?>[] parameterTypes = method.getParameterTypes();
    if (parameterTypes.length == 0) {
        if ("toString".equals(methodName)) {
            return invoker.toString();
        } else if ("$destroy".equals(methodName)) {
            invoker.destroy();
            return null;
        } else if ("hashCode".equals(methodName)) {
            return invoker.hashCode();
        }
    } else if (parameterTypes.length == 1 && "equals".equals(methodName)) {
        return invoker.equals(args[0]);
    }
    RpcInvocation rpcInvocation = new RpcInvocation(method, invoker.getInterface().getName(), protocolServiceKey, args);
    String serviceKey = invoker.getUrl().getServiceKey();
    rpcInvocation.setTargetServiceUniqueName(serviceKey);
    // invoker.getUrl() returns consumer url.
    RpcContext.setRpcContext(invoker.getUrl());
    if (consumerModel != null) {
        rpcInvocation.put(Constants.CONSUMER_MODEL, consumerModel);
        rpcInvocation.put(Constants.METHOD_MODEL, consumerModel.getMethodModel(method));
    }
    return invoker.invoke(rpcInvocation).recreate();
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation)

Example 54 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class AbstractProxyTest method testGetInvoker.

@Test
public void testGetInvoker() throws Exception {
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    DemoService origin = new org.apache.dubbo.rpc.support.DemoServiceImpl();
    Invoker<DemoService> invoker = factory.getInvoker(new DemoServiceImpl(), DemoService.class, url);
    Assertions.assertEquals(invoker.getInterface(), DemoService.class);
    Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", DemoService.class.getName(), DemoService.class.getName() + ":dubbo", new Class[] { String.class }, new Object[] { "aa" })).getValue(), origin.echo("aa"));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) DemoService(org.apache.dubbo.rpc.support.DemoService) URL(org.apache.dubbo.common.URL) DemoServiceImpl(org.apache.dubbo.rpc.support.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 55 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation in project dubbo by alibaba.

the class AbstractProxyTest method testGetProxy.

@Test
public void testGetProxy() throws Exception {
    URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
    Invoker<DemoService> invoker = new MyInvoker<>(url);
    DemoService proxy = factory.getProxy(invoker);
    Assertions.assertNotNull(proxy);
    Assertions.assertTrue(Arrays.asList(proxy.getClass().getInterfaces()).contains(DemoService.class));
    // Not equal
    // Assertions.assertEquals(proxy.toString(), invoker.toString());
    // Assertions.assertEquals(proxy.hashCode(), invoker.hashCode());
    Assertions.assertEquals(invoker.invoke(new RpcInvocation("echo", DemoService.class.getName(), DemoService.class.getName() + ":dubbo", new Class[] { String.class }, new Object[] { "aa" })).getValue(), proxy.echo("aa"));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) DemoService(org.apache.dubbo.rpc.support.DemoService) MyInvoker(org.apache.dubbo.rpc.support.MyInvoker) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)172 Test (org.junit.jupiter.api.Test)122 URL (org.apache.dubbo.common.URL)101 Invoker (org.apache.dubbo.rpc.Invoker)65 ArrayList (java.util.ArrayList)51 Result (org.apache.dubbo.rpc.Result)38 Invocation (org.apache.dubbo.rpc.Invocation)36 RpcException (org.apache.dubbo.rpc.RpcException)26 RegistryDirectory (org.apache.dubbo.registry.integration.RegistryDirectory)23 AppResponse (org.apache.dubbo.rpc.AppResponse)20 MockClusterInvoker (org.apache.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker)20 Router (org.apache.dubbo.rpc.cluster.Router)19 MockInvoker (org.apache.dubbo.rpc.cluster.router.MockInvoker)18 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)16 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Method (java.lang.reflect.Method)9 List (java.util.List)8 Person (org.apache.dubbo.rpc.support.Person)7 Protocol (org.apache.dubbo.rpc.Protocol)6