Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prove that multithreading is working?

How can I prove that multithreading is working in my C# programs? This is for a testing requirement. For example, I'm going to have to add some locking to a logger class (yes I know, I shouldn't have written my own logging class), and I need a test case to prove that the change will work.

like image 762
C. Ross Avatar asked Sep 06 '25 12:09

C. Ross


1 Answers

If you want to test that your locking code is correctly synchronizing access to your log(s), you need to construct tests that guarantee contention. This may require you to refactor your code so that you can inject a mock log writer class that can hold the log's lock for arbitrary periods of time.

This is a broad topic, and you can find several related questions on StackOverflow, which are all worth reading:

  • How do I perform a Unit Test using threads?
  • How to write an automated test for thread safety
  • What are some strategies to unit test a scheduler?
  • Unit testing a multithreaded application?
  • Should I unit test for multithreading problems before writing any lock? (.NET C# TDD)

CHESS is a framework under development for identifying "assertion violations, deadlocks, livelocks, data-races, and memory-model errors." I haven't actually used this, but it looks like it might be extremely helpful.

like image 157
Jeff Sternal Avatar answered Sep 09 '25 08:09

Jeff Sternal