Based on the answers to this question , I am pleased with the simplicity and ease of use of the following two synchronization methods:
func synchronized(lock: AnyObject, closure: () -> Void) { objc_sync_enter(lock) closure() objc_sync_exit(lock) } func synchronized<T>(lock: AnyObject, closure: () -> T) -> T { objc_sync_enter(lock) defer { objc_sync_exit(lock) } return closure() }
But to be sure that they really do what I want, I want to wrap them in heaps of unit tests. How can I write unit tests that will effectively test these methods (and show that they actually synchronize the code)?
Ideally, I would like these unit tests to be as simple and clear as possible. Presumably, this test should be a code that, if executed outside the synchronization block, will give one set of results, but will give a completely separate set of results inside these synchronized blocks.
source share