name_masked
name_masked

Reputation: 9794

How to create a test environment for a multi-threaded application

All,

Recently I developed a code that supposedly is a thread-safe class. Now the reason I have said 'supposedly' is because even after using the sync'ed blocks, immutable data structures and concurrent classes, I was not able to test the code for some cases because of the thread scheduling environment of JVM. i.e. I only had test cases on paper but could not replicate the same test environment. Is there any specific guidelines or something the experienced members over here who can share about how to test a multi-threaded environment.

Upvotes: 6

Views: 586

Answers (4)

soru
soru

Reputation: 5526

If you have code that you plan to test in order to make it reliable, then make it single threaded.

Threading should be reserved for code that either doesn't particularly need to work, or is simple enough to be statically analysed and proven correct without testing.

Upvotes: 0

Jayan
Jayan

Reputation: 18459

We use GroboUtils to create multi threaded tests.

Upvotes: 1

EKI
EKI

Reputation: 856

Also, another thing I would recomend is for you to use code coverage measuring tools and set a high standar as your goal. For example, set a high goal for modified condition/decision coverage.

Upvotes: 1

Péter Török
Péter Török

Reputation: 116256

First thing is, you can't ensure only with testing that your class is fully thread-safe. Whatever tests you run on it, you still need to have your code reviewed by as many experienced eyes as you can get, to detect subtle concurrency issues.

That said, you can devise specific test scenarios to try to cover all possible inter-thread timing scenarios, as you did. For ideas on this (and for designing thread-safe classes in general), it is recommended to read Java Concurrency in Practice.

Moreover, you can run stress tests, executing many threads simultaneously over an extended period of time. The number of threads should be way over the reasonable limit to make sure that thread contention happens often - this raises the chances of potential concurrency bugs to manifest over time.

Upvotes: 10

Related Questions