Search in sources :

Example 11 with RpcInvocation

use of org.apache.dubbo.rpc.RpcInvocation 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 12 with RpcInvocation

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

the class AccessKeyAuthenticatorTest method testAuthenticateRequestNoSignature.

@Test
void testAuthenticateRequestNoSignature() {
    URL url = URL.valueOf("dubbo://10.10.10.10:2181").addParameter(Constants.ACCESS_KEY_ID_KEY, "ak").addParameter(CommonConstants.APPLICATION_KEY, "test").addParameter(Constants.SECRET_ACCESS_KEY_KEY, "sk");
    Invocation invocation = new RpcInvocation();
    AccessKeyAuthenticator helper = new AccessKeyAuthenticator();
    assertThrows(RpcAuthenticationException.class, () -> helper.authenticate(invocation, url));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 13 with RpcInvocation

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

the class RpcUtilsTest method testIsAsync.

@Test
public void testIsAsync() {
    Object[] args = new Object[] { "hello", "dubbo", 520 };
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    URL url = URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService");
    RpcInvocation inv = new RpcInvocation("test", serviceName, "", new Class<?>[] { String.class, String[].class, Object[].class }, new Object[] { "method", new String[] {}, args }, null, invoker, null);
    Assertions.assertFalse(RpcUtils.isAsync(url, inv));
    inv.setInvokeMode(InvokeMode.ASYNC);
    Assertions.assertTrue(RpcUtils.isAsync(url, inv));
    URL url1 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv1 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv1.setAttachment("test." + ASYNC_KEY, Boolean.FALSE.toString());
    Assertions.assertFalse(RpcUtils.isAsync(url1, inv1));
    URL url2 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv2 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv2.setAttachment(ASYNC_KEY, Boolean.FALSE.toString());
    Assertions.assertFalse(RpcUtils.isAsync(url2, inv2));
    URL url3 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv3 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv3.setAttachment(ASYNC_KEY, Boolean.TRUE.toString());
    Assertions.assertTrue(RpcUtils.isAsync(url3, inv3));
    URL url4 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv4 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertTrue(RpcUtils.isAsync(url4, inv4));
    URL url5 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv5 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertFalse(RpcUtils.isAsync(url5, inv5));
    URL url6 = URL.valueOf("dubbo://localhost/?test");
    Invocation inv6 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv6.setAttachment("test." + ASYNC_KEY, Boolean.TRUE.toString());
    Assertions.assertTrue(RpcUtils.isAsync(url6, inv6));
    URL url7 = URL.valueOf("dubbo://localhost/?test.async=true&async=false");
    Invocation inv7 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertTrue(RpcUtils.isAsync(url7, inv7));
    URL url8 = URL.valueOf("dubbo://localhost/?test.async=false&async=true");
    Invocation inv8 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    Assertions.assertFalse(RpcUtils.isAsync(url8, inv8));
    URL url9 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv9 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    inv9.setAttachment("testB." + ASYNC_KEY, Boolean.FALSE.toString());
    Assertions.assertTrue(RpcUtils.isAsync(url9, inv9));
    URL url10 = URL.valueOf("dubbo://localhost/?test.async=true");
    Invocation inv10 = new RpcInvocation("test", "DemoService", "", new Class[] {}, new String[] {});
    ((RpcInvocation) inv10).setInvokeMode(InvokeMode.ASYNC);
    Assertions.assertTrue(RpcUtils.isAsync(url10, inv9));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with RpcInvocation

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

the class ChannelWrappedInvoker method doInvoke.

@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
    RpcInvocation inv = (RpcInvocation) invocation;
    // use interface's name as service path to export if it's not found on client side
    inv.setAttachment(PATH_KEY, getInterface().getName());
    inv.setAttachment(CALLBACK_SERVICE_KEY, serviceKey);
    try {
        if (RpcUtils.isOneway(getUrl(), inv)) {
            // may have concurrency issue
            currentClient.send(inv, getUrl().getMethodParameter(invocation.getMethodName(), SENT_KEY, false));
            return AsyncRpcResult.newDefaultAsyncResult(invocation);
        } else {
            final String methodName = RpcUtils.getMethodName(invocation);
            final int timeout = (int) RpcUtils.getTimeout(getUrl(), methodName, RpcContext.getContext(), DEFAULT_TIMEOUT);
            CompletableFuture<AppResponse> appResponseFuture = currentClient.request(inv, timeout, null).thenApply(obj -> (AppResponse) obj);
            return new AsyncRpcResult(appResponseFuture, inv);
        }
    } catch (RpcException e) {
        throw e;
    } catch (TimeoutException e) {
        throw new RpcException(RpcException.TIMEOUT_EXCEPTION, e.getMessage(), e);
    } catch (RemotingException e) {
        throw new RpcException(RpcException.NETWORK_EXCEPTION, e.getMessage(), e);
    } catch (Throwable e) {
        // here is non-biz exception, wrap it.
        throw new RpcException(e.getMessage(), e);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) RemotingException(org.apache.dubbo.remoting.RemotingException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) TimeoutException(org.apache.dubbo.remoting.TimeoutException)

Example 15 with RpcInvocation

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

the class RpcUtilsTest method testGetReturnType.

@Test
public void testGetReturnType() {
    Class<?> demoServiceClass = DemoService.class;
    String serviceName = demoServiceClass.getName();
    Invoker invoker = mock(Invoker.class);
    given(invoker.getUrl()).willReturn(URL.valueOf("test://127.0.0.1:1/org.apache.dubbo.rpc.support.DemoService?interface=org.apache.dubbo.rpc.support.DemoService"));
    // void sayHello(String name);
    RpcInvocation inv = new RpcInvocation("sayHello", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?> returnType = RpcUtils.getReturnType(inv);
    Assertions.assertNull(returnType);
    // String echo(String text);
    RpcInvocation inv1 = new RpcInvocation("echo", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?> returnType1 = RpcUtils.getReturnType(inv1);
    Assertions.assertNotNull(returnType1);
    Assertions.assertEquals(String.class, returnType1);
    // int getSize(String[] strs);
    RpcInvocation inv2 = new RpcInvocation("getSize", serviceName, "", new Class<?>[] { String[].class }, null, null, invoker, null);
    Class<?> returnType2 = RpcUtils.getReturnType(inv2);
    Assertions.assertNotNull(returnType2);
    Assertions.assertEquals(int.class, returnType2);
    // Person getPerson(Person person);
    RpcInvocation inv3 = new RpcInvocation("getPerson", serviceName, "", new Class<?>[] { Person.class }, null, null, invoker, null);
    Class<?> returnType3 = RpcUtils.getReturnType(inv3);
    Assertions.assertNotNull(returnType3);
    Assertions.assertEquals(Person.class, returnType3);
    // List<String> testReturnType1(String str);
    RpcInvocation inv4 = new RpcInvocation("testReturnType1", serviceName, "", new Class<?>[] { String.class }, null, null, invoker, null);
    Class<?> returnType4 = RpcUtils.getReturnType(inv4);
    Assertions.assertNotNull(returnType4);
    Assertions.assertEquals(List.class, returnType4);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invoker(org.apache.dubbo.rpc.Invoker) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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