Nikola Nikola - 1 month ago 6
Java Question

EmbeddedChannel and channelActive

I am writing unit test for my application using

EmbeddedChannel
:

@Test
public void sendMessage() {
CurrentMessage currentMessage = new CurrentMessage();
currentMessage.setCommand(CommandEnum.CURRENT_MEASUREMENT_REQUEST);

EmbeddedChannel channel = new EmbeddedChannel();
channel.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
channel.pipeline().addLast(new ClientEventHandler(currentMessage));
assertTrue(channel.isActive());
}


But my
channelActive
from
ClientEventHandler
is no invoked:

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
IConverter converter = CommandUtil.commands.get(message.getCommand().getHexValue());

byte[] byteMessage = converter.encode(message);

logger.info("Sending byte array message: " + byteMessage);

ctx.writeAndFlush(Unpooled.copiedBuffer(byteMessage));
}


Does
EmbeddedChannel
supports
channelActive
or
channelRegister
?

Answer

A EmbeddedChannel is active after its constructed so the handlers you are added are added too late. Add them via the constructor and it should work:

EmbeddedChannel channel = new EmbeddedChannel(
        new LoggingHandler(LogLevel.TRACE),
        new ClientEventHandler(currentMessage));
Comments