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?
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?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
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.
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.
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
Don't get me started about those stupid light bulbs.