Wednesday, February 29, 2012

WaitUtils - no more arbitrary sleeps in tests

Its the year 2012 and I still see functional test code bases using Thread.sleep() at arbitrary places. Its very surprising that the person who has written this can't think of why this could be a problem (Flaky Tests) or how it can be fixed.

As mentioned in an earlier post, I have created a very simple library for Java - WaitUtils. You can checkout the code and download it from Github. Its a BSD 2 Clause licensed open source software.

The library is really simple - you always make sure you wait for some thing i.e. an event or a predicate. You never sleep without answering some basic questions. This way whenever there is a need to wait, you will do so thinking about things like:
  • What am I waiting for?
  • How long should I wait for?
  • What should I do if what I am waiting for does not happen?
Now, these look trivial, in fact, obvious. However, you would be surprised at how many functional tests do not follow this. By answering these simple questions, you can make sure that your tests are reasoned out better. 

The library intends to solve this once and for all by introducing a WaitUtils. You can download the jar, attach source and checkout the Java docs. The API is trivial and is self-explanatory.

No comments: