-
Notifications
You must be signed in to change notification settings - Fork 16
Description
If you try to create a TcpCrusher bound to port 0 - the serverSocketChannel will use an automatically allocated port from the ephemeral port range; however because the TcpCrusher will be listening on this port, the connecting application needs to know what this port is so that it can connect. Currently the TcpCrusher doesn't expose the actual port that was allocated; so calling code is unable to know which port should be used to connect.
In the TcpAcceptor, I suggest setting the bindAddress aftering the channel.bind is called; and then setting the bindAddress to the localAddress returned by the serverSocketChannel
if (bindAddress.getPort() == 0 && serverSocketChannel.getLocalAddress() instanceof InetSocketAddress) {
this.bindAddress = (InetSocketAddress) serverSocketChannel.getLocalAddress();
} else {
this.bindAddress = bindAddress;
}
And then adding a getter for the bindAddress
public InetSocketAddress getBindAddress() {
return bindAddress;
}
Within the TcpCrusher class the bindAddress parameter should not be final, and within the open method, when the acceptor is created, I suggest that the bindAddress variable is reset (before the logging to say that the crusher is open) by calling the new getter on the acceptor:
if (bindAddress.getPort() == 0) {
this.bindAddress = this.acceptor.getBindAddress();
}
Calling code is then able to inspect the port that was allocated by simply calling the existing TcpCrusher.getBindAddress().getPort() method