Running good tests is hard. Running tests which test various scenarios in concurrent environment is even harder.
One of my most recent use-cases was to write an integration test which proves that concurrent update of the same entity in a persistent storage throws an exception. There are dozens of other use-cases which require similar test to be written.
The first attempt to write this kind of code is to explicitly create an ExecutorService and submit threads which execute the method being tested. But there was a lot of boilerplate code which pollutes the test. Isn’t there a more elegant solution?
After few attempts to improve this, I came up with the following utility method:
The above code allows to easily test any kind of logic in parallel using CompletableFuture & Stream utilities. Using the above utility looks pretty concise and clear:
This test assert that a hypothetical userService throws the CASMismatchException when the update operation is performed concurrently.
The above example shows an approach of easily test how a code behave in concurrent environment.comments powered by Disqus