How to write tests that test race conditions?

I am currently working on a large project that has recently made a lot of changes, primarily adding thread support.

When looking at the code, I identified sections that could potentially trigger race conditions, if not now, then in the future. To prevent such a regression, I would like to write a test that can reliably detect the state of the race in this particular region to ensure that no future will lead to this error.
The code is not clogged with sleep () statements, but it is a potential dead end and a racing minefield, and I want to ensure reliability.

This project is written entirely in C. So, is there anyway for me the ability to write unit tests to prevent race conditions?

+6
source share
3 answers

Race conditions are an integral part of non-determinism. If you cannot guarantee that the calling sequence is secure, enter some run-time checks that check protocol invariants. Then, at least, you will have evidence of an error when they occur.

Although this does not solve your problem, it at least gives you a tool to quantify the extent of the problem.

If any race is triggered from events outside the application area, then any static analysis would require that it also be modeled in order to be able to detect conditions.

+2
source

The Valgrind DRD tool can be used to detect many flow errors. Just use this tool and run regular test cases.

0
source

Clang Thread Sanitizer works by monitoring the testing process as it progresses. Whenever a stream reads or writes to unprotected memory, it writes to it, and it will tell you whether access to any part of the memory was unprotected by more than one stream.

0
source

Source: https://habr.com/ru/post/953725/


All Articles