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);
}
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();
}
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();
}
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"));
}
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"));
}
Aggregations