Network Tests: Address already in use
We keep getting this error in the unit tests that do socket IO. An example below.
The issue is always intermittent.
Possible mitigations:
- Don't use socket IO in unit tests.
- Make sure port isn't in use, if it's our own fault. Make sure those tests don't run in parallel, if they do.
- Catch the exeption and use another port.
org.bitcoinj.core.PeerGroupTest > preferLocalPeer[0] FAILED
java.net.BindException: Address already in use
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:459)
at java.base/sun.nio.ch.Net.bind(Net.java:448)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:73)
at org.bitcoinj.net.NioServer.<init>(NioServer.java:74)
at org.bitcoinj.testing.TestWithNetworkConnections.startPeerServer(TestWithNetworkConnections.java:133)
at org.bitcoinj.testing.TestWithNetworkConnections.startPeerServers(TestWithNetworkConnections.java:113)
at org.bitcoinj.testing.TestWithNetworkConnections.setUp(TestWithNetworkConnections.java:102)
at org.bitcoinj.testing.TestWithPeerGroup.setUp(TestWithPeerGroup.java:62)
at org.bitcoinj.testing.TestWithPeerGroup.setUp(TestWithPeerGroup.java:57)
at org.bitcoinj.core.PeerGroupTest.setUp(PeerGroupTest.java:80)
java.lang.RuntimeException: java.lang.NullPointerException
at org.bitcoinj.testing.TestWithPeerGroup.tearDown(TestWithPeerGroup.java:81)
at org.bitcoinj.core.PeerGroupTest.tearDown(PeerGroupTest.java:99)
Caused by:
java.lang.NullPointerException
at org.bitcoinj.testing.TestWithNetworkConnections.stopPeerServer(TestWithNetworkConnections.java:148)
at org.bitcoinj.testing.TestWithNetworkConnections.stopPeerServers(TestWithNetworkConnections.java:144)
at org.bitcoinj.testing.TestWithNetworkConnections.tearDown(TestWithNetworkConnections.java:139)
at org.bitcoinj.testing.TestWithPeerGroup.tearDown(TestWithPeerGroup.java:76)
... 1 more
I'm hoping that after we migrate Peer/PeerGroup I/O to CompletableFuture we can work to improve these tests.
I pushed 006c00ac35790da0cf9446b3541e72e58b2fb17b to try to help with this issue.
It just picks a random port between 10000 and 50000. The idea is that if the port clashes happen between two of our CI runs running at the same time, they would now use different ports (with a high probability).
While I was investigating, I found out we could get an unbound port from the system by supplying a 0 port to the InetSocketAddress constructors. However, it seems with the current structure of these "tests with network connection" its hard communicate the actually assigned port number to the client.