Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Testing and the fly likes testing servlet thread safety Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "testing servlet thread safety" Watch "testing servlet thread safety" New topic

testing servlet thread safety

Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
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?

Matthew Phillips
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
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).

Reid - SCJP2 (April 2002)
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
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
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Thank you both for your replies.
I agree. Here's the link:
subject: testing servlet thread safety
It's not a secret anymore!