use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class BroadcastCluster2Invoker method getCallables.
private List<Callable<BroadcastResult>> getCallables(List<Invoker<T>> invokers, Invocation invocation) {
List<Callable<BroadcastResult>> tasks = invokers.stream().map(it -> (Callable<BroadcastResult>) () -> {
BroadcastResult br = new BroadcastResult(it.getUrl().getIp(), it.getUrl().getPort());
Result result = null;
try {
result = it.invoke(invocation);
if (null != result && result.hasException()) {
Throwable resultException = result.getException();
if (null != resultException) {
RpcException exception = getRpcException(result.getException());
br.setExceptionMsg(exception.getMessage());
br.setException(exception);
logger.warn(exception.getMessage(), exception);
}
} else if (null != result) {
br.setData(result.getValue());
br.setResult(result);
}
} catch (Throwable ex) {
RpcException exception = getRpcException(result.getException());
br.setExceptionMsg(exception.getMessage());
br.setException(exception);
logger.warn(exception.getMessage(), exception);
}
return br;
}).collect(Collectors.toList());
return tasks;
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class TraceFilterTest method testInvoke.
@Test
public void testInvoke() throws Exception {
String method = "sayHello";
Class<?> type = DemoService.class;
String key = type.getName() + "." + method;
// add tracer
TraceFilter.addTracer(type, method, mockChannel, 2);
Invoker<DemoService> mockInvoker = mock(Invoker.class);
Invocation mockInvocation = mock(Invocation.class);
Result mockResult = mock(Result.class);
TraceFilter filter = new TraceFilter();
given(mockInvoker.getInterface()).willReturn(DemoService.class);
given(mockInvocation.getMethodName()).willReturn(method);
given(mockInvocation.getArguments()).willReturn(new Object[0]);
given(mockInvoker.invoke(mockInvocation)).willReturn(mockResult);
given(mockResult.getValue()).willReturn("result");
// test invoke
filter.invoke(mockInvoker, mockInvocation);
String message = listToString(mockChannel.getReceivedObjects());
String expectMessage = "org.apache.dubbo.rpc.protocol.dubbo.support.DemoService.sayHello([]) -> \"result\"";
System.out.println("actual message: " + message);
Assertions.assertTrue(message.contains(expectMessage));
Assertions.assertTrue(message.contains("elapsed:"));
AtomicInteger traceCount = (AtomicInteger) mockChannel.getAttribute(TRACE_COUNT);
Assertions.assertEquals(1, traceCount.get());
// test remove channel when count >= max - 1
filter.invoke(mockInvoker, mockInvocation);
Field tracers = TraceFilter.class.getDeclaredField(TRACERS_FIELD_NAME);
tracers.setAccessible(true);
ConcurrentHashMap<String, Set<Channel>> o = (ConcurrentHashMap<String, Set<Channel>>) tracers.get(new ConcurrentHashMap<String, Set<Channel>>());
Assertions.assertTrue(o.containsKey(key));
Set<Channel> channels = o.get(key);
Assertions.assertNotNull(channels);
Assertions.assertFalse(channels.contains(mockChannel));
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class MonitorFilterTest method testFilter.
@Test
public void testFilter() throws Exception {
MonitorFilter monitorFilter = new MonitorFilter();
monitorFilter.setMonitorFactory(monitorFactory);
Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), "", new Class<?>[0], new Object[0]);
RpcContext.getContext().setRemoteAddress(NetUtils.getLocalHost(), 20880).setLocalAddress(NetUtils.getLocalHost(), 2345);
Result result = monitorFilter.invoke(serviceInvoker, invocation);
result.whenCompleteWithContext((r, t) -> {
if (t == null) {
monitorFilter.onResponse(r, serviceInvoker, invocation);
} else {
monitorFilter.onError(t, serviceInvoker, invocation);
}
});
while (lastStatistics == null) {
Thread.sleep(10);
}
Assertions.assertEquals("abc", lastStatistics.getParameter(MonitorService.APPLICATION));
Assertions.assertEquals(MonitorService.class.getName(), lastStatistics.getParameter(MonitorService.INTERFACE));
Assertions.assertEquals("aaa", lastStatistics.getParameter(MonitorService.METHOD));
Assertions.assertEquals(NetUtils.getLocalHost() + ":20880", lastStatistics.getParameter(MonitorService.PROVIDER));
Assertions.assertEquals(NetUtils.getLocalHost(), lastStatistics.getAddress());
Assertions.assertNull(lastStatistics.getParameter(MonitorService.CONSUMER));
Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.SUCCESS, 0));
Assertions.assertEquals(0, lastStatistics.getParameter(MonitorService.FAILURE, 0));
Assertions.assertEquals(1, lastStatistics.getParameter(MonitorService.CONCURRENT, 0));
Assertions.assertEquals(invocation, lastInvocation);
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class RpcUtilsTest method testAttachInvocationIdIfAsync_nullAttachments.
/**
* scenario: async invocation, add attachment by default
* verify: no error report when the original attachment is null
*/
@Test
public void testAttachInvocationIdIfAsync_nullAttachments() {
URL url = URL.valueOf("dubbo://localhost/?test.async=true");
Invocation inv = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
RpcUtils.attachInvocationIdIfAsync(url, inv);
assertTrue(RpcUtils.getInvocationId(inv) >= 0L);
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class AccessLogFilterTest method testCustom.
@Test
public void testCustom() {
URL url = URL.valueOf("test://test:11/test?accesslog=custom-access.log");
Invoker<AccessLogFilterTest> invoker = new MyInvoker<AccessLogFilterTest>(url);
Invocation invocation = new MockInvocation();
accessLogFilter.invoke(invoker, invocation);
}
Aggregations