#47 Creating/Deleting mocked objects not thread-safe

You will run into issues if you create or delete mocked objects simultaneously. The reason is that every class created using MOCK_CLASS indirectly derives from a class that is called “verifiable” and all instances of these “verifiable” class will be stored in a vector inside of Turtle-Mock upon creation and removed from this vector upon deletion. This access to the vector is not synchronized and will cause crashes if you don’t care.

I propose to add synchronization mechanisms to Turtle to avoid such crashes. Synchronization mechanisms might get injected via extension points to avoid creating a dependency to a threading library. If Turtle Mock relies on C++11 anyway std::mutex might be used.

One thing I noticed: The mock::detail::functor default constructor uses a function-scope static mutex for synchronization. This is safe on all compilers implementing the C++11 feature N2660 ('magic statics').

Unfortunately, this feature is not available on any Visual C++ version (except the very latest VC12 November 2013 CTP, which does not come with a 'Go Live' license). It would be great if you could change the implementation to make it thread-safe on those compilers as well.

What is a bit annoying is that you will now get a new global instance of the functor_mutex reference in every .cpp file that pulls that header in (via the expanded BOOST_TEST_SINGLETON_INST macro), but I don't see a way to avoid this in a header-only library.

You will run into issues if you create or delete mocked objects simultaneously. The reason is that every class created using MOCK_CLASS indirectly derives from a class that is called “verifiable” and all instances of these “verifiable” class will be stored in a vector inside of Turtle-Mock upon creation and removed from this vector upon deletion. This access to the vector is not synchronized and will cause crashes if you don’t care.

MOCK_EXPECT((*dist_access.at(i)).getObject_mock)
.with(object_id, mock::any)
.calls(
{
output = createMockedObjectPtr(object_id);
return true;
});
This expectation is being called from multiple tests concurrently. This led to crashes from time to time.

And now it looks like this (where the MOCK-Object is created outside of the expectation as part of the test setup):