#include #include #include "../RwMutex.h" #include "../MutexException.h" BOOST_AUTO_TEST_SUITE( RwMutex ); using namespace CCDom; //! Gets a timeout 1s in the future boost::xtime getTimeout() { boost::xtime timeout; BOOST_CHECK( 0 != boost::xtime_get(&timeout, boost::TIME_UTC) ); timeout.sec += 1; return timeout; } BOOST_AUTO_TEST_CASE( test_single_lock ) { // Create a read-write mutex and obtain a read-lock RwMutex mutex; RwMutex::Lock lock(mutex, getTimeout()); // Should be able to promote to a write lock if only one reader { RwMutex::WriteLock writeLock(lock, getTimeout()); } // Obtain a second lock and should no longer be able to obtain a write // lock on either read locks { RwMutex::Lock lock2(mutex, getTimeout()); BOOST_CHECK_THROW(RwMutex::WriteLock(lock, getTimeout()), MutexException); BOOST_CHECK_THROW(RwMutex::WriteLock(lock2, getTimeout()), MutexException); } // Once the second lock bas been released, should again be able to obtain a write lock { RwMutex::WriteLock writeLock(lock, getTimeout()); // After obtaining the write lock, shouldn't be able to obtain another read lock BOOST_CHECK_THROW(RwMutex::Lock(mutex, getTimeout()), MutexException); } } BOOST_AUTO_TEST_SUITE_END();