Search in sources :

Example 21 with AppResponse

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

the class TokenFilterTest method testInvokeWithoutToken.

@Test
public void testInvokeWithoutToken() throws Exception {
    Assertions.assertThrows(RpcException.class, () -> {
        String token = "token";
        Invoker invoker = Mockito.mock(Invoker.class);
        URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token);
        when(invoker.getUrl()).thenReturn(url);
        when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result"));
        Invocation invocation = Mockito.mock(Invocation.class);
        tokenFilter.invoke(invoker, invocation);
    });
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 22 with AppResponse

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

the class TokenFilterTest method testInvokeWithWrongToken.

@Test
public void testInvokeWithWrongToken() throws Exception {
    Assertions.assertThrows(RpcException.class, () -> {
        String token = "token";
        Invoker invoker = Mockito.mock(Invoker.class);
        URL url = URL.valueOf("test://test:11/test?accesslog=true&group=dubbo&version=1.1&token=" + token);
        when(invoker.getUrl()).thenReturn(url);
        when(invoker.invoke(any(Invocation.class))).thenReturn(new AppResponse("result"));
        Map<String, Object> attachments = new HashMap<>();
        attachments.put(TOKEN_KEY, "wrongToken");
        Invocation invocation = Mockito.mock(Invocation.class);
        when(invocation.getObjectAttachments()).thenReturn(attachments);
        tokenFilter.invoke(invoker, invocation);
    });
}
Also used : Invoker(org.apache.dubbo.rpc.Invoker) Invocation(org.apache.dubbo.rpc.Invocation) HashMap(java.util.HashMap) AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 23 with AppResponse

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

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

the class FutureFilterTest method testSyncCallbackHasException.

@Test
public void testSyncCallbackHasException() throws RpcException, Throwable {
    Assertions.assertThrows(RuntimeException.class, () -> {
        @SuppressWarnings("unchecked") Invoker<DemoService> invoker = mock(Invoker.class);
        given(invoker.isAvailable()).willReturn(true);
        given(invoker.getInterface()).willReturn(DemoService.class);
        AppResponse result = new AppResponse();
        result.setException(new RuntimeException());
        given(invoker.invoke(invocation)).willReturn(result);
        URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1&onthrow.method=echo");
        given(invoker.getUrl()).willReturn(url);
        eventFilter.invoke(invoker, invocation).recreate();
    });
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Example 25 with AppResponse

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

the class BroadcastCluster2Invoker method createResult.

private Result createResult(Invocation invocation, RpcException exception, List<BroadcastResult> resultList) {
    AppResponse result = new AppResponse(invocation) {

        @Override
        public Result whenCompleteWithContext(BiConsumer<Result, Throwable> fn) {
            RpcContext.getServerContext().setAttachment(BROADCAST_RESULTS_KEY, new Gson().toJson(resultList));
            return new AppResponse();
        }
    };
    result.setException(exception);
    return result;
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) Gson(com.google.gson.Gson) BiConsumer(java.util.function.BiConsumer)

Aggregations

AppResponse (org.apache.dubbo.rpc.AppResponse)54 Test (org.junit.jupiter.api.Test)37 URL (org.apache.dubbo.common.URL)33 Invocation (org.apache.dubbo.rpc.Invocation)27 Result (org.apache.dubbo.rpc.Result)27 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)20 Invoker (org.apache.dubbo.rpc.Invoker)20 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)20 HashMap (java.util.HashMap)13 RpcException (org.apache.dubbo.rpc.RpcException)12 Method (java.lang.reflect.Method)8 Person (org.apache.dubbo.rpc.support.Person)7 TMessage (org.apache.thrift.protocol.TMessage)6 IMetricManager (com.alibaba.metrics.IMetricManager)5 DemoService (org.apache.dubbo.monitor.dubbo.service.DemoService)5 Request (org.apache.dubbo.remoting.exchange.Request)5 Response (org.apache.dubbo.remoting.exchange.Response)5 DemoService (org.apache.dubbo.rpc.support.DemoService)5 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)5 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)5