4

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.

2
  • 4
    you are going to write a cross-platform concurrency primitives library in a weekend. Impressive! Commented Jan 10, 2010 at 2:48
  • Haha it's the goal! There will always be another weekend that needs filling if I can't make it though. Constant learning is the name of the game. Commented Jan 10, 2010 at 2:57

1 Answer 1

6

Don't think about unit tests, think about the behaviour you want to specify. For example:

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 

I think that will help you identify what to do. And yes probably you need a helper thread in your unit tests to make it happen. Maybe this example is helpful.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I test that I "can't" get a lock, as it would just spin-lock until I can.
Please look at the link I provided in the answer, specifically the class Given_a_locked_MutexWrapper

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.