Unitized thread-based code testing? Coercion to race conditions

When using my application, I came across a race condition in some code that uses NSOperationQueue to run tasks asynchronously after events triggered by the user. I know how to fix the race condition, since this is a stupid design error that I will not delve into, but I would like to prove a mistake with a test case (so that it does not return during line optimization / refactoring). It guarded me. How can you test something multithreaded, especially if the goal of the test is to generate a race condition?

Does anyone have any links to reference material that I can reference when it comes to working with threads and unit testing? I am particularly interested in the generation of the race.

+8
multithreading objective-c unit-testing cocoa
source share
1 answer

You must ensure that the sequence of events causing the race condition actually occurs during testing. To do this, you need to influence the rotation of the threads within the test case.

You can achieve this with additional (conditional) synchronization or (simpler and less reliable) additional timers. Put sleep () calls in critical sections to make sure they work long enough for another thread to become undesirable. When this works for you, replace the sleep calls with explicit synchronization (i.e. Block one thread until the other confirms that it has arrived).

Then make all this conditional for the global flag specified by the test case.

+4
source share

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


All Articles