Search in sources :

Example 41 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class HeartBeatTaskTest method setup.

@BeforeEach
public void setup() throws Exception {
    long tickDuration = 1000;
    heartbeatTimer = new HashedWheelTimer(tickDuration / HEARTBEAT_CHECK_TICK, TimeUnit.MILLISECONDS);
    channel = new MockChannel() {

        @Override
        public URL getUrl() {
            return url;
        }
    };
    AbstractTimerTask.ChannelProvider cp = () -> Collections.<Channel>singletonList(channel);
    heartbeatTimerTask = new HeartbeatTimerTask(cp, tickDuration / HEARTBEAT_CHECK_TICK, (int) tickDuration);
}
Also used : Channel(org.apache.dubbo.remoting.Channel) HashedWheelTimer(org.apache.dubbo.common.timer.HashedWheelTimer) URL(org.apache.dubbo.common.URL) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 42 with Channel

use of org.apache.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);
    Assertions.assertEquals(isChannelClose, channel.isClosed());
}
Also used : Channel(org.apache.dubbo.remoting.Channel) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer)

Example 43 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class ExchangeCodecTest method testMessageLengthGreaterThanMessageActualLength.

@Test
public void testMessageLengthGreaterThanMessageActualLength() throws Exception {
    Channel channel = getCliendSideChannel(url);
    Request request = new Request(1L);
    request.setVersion(Version.getProtocolVersion());
    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);
    Assertions.assertEquals(date, decodedRequest.getData());
    Assertions.assertEquals(bytes.length + padding, decodeBuffer.readerIndex());
    decodedRequest = (Request) codec.decode(channel, decodeBuffer);
    Assertions.assertEquals(date, decodedRequest.getData());
}
Also used : Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(org.apache.dubbo.common.io.UnsafeByteArrayOutputStream) Date(java.util.Date) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 44 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Check_Payload.

@Test
public void test_Decode_Check_Payload() throws IOException {
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
    byte[] request = assemblyDataProtocol(header);
    try {
        Channel channel = getServerSideChannel(url);
        ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(request);
        Object obj = codec.decode(channel, buffer);
        Assertions.assertTrue(obj instanceof Response);
        Assertions.assertTrue(((Response) obj).getErrorMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[] { 1, 1, 1, 1 })));
    } catch (IOException expected) {
        Assertions.assertTrue(expected.getMessage().startsWith("Data length too large: " + Bytes.bytes2int(new byte[] { 1, 1, 1, 1 })));
    }
}
Also used : Response(org.apache.dubbo.remoting.exchange.Response) Channel(org.apache.dubbo.remoting.Channel) IOException(java.io.IOException) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) Test(org.junit.jupiter.api.Test)

Example 45 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class ExchangeCodecTest method test_Decode_Error_Length.

@Test
public void test_Decode_Error_Length() throws IOException {
    byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, 0x02, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    Person person = new Person();
    byte[] request = getRequestBytes(person, header);
    Channel channel = getServerSideChannel(url);
    byte[] baddata = new byte[] { 1, 2 };
    ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(join(request, baddata));
    Response obj = (Response) codec.decode(channel, buffer);
    Assertions.assertEquals(person, obj.getResult());
    // only decode necessary bytes
    Assertions.assertEquals(request.length, buffer.readerIndex());
}
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

Channel (org.apache.dubbo.remoting.Channel)53 Test (org.junit.jupiter.api.Test)32 Request (org.apache.dubbo.remoting.exchange.Request)18 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)16 Response (org.apache.dubbo.remoting.exchange.Response)12 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)9 URL (org.apache.dubbo.common.URL)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 RemotingException (org.apache.dubbo.remoting.RemotingException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 Demo (org.apache.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 IOException (java.io.IOException)3