user970742
user970742

Reputation: 419

How to test multithreaded windows service with timers?

I have a subject. How can I test it using UnitTests?

Thanks in advance!

Upvotes: 0

Views: 160

Answers (1)

ChrisWue
ChrisWue

Reputation: 19020

In general terms: Reliable unit testing of multi threaded code is extremely hard. The reason for that is that you have no control over when the threads are being run.

A simple solution is to initiate the requests (or whatever it is your threads process) and then Thread.Sleep() for a while in your unit test and hope that your threads got scheduled to do the work. Then check if everything got processed correctly. This mostly works but will occasionally fail due to scheduling and timing - especially when you run your unit tests in a VM. You could also set some events or states in your threads to indicate that they have passed certain stages but it can become a bit ugly.

However if you can be more specific what exactly you are doing then there might be some workaround which works better.

Upvotes: 1

Related Questions