This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a servlet that uses an API that I know not to be thread safe. I'm pretty sure that my servlet has been written in such a way that it will be thread safe, but I want to be absolutely sure. I'm using HttpUnit to test the servlet. Does anyone have any recommendations for testing the thread safety of the servlet?
Tough problem in general. I once looked at some code somebody wrote purporting to do unit testing on threads, but it just consisted of spawning a bunch of threads and hoping the test would fail if something went wrong. Given the unpredictable nature of thread scheduling (particularly for code that could be ported to environments with different approaches to thread scheduling), it is challenging to craft a robust testing harness. Thread safety is often more tractable when approached analytically instead of empirically. In practical terms, you write code using techniques that help you to avoid threading problems. The Doug Lea java book is a good source of background on this. The problem with the empirical (testing) approach is simply one of numbers. For tests to help you they have to exercise a known path through the code you are concerned about, and the total sum of all your test cases hopefully gives you coverage (or at least reasonable coverage) of your code. Threading causes an exponential explosion in the number of paths that need testing, and it can be very difficult to force a test to exercise a specific path (e.g. given two clients of a web app, you don't get to force how the intermediate steps of their request will be processed by the JVM).
There is a chapter about testing multi-threaded logic in Johannes Link's JUnit book. The conclusion is that you can drive multi-threaded designs by tests, but that testing for thread-safety is very hard, if not impossible in general. You most probably will have to use other means to convince yourself of the thread-safety of your code, such as using known-to-be-good patterns or libraries, code reviews and making the multi-threaded logic as simple and isolated as possible.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Mar 09, 2001
Thank you both for your replies.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com