Two things that could help:
-
Do not annotate with
@ChannelHandler.Sharableif your handler is NOT sharable. This can be misleading. Remove unnecessary state from handlers. In your case you should remove theremoteAddressmember variable and ensure thatGsonandCarParkPermissionServicecan be reused and are thread-safe. -
"Your remote address is embedded"is NOT an error. It actually is the message written by your handler onto the outbound channel (cf. yourchannelActive()method)
So it looks like it could work.
EDIT
Following your comments here are some clarifications regarding the second point. I mean that:
- your code making use of
EmbeddedChannelis almost correct. There is just a misunderstanding on the expected results (assert).
To make the unit test successful, you just have either:
- to comment this line in
channelActive():ctx.writeAndFlush("Your remote ...") - or to poll the second message from
Queue<Object> outboundMessagesintestHeartbeatMessage()
Indeed, when you do this:
// when
embeddedChannel.writeInbound(request);
(1) You actually open the channel once, which fires a channelActive() event. You don’t have a log in it but we see that the variable remoteAddress is not null afterwards, meaning that it was assigned in the channelActive() method.
(2) At the end of the channelActive() method, you eventually already send back a message by writing on the channel pipeline, as seen at this line:
ctx.writeAndFlush("Your remote address is " + remoteAddress + ".\r\n");
// In fact, this is the message you see in your failed assertion.
(3) Then the message written by embeddedChannel.writeInbound(request) is received and can be read, which fires a channelRead() event. This time, we see this in your log output:
22:21:29.062 [main] INFO handler.ProcessingHandler - CLIENT_IP: embedded
22:21:29.062 [main] INFO handler.ProcessingHandler - CLIENT_REQUEST: {"messageID":"heartbeat"}
22:21:29.067 [main] DEBUG handler.ProcessingHandler - heartbeat request: HeartbeatRequest(messageID=heartbeat)
(4) At the end of channelRead(ChannelHandlerContext ctx, Object msg), you will then send a second message (the expected one):
HeartbeatResponse response = HeartbeatResponse.builder()
.responseCode("ok")
.build();
ctx.writeAndFlush(response + "\n\r");
Therefore, with the following code of your unit test…
Queue<Object> outboundMessages = embeddedChannel.outboundMessages();
assertEquals(expected, outboundMessages.poll());
… you should be able to poll() two messages:
"Your remote address is embedded""{ResponseCode":"ok"}
Does it make sense for you?
9
solved Netty how to test Handler which uses Remote Address of a client