Unit Testing Concurrent Code

Cla*_*bel 4 c++ concurrency unit-testing

My weekend project consists of writing a cross-platform concurrency primitives library (critical sections, read/write mutexes, interlocked integers, events, etc) and was wondering how to unit test this stuff. I realize that testing concurrent code is hard in itself, but testing the primitives of said code couldn't be that hard, could it?

Turns out, it is that hard. At least, for me it is.

So how would you go about approaching this? Just as an example, I don't even know where to start with testing critical sections.

Cel*_*ish 6

不要考虑单元测试,考虑要指定的行为.例如:

Given_an_unlocked_lock
    It_should_be_possible_to_take_it
Given_a_locked_lock
    It_should_not_be_possible_to_take_it_from_another_thread
    It_should_be_possible_take_it_from_the_same_thread
Given_a_locked_lock_when_unlocked
    It_should_be_possible_to_take_it
Given_a_locked_lock_when_owning_thread_terminates
    It_should_be_possible_to_take_it
Run Code Online (Sandbox Code Playgroud)

我认为这将有助于您确定要做什么.是的,你可能需要在单元测试中使用辅助线程来实现它.也许这个例子很有帮助.