Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

testing servlet thread safety

 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your replies.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic