use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class MonitorFilterTest method testGenericFilter.
@Test
public void testGenericFilter() throws Exception {
MonitorFilter monitorFilter = new MonitorFilter();
monitorFilter.setMonitorFactory(monitorFactory);
Invocation invocation = new RpcInvocation("$invoke", MonitorService.class.getName(), "", new Class<?>[] { String.class, String[].class, Object[].class }, new Object[] { "xxx", new String[] {}, new Object[] {} });
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("xxx", 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 MonitorFilterTest method testSafeFailForMonitorCollectFail.
@Test
public void testSafeFailForMonitorCollectFail() {
MonitorFilter monitorFilter = new MonitorFilter();
MonitorFactory mockMonitorFactory = mock(MonitorFactory.class);
Monitor mockMonitor = mock(Monitor.class);
Mockito.doThrow(new RuntimeException()).when(mockMonitor).collect(any(URL.class));
monitorFilter.setMonitorFactory(mockMonitorFactory);
given(mockMonitorFactory.getMonitor(any(URL.class))).willReturn(mockMonitor);
Invocation invocation = new RpcInvocation("aaa", MonitorService.class.getName(), "", new Class<?>[0], new Object[0]);
monitorFilter.invoke(serviceInvoker, invocation);
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthSuccessfully.
@Test
void testAuthSuccessfully() {
String service = "org.apache.dubbo.DemoService";
String method = "test";
long currentTimeMillis = System.currentTimeMillis();
URL url = URL.valueOf("dubbo://10.10.10.10:2181").setServiceInterface(service).addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test-provider").addParameter(Constants.SERVICE_AUTH, true);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invocation.getAttachment(Constants.AK_KEY)).thenReturn("ak");
when(invocation.getAttachment(CommonConstants.CONSUMER)).thenReturn("test-consumer");
when(invocation.getAttachment(Constants.REQUEST_TIMESTAMP_KEY)).thenReturn(String.valueOf(currentTimeMillis));
when(invocation.getMethodName()).thenReturn(method);
when(invoker.getUrl()).thenReturn(url);
String requestString = String.format(Constants.SIGNATURE_STRING_FORMAT, url.getColonSeparatedKey(), invocation.getMethodName(), "sk", currentTimeMillis);
String sign = SignatureUtils.sign(requestString, "sk");
when(invocation.getAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(sign);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
Result result = providerAuthFilter.invoke(invoker, invocation);
assertNull(result);
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthDisabled.
@Test
void testAuthDisabled() {
URL url = mock(URL.class);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invoker.getUrl()).thenReturn(url);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
providerAuthFilter.invoke(invoker, invocation);
verify(url, never()).getParameter(eq(Constants.AUTHENTICATOR), eq(Constants.DEFAULT_AUTHENTICATOR));
}
use of org.apache.dubbo.rpc.Invocation in project dubbo by alibaba.
the class ProviderAuthFilterTest method testAuthEnabled.
@Test
void testAuthEnabled() {
URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SERVICE_AUTH, true);
Invoker invoker = mock(Invoker.class);
Invocation invocation = mock(Invocation.class);
when(invoker.getUrl()).thenReturn(url);
ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
providerAuthFilter.invoke(invoker, invocation);
verify(invocation, atLeastOnce()).getAttachment(anyString());
}
Aggregations