• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

testing of singleton for thread safety

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic