Search in sources :

Example 31 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method testMessageLengthExceedPayloadLimitWhenEncode.

@Test
public void testMessageLengthExceedPayloadLimitWhenEncode() throws Exception {
    Request request = new Request(1L);
    request.setData("hello");
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    AbstractMockChannel channel = getCliendSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    try {
        codec.encode(channel, encodeBuffer, request);
        Assert.fail();
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().startsWith("Data length too large: " + 6));
    }
    Response response = new Response(1L);
    response.setResult("hello");
    encodeBuffer = ChannelBuffers.dynamicBuffer(512);
    channel = getServerSideChannel(url.addParameter(Constants.PAYLOAD_KEY, 4));
    codec.encode(channel, encodeBuffer, response);
    Assert.assertTrue(channel.getReceivedMessage() instanceof Response);
    Response receiveMessage = (Response) channel.getReceivedMessage();
    Assert.assertEquals(Response.BAD_RESPONSE, receiveMessage.getStatus());
    Assert.assertTrue(receiveMessage.getErrorMessage().contains("Data length too large: "));
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) Request(com.alibaba.dubbo.remoting.exchange.Request) IOException(java.io.IOException) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 32 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Error_Request_Object.

@Test
public void test_Decode_Error_Request_Object() throws IOException {
    // 00000010-response/oneway/hearbeat=true |20-stats=ok|id=0|length=0
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xe2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Person person = new Person();
    byte[] request = getRequestBytes(person, header);
    // bad object
    byte[] badbytes = new byte[] { -1, -2, -3, -4, -3, -4, -3, -4, -3, -4, -3, -4 };
    System.arraycopy(badbytes, 0, request, 21, badbytes.length);
    Request obj = (Request) decode(request);
    Assert.assertEquals(true, obj.isBroken());
    Assert.assertEquals(true, obj.getData() instanceof Throwable);
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 33 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeartBeatTaskTest method testHeartBeat.

@Test
public void testHeartBeat() throws Exception {
    url = url.addParameter(Constants.DUBBO_VERSION_KEY, "2.1.1");
    channel.setAttribute(HeaderExchangeHandler.KEY_READ_TIMESTAMP, System.currentTimeMillis());
    channel.setAttribute(HeaderExchangeHandler.KEY_WRITE_TIMESTAMP, System.currentTimeMillis());
    Thread.sleep(2000L);
    task.run();
    List<Object> objects = channel.getSentObjects();
    Assert.assertTrue(objects.size() > 0);
    Object obj = objects.get(0);
    Assert.assertTrue(obj instanceof Request);
    Request request = (Request) obj;
    Assert.assertTrue(request.isHeartbeat());
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 34 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeHandlerTest method test_received_request_twoway_error_reply.

@Test
public void test_received_request_twoway_error_reply() throws RemotingException {
    final Person requestdata = new Person("charles");
    final Request request = new Request();
    request.setTwoWay(true);
    request.setData(requestdata);
    final AtomicInteger count = new AtomicInteger(0);
    final Channel mchannel = new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Response res = (Response) message;
            Assert.assertEquals(request.getId(), res.getId());
            Assert.assertEquals(request.getVersion(), res.getVersion());
            Assert.assertEquals(Response.SERVICE_ERROR, res.getStatus());
            Assert.assertNull(res.getResult());
            Assert.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
            count.incrementAndGet();
        }
    };
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        @Override
        public Object reply(ExchangeChannel channel, Object request) throws RemotingException {
            throw new BizException();
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
    Assert.assertEquals(1, count.get());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Channel(com.alibaba.dubbo.remoting.Channel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) Request(com.alibaba.dubbo.remoting.exchange.Request) ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) ExchangeHandler(com.alibaba.dubbo.remoting.exchange.ExchangeHandler) Test(org.junit.Test)

Example 35 with Request

use of com.alibaba.dubbo.remoting.exchange.Request in project dubbo by alibaba.

the class HeaderExchangeHandlerTest method test_received_request_oneway.

@Test
public void test_received_request_oneway() throws RemotingException {
    final Channel mchannel = new MockedChannel();
    final Person requestdata = new Person("charles");
    Request request = new Request();
    request.setTwoWay(false);
    request.setData(requestdata);
    ExchangeHandler exhandler = new MockedExchangeHandler() {

        public void received(Channel channel, Object message) throws RemotingException {
            Assert.assertEquals(requestdata, message);
        }
    };
    HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
    hexhandler.received(mchannel, request);
}
Also used : ExchangeChannel(com.alibaba.dubbo.remoting.exchange.ExchangeChannel) Channel(com.alibaba.dubbo.remoting.Channel) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) Request(com.alibaba.dubbo.remoting.exchange.Request) HeaderExchangeHandler(com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler) ExchangeHandler(com.alibaba.dubbo.remoting.exchange.ExchangeHandler) Test(org.junit.Test)

Aggregations

Request (com.alibaba.dubbo.remoting.exchange.Request)38 Test (org.junit.Test)22 Response (com.alibaba.dubbo.remoting.exchange.Response)18 Channel (com.alibaba.dubbo.remoting.Channel)14 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)10 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)8 RemotingException (com.alibaba.dubbo.remoting.RemotingException)7 TMessage (org.apache.thrift.protocol.TMessage)7 HeaderExchangeHandler (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)6 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)6 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)6 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)5 RpcResult (com.alibaba.dubbo.rpc.RpcResult)5 URL (com.alibaba.dubbo.common.URL)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Serialization (com.alibaba.dubbo.common.serialize.Serialization)3 ExchangeHandler (com.alibaba.dubbo.remoting.exchange.ExchangeHandler)3 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)3 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)3