Search in sources :

Example 71 with Result

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

the class MockClusterInvokerTest method testMockInvokerInvoke_forcemock.

/**
 * Test if mock policy works fine: force-mock
 */
@Test
public void testMockInvokerInvoke_forcemock() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloService.class.getName());
    url = url.addParameter(MOCK_KEY, "force:return null").addParameter(REFER_KEY, URL.encode(PATH_KEY + "=" + IHelloService.class.getName()));
    URL mockUrl = URL.valueOf("mock://localhost/" + IHelloService.class.getName() + "?getSomething.mock=return aa&getSomething3xx.mock=return xx").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)

Example 72 with Result

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

the class MockProviderRpcExceptionTest method testMockInvokerProviderRpcException.

/**
 * Test if mock policy works fine: ProviderRpcException
 */
@Test
public void testMockInvokerProviderRpcException() {
    URL url = URL.valueOf("remote://1.2.3.4/" + IHelloRpcService.class.getName());
    url = url.addParameter(MOCK_KEY, "true").addParameter("invoke_return_error", "true").addParameter(REFER_KEY, "path%3dorg.apache.dubbo.rpc.cluster.support.wrapper.MockProviderRpcExceptionTest%24IHelloRpcService");
    Invoker<IHelloRpcService> cluster = getClusterInvoker(url);
    RpcInvocation invocation = new RpcInvocation();
    invocation.setMethodName("getSomething4");
    Result ret = cluster.invoke(invocation);
    Assertions.assertEquals("something4mock", ret.getValue());
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 73 with Result

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

the class ForkingClusterInvokerTest method testInvokeNoException.

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

Example 74 with Result

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

the class MetricsFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (exported.compareAndSet(false, true)) {
        this.protocolName = invoker.getUrl().getParameter(METRICS_PROTOCOL) == null ? DEFAULT_PROTOCOL : invoker.getUrl().getParameter(METRICS_PROTOCOL);
        Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
        this.port = invoker.getUrl().getParameter(METRICS_PORT) == null ? protocol.getDefaultPort() : Integer.parseInt(invoker.getUrl().getParameter(METRICS_PORT));
        Invoker<MetricsService> metricsInvoker = initMetricsInvoker();
        try {
            protocol.export(metricsInvoker);
        } catch (RuntimeException e) {
            logger.error("Metrics Service need to be configured" + " when multiple processes are running on a host" + e.getMessage());
        }
    }
    RpcContext context = RpcContext.getContext();
    boolean isProvider = context.isProviderSide();
    long start = System.currentTimeMillis();
    try {
        // proceed invocation chain
        Result result = invoker.invoke(invocation);
        long duration = System.currentTimeMillis() - start;
        reportMetrics(invoker, invocation, duration, "success", isProvider);
        return result;
    } catch (RpcException e) {
        long duration = System.currentTimeMillis() - start;
        String result = "error";
        if (e.isTimeout()) {
            result = "timeoutError";
        }
        if (e.isBiz()) {
            result = "bisError";
        }
        if (e.isNetwork()) {
            result = "networkError";
        }
        if (e.isSerialization()) {
            result = "serializationError";
        }
        reportMetrics(invoker, invocation, duration, result, isProvider);
        throw e;
    }
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) MetricsService(org.apache.dubbo.monitor.MetricsService) RpcException(org.apache.dubbo.rpc.RpcException) Protocol(org.apache.dubbo.rpc.Protocol) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result)

Example 75 with Result

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

the class ProviderAuthFilterTest method testAuthFailedWhenParameterError.

@Test
void testAuthFailedWhenParameterError() {
    String service = "org.apache.dubbo.DemoService";
    String method = "test";
    Object[] originalParams = new Object[] { "dubbo1", "dubbo2" };
    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.PARAMETER_SIGNATURE_ENABLE_KEY, true).addParameter(Constants.SERVICE_AUTH, true);
    Invoker invoker = mock(Invoker.class);
    Invocation invocation = mock(Invocation.class);
    when(invocation.getObjectAttachment(Constants.AK_KEY)).thenReturn("ak");
    when(invocation.getObjectAttachment(CommonConstants.CONSUMER)).thenReturn("test-consumer");
    when(invocation.getObjectAttachment(Constants.REQUEST_TIMESTAMP_KEY)).thenReturn(currentTimeMillis);
    when(invocation.getMethodName()).thenReturn(method);
    Object[] fakeParams = new Object[] { "dubbo1", "dubbo3" };
    when(invocation.getArguments()).thenReturn(fakeParams);
    when(invoker.getUrl()).thenReturn(url);
    String requestString = String.format(Constants.SIGNATURE_STRING_FORMAT, url.getColonSeparatedKey(), invocation.getMethodName(), "sk", currentTimeMillis);
    String sign = SignatureUtils.sign(originalParams, requestString, "sk");
    when(invocation.getObjectAttachment(Constants.REQUEST_SIGNATURE_KEY)).thenReturn(sign);
    ProviderAuthFilter providerAuthFilter = new ProviderAuthFilter();
    Result result = providerAuthFilter.invoke(invoker, invocation);
    assertTrue(result.hasException());
    assertTrue(result.getException() instanceof RpcAuthenticationException);
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) RpcAuthenticationException(org.apache.dubbo.auth.exception.RpcAuthenticationException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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