If you wrote the Lock statement (something similar to the System.Threading.Lock statement), I can understand why you want to test it.
In this case, you need to have the _list class that you have implemented the .Add method for this class, it will be much easier if you use dependency injection to insert IList to set _list. You will need to use a dummy instance of IList that implements the .Add () method.
If you have dummy.Add () cause hibernation for a given period of time (for example, 5 seconds), you can test by running a thread to call the .AddItem () method, this thread will block .Add () through the .AddItem ( ), the main thread can then wait 3 seconds before calling the .AddItem method.
If the lock works, the second thread is delayed for 2 seconds before executing .Add, if the lock does not work, it will immediately ring.
This is random and non-deterministic, so if you run enough time (millions), you will get a false result.
joocer
source share