Search in sources :

Example 6 with AppResponse

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

the class ValidationFilterTest method testItWithExistClass.

@Test
public void testItWithExistClass() throws Exception {
    URL url = URL.valueOf("test://test:11/test?validation=true");
    given(validation.getValidator(url)).willReturn(validator);
    given(invoker.invoke(invocation)).willReturn(new AppResponse("success"));
    given(invoker.getUrl()).willReturn(url);
    given(invocation.getMethodName()).willReturn("echo1");
    given(invocation.getParameterTypes()).willReturn(new Class<?>[] { String.class });
    given(invocation.getArguments()).willReturn(new Object[] { "arg1" });
    validationFilter.setValidation(validation);
    Result result = validationFilter.invoke(invoker, invocation);
    assertThat(String.valueOf(result.getValue()), is("success"));
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) URL(org.apache.dubbo.common.URL) Result(org.apache.dubbo.rpc.Result) Test(org.junit.jupiter.api.Test)

Example 7 with AppResponse

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

the class FutureAdapter method setCallback.

void setCallback(ResponseCallback callback) {
    BiConsumer<Object, ? super Throwable> biConsumer = new BiConsumer<Object, Throwable>() {

        @Override
        public void accept(Object obj, Throwable t) {
            if (t != null) {
                if (t instanceof CompletionException) {
                    t = t.getCause();
                }
                callback.caught(t);
            } else {
                AppResponse appResponse = (AppResponse) obj;
                if (appResponse.hasException()) {
                    callback.caught(appResponse.getException());
                } else {
                    callback.done((V) appResponse.getValue());
                }
            }
        }
    };
    future.whenComplete(biConsumer);
}
Also used : AppResponse(org.apache.dubbo.rpc.AppResponse) CompletionException(java.util.concurrent.CompletionException) BiConsumer(java.util.function.BiConsumer)

Example 8 with AppResponse

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

the class ThriftCodecTest method testDecodeExceptionResponse.

@Test
public void testDecodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.class.getName());
    Channel channel = new MockedChannel(url);
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
    Request request = createRequest();
    DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
    TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId());
    TTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    TApplicationException exception = new TApplicationException();
    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.class.getName());
    // path
    protocol.writeString(Demo.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    protocol.writeMessageBegin(message);
    exception.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare
    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));
    Object obj = codec.decode((Channel) null, bis);
    Assertions.assertNotNull(obj);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertTrue(response.getResult() instanceof AppResponse);
    AppResponse result = (AppResponse) response.getResult();
    Assertions.assertTrue(result.hasException());
    Assertions.assertTrue(result.getException() instanceof RpcException);
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture) TApplicationException(org.apache.thrift.TApplicationException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 9 with AppResponse

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

the class ThriftCodecTest method testDecodeReplyResponse.

@Test
public void testDecodeReplyResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
    Request request = createRequest();
    DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
    TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId());
    Demo.echoString_result methodResult = new Demo.echoString_result();
    methodResult.success = "Hello, World!";
    TTransport transport = new TIOStreamTransport(bos);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    int messageLength, headerLength;
    // prepare
    protocol.writeI16(ThriftCodec.MAGIC);
    protocol.writeI32(Integer.MAX_VALUE);
    protocol.writeI16(Short.MAX_VALUE);
    protocol.writeByte(ThriftCodec.VERSION);
    protocol.writeString(Demo.Iface.class.getName());
    // path
    protocol.writeString(Demo.Iface.class.getName());
    protocol.writeI64(request.getId());
    protocol.getTransport().flush();
    headerLength = bos.size();
    protocol.writeMessageBegin(message);
    methodResult.write(protocol);
    protocol.writeMessageEnd();
    protocol.getTransport().flush();
    int oldIndex = messageLength = bos.size();
    try {
        bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
        protocol.writeI32(messageLength);
        bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
        protocol.writeI16((short) (0xffff & headerLength));
    } finally {
        bos.setWriteIndex(oldIndex);
    }
    // prepare
    byte[] buf = new byte[4 + bos.size()];
    System.arraycopy(bos.toByteArray(), 0, buf, 4, bos.size());
    ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);
    Object obj = codec.decode((Channel) null, bis);
    Assertions.assertNotNull(obj);
    Assertions.assertTrue(obj instanceof Response);
    Response response = (Response) obj;
    Assertions.assertEquals(request.getId(), response.getId());
    Assertions.assertTrue(response.getResult() instanceof AppResponse);
    AppResponse result = (AppResponse) response.getResult();
    Assertions.assertTrue(result.getValue() instanceof String);
    Assertions.assertEquals(methodResult.success, result.getValue());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) DefaultFuture(org.apache.dubbo.remoting.exchange.support.DefaultFuture) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) RandomAccessByteArrayOutputStream(org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) TTransport(org.apache.thrift.transport.TTransport) Test(org.junit.jupiter.api.Test)

Example 10 with AppResponse

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

the class MetricsFilterTest method testConsumerSuccess.

public void testConsumerSuccess() throws Exception {
    IMetricManager metricManager = MetricManager.getIMetricManager();
    metricManager.clear();
    MetricsFilter metricsFilter = new MetricsFilter();
    Invocation invocation = new RpcInvocation("sayName", DemoService.class.getName(), "", new Class<?>[] { Integer.class }, new Object[0]);
    RpcContext.getContext().setRemoteAddress(host, url.getPort()).setLocalAddress(host, NetUtils.getAvailablePort());
    RpcContext.getContext().setUrl(serviceInvoker.getUrl().addParameter(SIDE_KEY, CONSUMER_SIDE));
    AppResponse response = AppResponseBuilder.create().build();
    onInvokeReturns(response);
    for (int i = 0; i < 100; i++) {
        metricsFilter.invoke(serviceInvoker, invocation);
    }
    FastCompass dubboClient = metricManager.getFastCompass(DUBBO_GROUP, new MetricName(DUBBO_CONSUMER, MetricLevel.MAJOR));
    FastCompass dubboMethod = metricManager.getFastCompass(DUBBO_GROUP, new MetricName(DUBBO_CONSUMER_METHOD, new HashMap<String, String>(4) {

        {
            put(SERVICE, "org.apache.dubbo.monitor.dubbo.service.DemoService");
            put(METHOD, "void sayName(Integer)");
        }
    }, MetricLevel.NORMAL));
    long timestamp = System.currentTimeMillis() / 5000 * 5000;
    Assertions.assertEquals(100, dubboClient.getMethodCountPerCategory(0).get("success").get(timestamp));
    timestamp = timestamp / 15000 * 15000;
    Assertions.assertEquals(100, dubboMethod.getMethodCountPerCategory(0).get("success").get(timestamp));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) MetricName(com.alibaba.metrics.MetricName) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) FastCompass(com.alibaba.metrics.FastCompass) HashMap(java.util.HashMap) AppResponse(org.apache.dubbo.rpc.AppResponse) DemoService(org.apache.dubbo.monitor.dubbo.service.DemoService) IMetricManager(com.alibaba.metrics.IMetricManager)

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