Search in sources :

Example 6 with Request

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

the class ExchangeCodecTest method test_Decode_Return_Request_Object.

@Test
public void test_Decode_Return_Request_Object() throws IOException {
    // |10011111|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);
    Request obj = (Request) decode(request);
    Assert.assertEquals(person, obj.getData());
    Assert.assertEquals(true, obj.isTwoWay());
    Assert.assertEquals(false, obj.isHeartbeat());
    Assert.assertEquals("2.0.0", obj.getVersion());
    System.out.println(obj);
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 7 with Request

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

the class ExchangeCodecTest method test_Decode_Return_Request_Event_Object.

@Test
public void test_Decode_Return_Request_Event_Object() throws IOException {
    // |10011111|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);
    Request obj = (Request) decode(request);
    Assert.assertEquals(person, obj.getData());
    Assert.assertEquals(true, obj.isTwoWay());
    Assert.assertEquals(true, obj.isEvent());
    Assert.assertEquals("2.0.0", obj.getVersion());
    System.out.println(obj);
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 8 with Request

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

the class ExchangeCodecTest method testMessageLengthGreaterThanMessageActualLength.

// http://code.alibabatech.com/jira/browse/DUBBO-392
@Test
public void testMessageLengthGreaterThanMessageActualLength() throws Exception {
    Channel channel = getCliendSideChannel(url);
    Request request = new Request(1L);
    request.setVersion("2.0.0");
    Date date = new Date();
    request.setData(date);
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, encodeBuffer, request);
    byte[] bytes = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(bytes);
    int len = Bytes.bytes2int(bytes, 12);
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    out.write(bytes, 0, 12);
    /*
         * The fill length can not be less than 256, because by default, hessian reads 256 bytes from the stream each time.
         * Refer Hessian2Input.readBuffer for more details
         */
    int padding = 512;
    out.write(Bytes.int2bytes(len + padding));
    out.write(bytes, 16, bytes.length - 16);
    for (int i = 0; i < padding; i++) {
        out.write(1);
    }
    out.write(bytes);
    /* request|1111...|request */
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(out.toByteArray());
    Request decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertTrue(date.equals(decodedRequest.getData()));
    Assert.assertEquals(bytes.length + padding, decodeBuffer.readerIndex());
    decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertTrue(date.equals(decodedRequest.getData()));
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(com.alibaba.dubbo.common.io.UnsafeByteArrayOutputStream) Date(java.util.Date) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 9 with Request

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

the class ExchangeCodecTest method test_Decode_Return_Request_Heartbeat_Object.

@Test
public void test_Decode_Return_Request_Heartbeat_Object() throws IOException {
    // |10011111|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 };
    byte[] request = getRequestBytes(null, header);
    Request obj = (Request) decode(request);
    Assert.assertEquals(null, obj.getData());
    Assert.assertEquals(true, obj.isTwoWay());
    Assert.assertEquals(true, obj.isHeartbeat());
    Assert.assertEquals("2.0.0", obj.getVersion());
    System.out.println(obj);
}
Also used : Request(com.alibaba.dubbo.remoting.exchange.Request) Test(org.junit.Test)

Example 10 with Request

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

the class ConnectChannelHandlerTest method test_Received_Event_invoke_direct.

/**
 * Events do not pass through the thread pool and execute directly on the IO
 */
@SuppressWarnings("deprecation")
@Ignore("Heartbeat is processed in HeartbeatHandler not WrappedChannelHandler.")
@Test
public void test_Received_Event_invoke_direct() throws RemotingException {
    handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
    ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "SHARED_EXECUTOR", 1);
    executor.shutdown();
    executor = (ThreadPoolExecutor) getField(handler, "executor", 1);
    executor.shutdown();
    Request req = new Request();
    req.setHeartbeat(true);
    final AtomicInteger count = new AtomicInteger(0);
    handler.received(new MockedChannel() {

        @Override
        public void send(Object message) throws RemotingException {
            Assert.assertEquals("response.heartbeat", true, ((Response) message).isHeartbeat());
            count.incrementAndGet();
        }
    }, req);
    Assert.assertEquals("channel.send must be invoke", 1, count.get());
}
Also used : Response(com.alibaba.dubbo.remoting.exchange.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemotingException(com.alibaba.dubbo.remoting.RemotingException) Request(com.alibaba.dubbo.remoting.exchange.Request) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ConnectionOrderedChannelHandler(com.alibaba.dubbo.remoting.transport.dispatcher.connection.ConnectionOrderedChannelHandler) Ignore(org.junit.Ignore) 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