Search in sources :

Example 1 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class TelnetCodecTest method testEecode_assertEquals.

protected void testEecode_assertEquals(Object request, byte[] ret, boolean isServerside) throws IOException {
    // init channel
    Channel channel = isServerside ? getServerSideChannel(url) : getCliendSideChannel(url);
    ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(1024);
    codec.encode(channel, buffer, request);
    byte[] data = new byte[buffer.readableBytes()];
    buffer.readBytes(data);
    Assertions.assertEquals(ret.length, data.length);
    for (int i = 0; i < ret.length; i++) {
        if (ret[i] != data[i]) {
            Assertions.fail();
        }
    }
}
Also used : Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 2 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class TelnetCodecTest method testDecode_PersonWithEnterByte.

private void testDecode_PersonWithEnterByte(byte[] enterbytes, boolean isNeedmore) throws IOException {
    // init channel
    Channel channel = getServerSideChannel(url);
    // init request string
    Person request = new Person();
    byte[] newbuf = join(objectToByte(request), enterbytes);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(newbuf);
    // decode
    Object obj = codec.decode(channel, buffer);
    if (isNeedmore) {
        Assertions.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT, obj);
    } else {
        Assertions.assertTrue(obj instanceof String, "return must string ");
    }
}
Also used : Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 3 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class TelnetCodecTest method testDecode_assertEquals.

private void testDecode_assertEquals(AbstractMockChannel channel, Object request, Object expectret, Object channelReceive) throws IOException {
    // init channel
    if (channel == null) {
        channel = getServerSideChannel(url);
    }
    byte[] buf = objectToByte(request);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(buf);
    // decode
    Object obj = codec.decode(channel, buffer);
    Assertions.assertEquals(expectret, obj);
    Assertions.assertEquals(channelReceive, channel.getReceivedMessage());
}
Also used : ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 4 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class TelnetCodecTest method testDecode_assertEquals.

protected void testDecode_assertEquals(byte[] request, Object ret, boolean isServerside) throws IOException {
    // init channel
    Channel channel = isServerside ? getServerSideChannel(url) : getCliendSideChannel(url);
    // init request string
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request);
    // decode
    Object obj = codec.decode(channel, buffer);
    Assertions.assertEquals(ret, obj);
}
Also used : Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 5 with ChannelBuffer

use of org.apache.dubbo.remoting.buffer.ChannelBuffer in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Error_Response.

@Test
public void test_Encode_Error_Response() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
    Channel channel = getCliendSideChannel(url);
    Response response = new Response();
    response.setHeartbeat(true);
    response.setId(1001L);
    response.setStatus((byte) 10);
    response.setVersion("11");
    String badString = "bad";
    response.setErrorMessage(badString);
    Person person = new Person();
    response.setResult(person);
    codec.encode(channel, encodeBuffer, response);
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    // encode resault check need decode
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Response obj = (Response) codec.decode(channel, decodeBuffer);
    Assertions.assertEquals(response.getId(), obj.getId());
    Assertions.assertEquals(response.getStatus(), obj.getStatus());
    Assertions.assertEquals(response.isHeartbeat(), obj.isHeartbeat());
    Assertions.assertEquals(badString, obj.getErrorMessage());
    Assertions.assertNull(obj.getResult());
// Assertions.assertEquals(response.getProtocolVersion(), obj.getVersion());
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)22 Channel (org.apache.dubbo.remoting.Channel)16 Test (org.junit.jupiter.api.Test)13 Request (org.apache.dubbo.remoting.exchange.Request)9 Response (org.apache.dubbo.remoting.exchange.Response)9 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)5 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)5 TMessage (org.apache.thrift.protocol.TMessage)5 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)5 URL (org.apache.dubbo.common.URL)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 TTransport (org.apache.thrift.transport.TTransport)3 DynamicChannelBuffer (org.apache.dubbo.remoting.buffer.DynamicChannelBuffer)2 DefaultFuture (org.apache.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (org.apache.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 TApplicationException (org.apache.thrift.TApplicationException)2 Buffer (org.glassfish.grizzly.Buffer)2 ByteBuf (io.netty.buffer.ByteBuf)1