Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

testing of singleton for thread safety  RSS feed

 
P. Ingle
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

What is the best way to test a singleton class to ensure thread safety
I have DAOFactory implemented as singleton. i would like to test it in single node and in clustered environment. What is the best way to go about it?

Thanks,
P. Ingle
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Proving a class thread safe through tests will be tough because thread behavior is so non-deterministic. You could run a million repetitions and still hit a new race condition on the next. It might be more productive to eye-ball it and make sure it's following good practices ... try not to use any shared resources, synchronize any that you do use, others?
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that eyeballs are good ... perhaps in terms of code review or code inspection.

If the threads communicate, it's also worthwhile to write a test to check that the communication works properly; this won't catch race conditions and such, but it will catch some coding and design errors.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use tests to ensure that a class is thread safe, but you can use them to improve your confidence that you've taken care of specific problems. Is a rather complicated topic, though. The book "Unit Testing in Java" by Johannes Link has a nice introductory chapter on the topic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!