This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

testing servlet thread safety

 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Reid M. Pinchback
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both for your replies.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic