JMX for TDD Integration and Acceptance Tests

While designing and implementing a test that has to wait for multiple sequential asynchronous steps to occur, it became apparent that JXM is a very good solution to sleeps in tests to avoid the flakey and long running test problem. It not only provides an alternative means of communication both into and out of the system, but it also provides event notification. This solution will work nicely for both integration and acceptance tests.

After the test code executes whatever is necessary to cause asynchronous executions, it can go into a timeout loop waiting for completion events via JMX. Upon receiving the appropriate events and/or inspecting appropriate state following those events, the test should immediately proceed with any queries and assertions.

The JMX solution involves creating a JMX MBean interface for the threads and registering them during thread startup initialization. The threads will increment counters indicating particular actions and fire appropriate events. The test will connect to the MBean Server, reduce the thread loop delay to speed up the testing, and register for event notification. The test will then perform test initialization actions and wait for appropriate events before querying and applying asserts to the results.

When implemented correctly, these tests will run quickly and without relying on flakey sleeps. Timeouts will still be necessary to signal a failure.

When an application goes into production the JMX server can be set to run over SSL with authentication for security. For Java 5 it is necessary to add the following arguments to the command line, but for Java 6, JMX is enabled by default.

Local access:

com.sun.management.jmxremote

Remote access:

com.sun.management.jmxremote.port=portNum


Notifications

Comments

Popular posts from this blog

Sites, Newsletters, and Blogs

Oracle JDBC ReadTimeout QueryTimeout

Locks held on Oracle for hours after sessions abnormally terminated by node failure