Search code examples
objective-cunit-testingxcode4osx-lion

Testing multi-threading


I'm trying to make sure that my implementation of ObjC multithreading is proper when I transfer the application from single-thread to multiple.

Right now I have unit tests set up to make sure everything works properly in the single-thread environment.

What can I do to make sure this holds in multi-threads? I'd like to continue to use unit-testing but not sure how to go about testing multithreading in unit tests.

Clarification: I am implementing the multi-threading using NSBlockOperation / NSOperationQueue.


Solution

  • In short, you can't. At least, you can't get anywhere near 100% testing coverage. With threads, everything from simultaneous I/O to core count to memory pressure to activity in other processes (or same process) will impact thread scheduling.

    For unit tests, you typically want the unit test to block until the threaded subsystem does whatever it needs to do. Often the unit testing thread will asynchronously spawn whatever work needs to be done, then block until a signal occurs (the thread kind of signal, not signal()). There are many different ways to do this, obviously, and much of the details will be specific to your app; need run loops? using GCD (hopefully)? etc....