I wanted to write about a little project I have been working on which I added to the Mule testing framework recently: dynamic port testing. As long as I have been working on the Mule ESB project, we have had weird random intermittent test failures which fail with a message like this:
Caused by: org.mule.transport.ConnectException: Failed to bind to uri at org.mule.transport.tcp.TcpMessageReceiver.doConnect(TcpMessageReceiver.java:81) at org.mule.transport.http.HttpMessageReceiver.doConnect(HttpMessageReceiver.java:83) at org.mule.transport.AbstractConnectable$3.doWork(AbstractConnectable.java:238) at org.mule.retry.policies.AbstractPolicyTemplate.execute(AbstractPolicyTemplate.java:67) ... 50 more Caused by: java.
BindException: Address already in use
Most of the time, the condition is not reproducible and re-running the test again would pass. After a lot of investigation/poking around, I traced the cause of the problem to be the OS not releasing the port quickly enough for the next test to use it. This usually happened when the build machine was under high load. That’s where dynamic port testing comes in. Using variable substitution in our Mule test configuration files, I wrote a wrapper test framework class which looks for and assigns free ports to tests so we can avoid the ‘Address already in use’ condition.
If you are writing functional test cases for Mule ESB and would like to use dynamic ports, you simply need to do the following: