Search in sources :

Example 6 with Invocation

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);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MonitorService(org.apache.dubbo.monitor.MonitorService) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 7 with Invocation

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);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Monitor(org.apache.dubbo.monitor.Monitor) MonitorFactory(org.apache.dubbo.monitor.MonitorFactory) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MonitorService(org.apache.dubbo.monitor.MonitorService) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 8 with 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);
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 9 with Invocation

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));
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 10 with Invocation

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());
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

Invocation (org.apache.dubbo.rpc.Invocation)98 Test (org.junit.jupiter.api.Test)78 URL (org.apache.dubbo.common.URL)77 Invoker (org.apache.dubbo.rpc.Invoker)44 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)44 Result (org.apache.dubbo.rpc.Result)33 AppResponse (org.apache.dubbo.rpc.AppResponse)29 RpcException (org.apache.dubbo.rpc.RpcException)18 MockInvocation (org.apache.dubbo.rpc.support.MockInvocation)17 HashMap (java.util.HashMap)16 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)13 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)11 MyInvoker (org.apache.dubbo.rpc.support.MyInvoker)10 ArrayList (java.util.ArrayList)8 Person (org.apache.dubbo.rpc.support.Person)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 IMetricManager (com.alibaba.metrics.IMetricManager)5 Method (java.lang.reflect.Method)5 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)5 FastCompass (com.alibaba.metrics.FastCompass)4