Search in sources :

Example 71 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project baseio by generallycloud.

the class TestLoadEchoClient1 method prepare.

@Override
public void prepare() throws Exception {
    eventHandleAdaptor = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            // System.out.println("_________________"+msg);
            // ctx.write(msg);
            addCount(1);
        }
    };
    Bootstrap b = new Bootstrap();
    b.group(group);
    b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, false);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
            pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
            pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
            pipeline.addLast("handler", eventHandleAdaptor);
        }
    });
    f = b.connect("localhost", 5656).sync();
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) LengthFieldPrepender(io.netty.handler.codec.LengthFieldPrepender) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) StringEncoder(io.netty.handler.codec.string.StringEncoder) Bootstrap(io.netty.bootstrap.Bootstrap) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 72 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultRequestProcessorTest method testProcessRequest_UnregisterBroker.

@Test
public void testProcessRequest_UnregisterBroker() throws RemotingCommandException, NoSuchFieldException, IllegalAccessException {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(null);
    // Register broker
    RemotingCommand regRequest = genSampleRegisterCmd(true);
    defaultRequestProcessor.processRequest(ctx, regRequest);
    // Unregister broker
    RemotingCommand unregRequest = genSampleRegisterCmd(false);
    RemotingCommand unregResponse = defaultRequestProcessor.processRequest(ctx, unregRequest);
    assertThat(unregResponse.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(unregResponse.getRemark()).isNull();
    RouteInfoManager routes = namesrvController.getRouteInfoManager();
    Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable");
    brokerAddrTable.setAccessible(true);
    assertThat((Map) brokerAddrTable.get(routes)).isNotEmpty();
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Field(java.lang.reflect.Field) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RouteInfoManager(org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 73 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project rocketmq-rocketmq-all-4.1.0-incubating by lirenzuo.

the class DefaultRequestProcessorTest method testProcessRequest_RegisterBrokerWithFilterServer.

@Test
public void testProcessRequest_RegisterBrokerWithFilterServer() throws RemotingCommandException, NoSuchFieldException, IllegalAccessException {
    RemotingCommand request = genSampleRegisterCmd(true);
    // version >= MQVersion.Version.V3_0_11.ordinal() to register with filter server
    request.setVersion(100);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(null);
    RemotingCommand response = defaultRequestProcessor.processRequest(ctx, request);
    assertThat(response.getCode()).isEqualTo(ResponseCode.SUCCESS);
    assertThat(response.getRemark()).isNull();
    RouteInfoManager routes = namesrvController.getRouteInfoManager();
    Field brokerAddrTable = RouteInfoManager.class.getDeclaredField("brokerAddrTable");
    brokerAddrTable.setAccessible(true);
    BrokerData broker = new BrokerData();
    broker.setBrokerName("broker");
    broker.setBrokerAddrs((HashMap) Maps.newHashMap(new Long(2333), "10.10.1.1"));
    assertThat((Map) brokerAddrTable.get(routes)).contains(new HashMap.SimpleEntry("broker", broker));
}
Also used : RemotingCommand(org.apache.rocketmq.remoting.protocol.RemotingCommand) Field(java.lang.reflect.Field) BrokerData(org.apache.rocketmq.common.protocol.route.BrokerData) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RouteInfoManager(org.apache.rocketmq.namesrv.routeinfo.RouteInfoManager) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Example 74 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project bookkeeper by apache.

the class BookieProtoEncodingTest method testV3ResponseDecoderNoFallback.

@Test
public void testV3ResponseDecoderNoFallback() throws Exception {
    AddResponse v2Resp = AddResponse.create(BookieProtocol.CURRENT_PROTOCOL_VERSION, BookieProtocol.EOK, 1L, 2L);
    BookkeeperProtocol.Response v3Resp = BookkeeperProtocol.Response.newBuilder().setHeader(BKPacketHeader.newBuilder().setVersion(ProtocolVersion.VERSION_THREE).setTxnId(1L).setOperation(OperationType.ADD_ENTRY).build()).setStatus(StatusCode.EOK).setAddResponse(BookkeeperProtocol.AddResponse.newBuilder().setStatus(StatusCode.EOK).setLedgerId(1L).setEntryId(2L).build()).build();
    List<Object> outList = Lists.newArrayList();
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.fireChannelRead(any())).thenAnswer((iom) -> {
        outList.add(iom.getArgument(0));
        return null;
    });
    ResponseEnDeCoderPreV3 v2Encoder = new ResponseEnDeCoderPreV3(null);
    ResponseEnDecoderV3 v3Encoder = new ResponseEnDecoderV3(null);
    ResponseDecoder v3Decoder = new ResponseDecoder(null, false);
    try {
        v3Decoder.channelRead(ctx, v2Encoder.encode(v2Resp, UnpooledByteBufAllocator.DEFAULT));
        fail("V3 response decoder should fail on decoding v2 response");
    } catch (InvalidProtocolBufferException e) {
    // expected
    }
    assertEquals(0, outList.size());
    v3Decoder.channelRead(ctx, v3Encoder.encode(v3Resp, UnpooledByteBufAllocator.DEFAULT));
    assertEquals(1, outList.size());
}
Also used : ResponseEnDeCoderPreV3(org.apache.bookkeeper.proto.BookieProtoEncoding.ResponseEnDeCoderPreV3) ResponseDecoder(org.apache.bookkeeper.proto.BookieProtoEncoding.ResponseDecoder) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ResponseEnDecoderV3(org.apache.bookkeeper.proto.BookieProtoEncoding.ResponseEnDecoderV3) AddResponse(org.apache.bookkeeper.proto.BookieProtocol.AddResponse) Test(org.junit.Test)

Example 75 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project bookkeeper by apache.

the class ByteBufListTest method testEncoder.

@Test
public void testEncoder() throws Exception {
    ByteBuf b1 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b1.writerIndex(b1.capacity());
    ByteBuf b2 = PooledByteBufAllocator.DEFAULT.heapBuffer(128, 128);
    b2.writerIndex(b2.capacity());
    ByteBufList buf = ByteBufList.get(b1, b2);
    ChannelHandlerContext ctx = new MockChannelHandlerContext();
    ByteBufList.ENCODER.write(ctx, buf, null);
    assertEquals(buf.refCnt(), 0);
    assertEquals(b1.refCnt(), 0);
    assertEquals(b2.refCnt(), 0);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)661 Channel (io.netty.channel.Channel)274 ByteBuf (io.netty.buffer.ByteBuf)234 ChannelFuture (io.netty.channel.ChannelFuture)210 Test (org.junit.Test)208 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)201 Bootstrap (io.netty.bootstrap.Bootstrap)177 InetSocketAddress (java.net.InetSocketAddress)160 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)156 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)154 Test (org.junit.jupiter.api.Test)153 ChannelPipeline (io.netty.channel.ChannelPipeline)151 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)141 AtomicReference (java.util.concurrent.atomic.AtomicReference)140 IOException (java.io.IOException)137 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)120 ClosedChannelException (java.nio.channels.ClosedChannelException)119 CountDownLatch (java.util.concurrent.CountDownLatch)115 ArrayList (java.util.ArrayList)113 List (java.util.List)111