Search in sources :

Example 26 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class ExchangeCodecTest method test_Encode_Request.

@Test
public void test_Encode_Request() throws IOException {
    ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(2014);
    Channel channel = getCliendSideChannel(url);
    Request request = new Request();
    Person person = new Person();
    request.setData(person);
    codec.encode(channel, encodeBuffer, request);
    // encode resault check need decode
    byte[] data = new byte[encodeBuffer.writerIndex()];
    encodeBuffer.readBytes(data);
    ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(data);
    Request obj = (Request) codec.decode(channel, decodeBuffer);
    Assert.assertEquals(request.isBroken(), obj.isBroken());
    Assert.assertEquals(request.isHeartbeat(), obj.isHeartbeat());
    Assert.assertEquals(request.isTwoWay(), obj.isTwoWay());
    Assert.assertEquals(person, obj.getData());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) Request(com.alibaba.dubbo.remoting.exchange.Request) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.Test)

Example 27 with Channel

use of com.alibaba.dubbo.remoting.Channel in project dubbo by alibaba.

the class TelnetCodecTest method testDecode_WithExitByte.

private void testDecode_WithExitByte(byte[] exitbytes, boolean isChannelClose) throws IOException {
    // init channel
    Channel channel = getServerSideChannel(url);
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(exitbytes);
    // decode
    codec.decode(channel, buffer);
    Assert.assertEquals(isChannelClose, channel.isClosed());
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 28 with Channel

use of com.alibaba.dubbo.remoting.Channel 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) {
        Assert.assertEquals(Codec2.DecodeResult.NEED_MORE_INPUT, obj);
    } else {
        Assert.assertTrue("return must string ", obj instanceof String);
    }
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 29 with Channel

use of com.alibaba.dubbo.remoting.Channel 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);
    Assert.assertEquals(ret, obj);
}
Also used : Channel(com.alibaba.dubbo.remoting.Channel) ChannelBuffer(com.alibaba.dubbo.remoting.buffer.ChannelBuffer)

Example 30 with Channel

use of com.alibaba.dubbo.remoting.Channel 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)

Aggregations

Channel (com.alibaba.dubbo.remoting.Channel)35 Test (org.junit.Test)16 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)15 Request (com.alibaba.dubbo.remoting.exchange.Request)14 Response (com.alibaba.dubbo.remoting.exchange.Response)10 ExchangeChannel (com.alibaba.dubbo.remoting.exchange.ExchangeChannel)8 HeaderExchangeHandler (com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 URL (com.alibaba.dubbo.common.URL)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 RemotingException (com.alibaba.dubbo.remoting.RemotingException)4 RpcResult (com.alibaba.dubbo.rpc.RpcResult)4 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TMessage (org.apache.thrift.protocol.TMessage)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 ExchangeHandler (com.alibaba.dubbo.remoting.exchange.ExchangeHandler)3 Client (com.alibaba.dubbo.remoting.Client)2 DefaultFuture (com.alibaba.dubbo.remoting.exchange.support.DefaultFuture)2 RandomAccessByteArrayOutputStream (com.alibaba.dubbo.rpc.protocol.thrift.io.RandomAccessByteArrayOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2