Search in sources :

Example 86 with ChannelHandlerContext

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

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 87 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project incubator-pulsar by apache.

the class ClientCnxTest method testClientCnxTimeout.

@Test
public void testClientCnxTimeout() throws Exception {
    EventLoopGroup eventLoop = EventLoopUtil.newEventLoopGroup(1, new DefaultThreadFactory("testClientCnxTimeout"));
    ClientConfigurationData conf = new ClientConfigurationData();
    conf.setOperationTimeoutMs(10);
    ClientCnx cnx = new ClientCnx(conf, eventLoop);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ChannelFuture listenerFuture = mock(ChannelFuture.class);
    when(listenerFuture.addListener(anyObject())).thenReturn(listenerFuture);
    when(ctx.writeAndFlush(anyObject())).thenReturn(listenerFuture);
    Field ctxField = PulsarHandler.class.getDeclaredField("ctx");
    ctxField.setAccessible(true);
    ctxField.set(cnx, ctx);
    try {
        cnx.newLookup(null, 123).get();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof PulsarClientException.TimeoutException);
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) ClientConfigurationData(org.apache.pulsar.client.impl.conf.ClientConfigurationData) ChannelFuture(io.netty.channel.ChannelFuture) Field(java.lang.reflect.Field) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Test(org.testng.annotations.Test)

Example 88 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project incubator-pulsar by apache.

the class ByteBufPairTest method testEncoder.

@Test
public void testEncoder() throws Exception {
    ByteBuf b1 = Unpooled.wrappedBuffer("hello".getBytes());
    ByteBuf b2 = Unpooled.wrappedBuffer("world".getBytes());
    ByteBufPair buf = ByteBufPair.get(b1, b2);
    assertEquals(buf.readableBytes(), 10);
    assertEquals(buf.getFirst(), b1);
    assertEquals(buf.getSecond(), b2);
    assertEquals(buf.refCnt(), 1);
    assertEquals(b1.refCnt(), 1);
    assertEquals(b2.refCnt(), 1);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.write(any(), any())).then(invocation -> {
        // Simulate a write on the context which releases the buffer
        ((ByteBuf) invocation.getArguments()[0]).release();
        return null;
    });
    ByteBufPair.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.testng.annotations.Test)

Example 89 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project incubator-pulsar by apache.

the class ClientErrorsTest method testProducerReconnect.

// Run this test multiple times to reproduce race conditions on reconnection logic
@Test(invocationCount = 10)
public void testProducerReconnect() throws Exception {
    AtomicInteger numOfConnections = new AtomicInteger();
    AtomicReference<ChannelHandlerContext> channelCtx = new AtomicReference<>();
    AtomicBoolean msgSent = new AtomicBoolean();
    mockBrokerService.setHandleConnect((ctx, connect) -> {
        channelCtx.set(ctx);
        ctx.writeAndFlush(Commands.newConnected(connect.getProtocolVersion()));
        if (numOfConnections.incrementAndGet() == 2) {
            // close the cnx immediately when trying to conenct the 2nd time
            ctx.channel().close();
        }
    });
    mockBrokerService.setHandleProducer((ctx, produce) -> {
        ctx.writeAndFlush(Commands.newProducerSuccess(produce.getRequestId(), "default-producer", SchemaVersion.Empty));
    });
    mockBrokerService.setHandleSend((ctx, sendCmd, headersAndPayload) -> {
        msgSent.set(true);
        ctx.writeAndFlush(Commands.newSendReceipt(0, 0, 1, 1));
    });
    PulsarClient client = PulsarClient.builder().serviceUrl("http://127.0.0.1:" + WEB_SERVICE_PORT).build();
    Producer<byte[]> producer = client.newProducer().topic("persistent://prop/use/ns/t1").create();
    // close the cnx after creating the producer
    channelCtx.get().channel().close().get();
    producer.send(new byte[0]);
    assertEquals(msgSent.get(), true);
    assertTrue(numOfConnections.get() >= 3);
    mockBrokerService.resetHandleConnect();
    mockBrokerService.resetHandleProducer();
    mockBrokerService.resetHandleSend();
    client.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.testng.annotations.Test)

Example 90 with ChannelHandlerContext

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext in project cypher-for-gremlin by opencypher.

the class CypherOpProcessor method explainQuery.

private void explainQuery(Context context, CypherAstWrapper ast, String gremlin) {
    Map<String, Object> explanation = new LinkedHashMap<>();
    explanation.put("translation", gremlin);
    explanation.put("options", ast.getOptions().toString());
    ResponseMessage explainMsg = ResponseMessage.build(context.getRequestMessage()).code(ResponseStatusCode.SUCCESS).statusMessage("OK").result(singletonList(explanation)).create();
    ChannelHandlerContext ctx = context.getChannelHandlerContext();
    ctx.writeAndFlush(explainMsg);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ResponseMessage(org.apache.tinkerpop.gremlin.driver.message.ResponseMessage) LinkedHashMap(java.util.LinkedHashMap)

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