Reputation: 1811
I am using Netty's EmbeddedChannel in order to test some of my handlers.
I have a use case where i want to test that my handler distinguishes between two connections( channels ), based on their #remoteAddress().
Unfortunately, EmbeddedChannel uses EmbeddedSocketAddress, which provides a hard-coded value, for every channel created. Hence, two different EmbeddedChannel instances have the exact same #remoteAddress().
I have tried to Spy the EmbeddedChannel and mock out the remoteAddress method, but because of the EmbeddedChannel implementation this does not apply as the ChannelPipeline is created before the mocking takes place. Hence, the mocked value is not passed on the pipeline, as it has a reference to the non-mocked object.
Is there any alternative on how to achieve the above. Ideally i would like two different instances of EmbeddedChannel to have different #remoteAddress().
Thank you.
Upvotes: 1
Views: 741
Reputation: 55
You can try creating a custom channel that extends EmbeddedChannel
, then override protected SocketAddress remoteAddress0()
to return the address you want.
Something like this should do
public class CustomEmbeddedChannel extends EmbeddedChannel{
private InetSocketAddress socketAddress;
public CustomEmbeddedChannel(String host, int port, final ChannelHandler ... handlers){
super(handlers);
socketAddress = new InetSocketAddress(host, port);
}
@Override
protected SocketAddress remoteAddress0(){
return this.socketAddress;
}
}
Upvotes: 0
Reputation: 2216
Maybe you can try to use the id of the channel ?
see http://netty.io/4.1/api/io/netty/channel/AbstractChannel.html#id()
Of course this will lead to not behave as your current code based on remote Address. However basing only on remote Address could be not enough in some cases (for instance if the real remote is behind a proxy). So maybe you could consider id in "normal" situation too ?
Upvotes: 1