Search in sources :

Example 56 with Result

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

the class FilterNode method invoke.

@Override
public Result invoke(Invocation invocation) throws RpcException {
    Result asyncResult;
    try {
        asyncResult = filter.invoke(next, invocation);
    } catch (Exception e) {
        if (filter instanceof ListenableFilter) {
            ListenableFilter listenableFilter = ((ListenableFilter) filter);
            try {
                Filter.Listener listener = listenableFilter.listener(invocation);
                if (listener != null) {
                    listener.onError(e, invoker, invocation);
                }
            } finally {
                listenableFilter.removeListener(invocation);
            }
        } else if (filter instanceof Filter.Listener) {
            Filter.Listener listener = (Filter.Listener) filter;
            listener.onError(e, invoker, invocation);
        }
        throw e;
    } finally {
    }
    return asyncResult.whenCompleteWithContext((r, t) -> {
        if (filter instanceof ListenableFilter) {
            ListenableFilter listenableFilter = ((ListenableFilter) filter);
            Filter.Listener listener = listenableFilter.listener(invocation);
            try {
                if (listener != null) {
                    if (t == null) {
                        listener.onResponse(r, invoker, invocation);
                    } else {
                        listener.onError(t, invoker, invocation);
                    }
                }
            } finally {
                listenableFilter.removeListener(invocation);
            }
        } else if (filter instanceof Filter.Listener) {
            Filter.Listener listener = (Filter.Listener) filter;
            if (t == null) {
                listener.onResponse(r, invoker, invocation);
            } else {
                listener.onError(t, invoker, invocation);
            }
        }
    });
}
Also used : ListenableFilter(org.apache.dubbo.rpc.ListenableFilter) Filter(org.apache.dubbo.rpc.Filter) ListenableFilter(org.apache.dubbo.rpc.ListenableFilter) RpcException(org.apache.dubbo.rpc.RpcException) Result(org.apache.dubbo.rpc.Result)

Example 57 with Result

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

the class ConsumerContextClusterInterceptorTest method testOnMessage.

@Test
public void testOnMessage() {
    RpcContext serverContext = RpcContext.getServerContext();
    Result mockResult = Mockito.mock(Result.class);
    Map<String, Object> map = Collections.singletonMap("key", "value");
    Mockito.when(mockResult.getObjectAttachments()).thenReturn(map);
    interceptor.onMessage(mockResult, null, null);
    Map<String, Object> objectAttachments = serverContext.getObjectAttachments();
    Assertions.assertNotNull(objectAttachments);
    Assertions.assertTrue(objectAttachments.size() == 1);
    Assertions.assertEquals("value", objectAttachments.get("key"));
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 58 with Result

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

the class FailSafeClusterInvokerTest method testInvokeNoExceptoin.

@Test()
public void testInvokeNoExceptoin() {
    resetInvokerToNoException();
    FailsafeClusterInvoker<DemoService> invoker = new FailsafeClusterInvoker<DemoService>(dic);
    Result ret = invoker.invoke(invocation);
    Assertions.assertSame(result, ret);
}
Also used : DemoService(org.apache.dubbo.rpc.cluster.filter.DemoService) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 59 with Result

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

the class FailbackClusterInvokerTest method testInvokeNoException.

@Test
@Order(2)
public void testInvokeNoException() {
    resetInvokerToNoException();
    FailbackClusterInvoker<FailbackClusterInvokerTest> invoker = new FailbackClusterInvoker<FailbackClusterInvokerTest>(dic);
    Result ret = invoker.invoke(invocation);
    Assertions.assertSame(result, ret);
}
Also used : Result(org.apache.dubbo.rpc.Result) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 60 with Result

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

the class FailoverClusterInvokerTest method testInvokeWithRPCException.

@Test()
public void testInvokeWithRPCException() {
    given(invoker1.invoke(invocation)).willThrow(new RpcException());
    given(invoker1.isAvailable()).willReturn(true);
    given(invoker1.getUrl()).willReturn(url);
    given(invoker1.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    given(invoker2.invoke(invocation)).willReturn(result);
    given(invoker2.isAvailable()).willReturn(true);
    given(invoker2.getUrl()).willReturn(url);
    given(invoker2.getInterface()).willReturn(FailoverClusterInvokerTest.class);
    FailoverClusterInvoker<FailoverClusterInvokerTest> invoker = new FailoverClusterInvoker<FailoverClusterInvokerTest>(dic);
    for (int i = 0; i < 100; i++) {
        Result ret = invoker.invoke(invocation);
        assertSame(result, ret);
    }
}
Also used : RpcException(org.apache.dubbo.rpc.RpcException) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) 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