Unit testing parallel code

My weekend project consists of writing a cross-platform library of concurrency primitives (critical sections, read / write mutexes, locking chains, events, etc.) and wondered how unit test this stuff. I understand that testing parallel code in itself is difficult, but testing the primitives of the specified code cannot be so tough, right?

It turns out it's so complicated. At least for me it is.

So how would you approach this? As an example, I don’t even know where to start testing critical sections.

+4
c ++ concurrency unit-testing
source share
1 answer

Do not think about unit tests, think about the behavior you want to indicate. For example:

Given_an_unlocked_lock It_should_be_possible_to_take_it Given_a_locked_locked It_should_not_be_possible_to_take_it_from_another_thread It_should_be_possible_take_it_from_the_same_thread Given_a_locked_locked_when_unlocked It_should_be_possible_to_take_it Given_a_locked_locked_when_owning_thread_terminates It_should_be_possible_to_take_it 

I think this will help you determine what to do. And yes, maybe you need an auxiliary thread in your unit tests for this to happen. Perhaps this example is useful.

+6
source share

All Articles