Search in sources :

Example 61 with Result

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

the class MergeableClusterInvokerTest method testAddMenu.

@Test
public void testAddMenu() throws Exception {
    String menu = "first";
    List<String> menuItems = new ArrayList<String>() {

        {
            add("1");
            add("2");
        }
    };
    given(invocation.getMethodName()).willReturn("addMenu");
    given(invocation.getParameterTypes()).willReturn(new Class<?>[] { String.class, List.class });
    given(invocation.getArguments()).willReturn(new Object[] { menu, menuItems });
    given(invocation.getObjectAttachments()).willReturn(new HashMap<>());
    given(invocation.getInvoker()).willReturn(firstInvoker);
    given(firstInvoker.getUrl()).willReturn(url.addParameter(GROUP_KEY, "first"));
    given(firstInvoker.getInterface()).willReturn(MenuService.class);
    given(firstInvoker.invoke(invocation)).willReturn(new AppResponse());
    given(firstInvoker.isAvailable()).willReturn(true);
    given(secondInvoker.getUrl()).willReturn(url.addParameter(GROUP_KEY, "second"));
    given(secondInvoker.getInterface()).willReturn(MenuService.class);
    given(secondInvoker.invoke(invocation)).willReturn(new AppResponse());
    given(secondInvoker.isAvailable()).willReturn(true);
    given(directory.list(invocation)).willReturn(new ArrayList() {

        {
            add(firstInvoker);
            add(secondInvoker);
        }
    });
    given(directory.getUrl()).willReturn(url);
    given(directory.getConsumerUrl()).willReturn(url);
    given(directory.getConsumerUrl()).willReturn(url);
    given(directory.getInterface()).willReturn(MenuService.class);
    mergeableClusterInvoker = new MergeableClusterInvoker<MenuService>(directory);
    Result result = mergeableClusterInvoker.invoke(invocation);
    Assertions.assertNull(result.getValue());
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) ArrayList(java.util.ArrayList) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 62 with Result

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

the class MergeableClusterInvokerTest method testGetMenuSuccessfully.

@Test
public void testGetMenuSuccessfully() throws Exception {
    // setup
    url = url.addParameter(MERGER_KEY, ".merge");
    given(invocation.getMethodName()).willReturn("getMenu");
    given(invocation.getParameterTypes()).willReturn(new Class<?>[] {});
    given(invocation.getArguments()).willReturn(new Object[] {});
    given(invocation.getObjectAttachments()).willReturn(new HashMap<>());
    given(invocation.getInvoker()).willReturn(firstInvoker);
    firstInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Invoker.class }, (proxy, method, args) -> {
        if ("getUrl".equals(method.getName())) {
            return url.addParameter(GROUP_KEY, "first");
        }
        if ("getInterface".equals(method.getName())) {
            return MenuService.class;
        }
        if ("invoke".equals(method.getName())) {
            return AsyncRpcResult.newDefaultAsyncResult(firstMenu, invocation);
        }
        return null;
    });
    secondInvoker = (Invoker) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { Invoker.class }, (proxy, method, args) -> {
        if ("getUrl".equals(method.getName())) {
            return url.addParameter(GROUP_KEY, "second");
        }
        if ("getInterface".equals(method.getName())) {
            return MenuService.class;
        }
        if ("invoke".equals(method.getName())) {
            return AsyncRpcResult.newDefaultAsyncResult(secondMenu, invocation);
        }
        return null;
    });
    given(directory.list(invocation)).willReturn(new ArrayList() {

        {
            add(firstInvoker);
            add(secondInvoker);
        }
    });
    given(directory.getUrl()).willReturn(url);
    given(directory.getConsumerUrl()).willReturn(url);
    given(directory.getConsumerUrl()).willReturn(url);
    given(directory.getInterface()).willReturn(MenuService.class);
    mergeableClusterInvoker = new MergeableClusterInvoker<MenuService>(directory);
    // invoke
    Result result = mergeableClusterInvoker.invoke(invocation);
    Assertions.assertTrue(result.getValue() instanceof Menu);
    Menu menu = (Menu) result.getValue();
    Map<String, List<String>> expected = new HashMap<String, List<String>>();
    merge(expected, firstMenuMap);
    merge(expected, secondMenuMap);
    assertEquals(expected.keySet(), menu.getMenus().keySet());
    for (Map.Entry<String, List<String>> entry : expected.entrySet()) {
        // FIXME: cannot guarantee the sequence of the merge result, check implementation in
        // MergeableClusterInvoker#invoke
        List<String> values1 = new ArrayList<String>(entry.getValue());
        List<String> values2 = new ArrayList<String>(menu.getMenus().get(entry.getKey()));
        Collections.sort(values1);
        Collections.sort(values2);
        assertEquals(values1, values2);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 63 with Result

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

the class AbstractClusterInvokerTest method testTimeoutExceptionCode.

@Test()
public void testTimeoutExceptionCode() {
    List<Invoker<DemoService>> invokers = new ArrayList<Invoker<DemoService>>();
    invokers.add(new Invoker<DemoService>() {

        @Override
        public Class<DemoService> getInterface() {
            return DemoService.class;
        }

        public URL getUrl() {
            return URL.valueOf("dubbo://" + NetUtils.getLocalHost() + ":20880/" + DemoService.class.getName());
        }

        @Override
        public boolean isAvailable() {
            return false;
        }

        @Override
        public Result invoke(Invocation invocation) throws RpcException {
            throw new RpcException(RpcException.TIMEOUT_EXCEPTION, "test timeout");
        }

        @Override
        public void destroy() {
        }
    });
    Directory<DemoService> directory = new StaticDirectory<DemoService>(invokers);
    FailoverClusterInvoker<DemoService> failoverClusterInvoker = new FailoverClusterInvoker<DemoService>(directory);
    try {
        failoverClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    ForkingClusterInvoker<DemoService> forkingClusterInvoker = new ForkingClusterInvoker<DemoService>(directory);
    try {
        forkingClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
    FailfastClusterInvoker<DemoService> failfastClusterInvoker = new FailfastClusterInvoker<DemoService>(directory);
    try {
        failfastClusterInvoker.invoke(new RpcInvocation("sayHello", DemoService.class.getName(), "", new Class<?>[0], new Object[0]));
        Assertions.fail();
    } catch (RpcException e) {
        Assertions.assertEquals(RpcException.TIMEOUT_EXCEPTION, e.getCode());
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) ArrayList(java.util.ArrayList) DemoService(org.apache.dubbo.rpc.cluster.filter.DemoService) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) StaticDirectory(org.apache.dubbo.rpc.cluster.directory.StaticDirectory) Invoker(org.apache.dubbo.rpc.Invoker) RpcException(org.apache.dubbo.rpc.RpcException) Test(org.junit.jupiter.api.Test)

Example 64 with Result

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

the class AbstractClusterInvokerTest method setUp.

@SuppressWarnings({ "unchecked" })
@BeforeEach
public void setUp() throws Exception {
    invocation.setMethodName("sayHello");
    invoker1 = mock(Invoker.class);
    invoker2 = mock(Invoker.class);
    invoker3 = mock(Invoker.class);
    invoker4 = mock(Invoker.class);
    invoker5 = mock(Invoker.class);
    mockedInvoker1 = mock(Invoker.class);
    URL turl = URL.valueOf("test://test:11/test");
    given(invoker1.isAvailable()).willReturn(false);
    given(invoker1.getInterface()).willReturn(IHelloService.class);
    given(invoker1.getUrl()).willReturn(turl.setPort(1).addParameter("name", "invoker1"));
    given(invoker2.isAvailable()).willReturn(true);
    given(invoker2.getInterface()).willReturn(IHelloService.class);
    given(invoker2.getUrl()).willReturn(turl.setPort(2).addParameter("name", "invoker2"));
    given(invoker3.isAvailable()).willReturn(false);
    given(invoker3.getInterface()).willReturn(IHelloService.class);
    given(invoker3.getUrl()).willReturn(turl.setPort(3).addParameter("name", "invoker3"));
    given(invoker4.isAvailable()).willReturn(true);
    given(invoker4.getInterface()).willReturn(IHelloService.class);
    given(invoker4.getUrl()).willReturn(turl.setPort(4).addParameter("name", "invoker4"));
    given(invoker5.isAvailable()).willReturn(false);
    given(invoker5.getInterface()).willReturn(IHelloService.class);
    given(invoker5.getUrl()).willReturn(turl.setPort(5).addParameter("name", "invoker5"));
    given(mockedInvoker1.isAvailable()).willReturn(false);
    given(mockedInvoker1.getInterface()).willReturn(IHelloService.class);
    given(mockedInvoker1.getUrl()).willReturn(turl.setPort(999).setProtocol("mock"));
    invokers.add(invoker1);
    dic = new StaticDirectory<>(url, invokers, null);
    dic.setConsumerUrl(consumerUrl);
    cluster = new AbstractClusterInvoker(dic) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
    cluster_nocheck = new AbstractClusterInvoker(dic, url.addParameterIfAbsent(CLUSTER_AVAILABLE_CHECK_KEY, Boolean.FALSE.toString())) {

        @Override
        protected Result doInvoke(Invocation invocation, List invokers, LoadBalance loadbalance) throws RpcException {
            return null;
        }
    };
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) LoadBalance(org.apache.dubbo.rpc.cluster.LoadBalance) RoundRobinLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance) LeastActiveLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance) RandomLoadBalance(org.apache.dubbo.rpc.cluster.loadbalance.RandomLoadBalance) RpcException(org.apache.dubbo.rpc.RpcException) ArrayList(java.util.ArrayList) List(java.util.List) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 65 with Result

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

the class MockClusterInvokerTest method testMockInvokerInvoke_failmock.

/**
 * Test if mock policy works fine: fail-mock
 */
@Test
public void testMockInvokerInvoke_failmock() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName()).addParameter(MOCK_KEY, "fail:return null").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName())).addParameter("invoke_return_error", "true");
    URL mockUrl = URL.valueOf("mock://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa").addParameters(url.getParameters());
    Protocol protocol = new MockProtocol();
    Invoker<IHelloService> mInvoker1 = protocol.refer(IHelloService.class, mockUrl);
    Invoker<IHelloService> cluster = getClusterInvokerMock(url, mInvoker1);
    // Configured with mock
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getSomething");
    Result ret = cluster.invoke(invocation);
    Assertions.assertEquals("aa", ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("getSomething2");
    ret = cluster.invoke(invocation);
    Assertions.assertNull(ret.getValue());
    // If no mock was configured, return null directly
    invocation = new RpcInvocation();
    invocation.setMethodName("sayHello");
    ret = cluster.invoke(invocation);
    Assertions.assertNull(ret.getValue());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Protocol(org.apache.dubbo.rpc.Protocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) MockProtocol(org.apache.dubbo.rpc.support.MockProtocol) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Aggregations

Result (org.apache.dubbo.rpc.Result)97 Test (org.junit.jupiter.api.Test)74 URL (org.apache.dubbo.common.URL)55 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)39 Invocation (org.apache.dubbo.rpc.Invocation)33 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)31 AppResponse (org.apache.dubbo.rpc.AppResponse)28 Invoker (org.apache.dubbo.rpc.Invoker)27 RpcException (org.apache.dubbo.rpc.RpcException)22 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)7 RpcContext (org.apache.dubbo.rpc.RpcContext)7 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)6 DemoService (org.apache.dubbo.rpc.support.DemoService)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Protocol (org.apache.dubbo.rpc.Protocol)5 Person (org.apache.dubbo.rpc.support.Person)5 Method (java.lang.reflect.Method)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4