Commit 9de48090 authored by Mukund Sivaraman's avatar Mukund Sivaraman
Browse files

[2198] Test the non-blocking variant of Mutex::Locker

parent 11df86c5
......@@ -38,6 +38,45 @@ TEST(MutexTest, lockMultiple) {
Mutex::Locker l2(mutex); // Attempt to lock again.
}, isc::InvalidOperation);
EXPECT_TRUE(mutex.locked()); // Debug-only build
// block=true explicitly.
Mutex mutex2;
EXPECT_FALSE(mutex2.locked()); // Debug-only build
Mutex::Locker l12(mutex2, true);
EXPECT_TRUE(mutex2.locked()); // Debug-only build
testThread(Mutex* mutex)
// This should not block indefinitely, but throw AlreadyLocked.
// block=false (tryLock).
Mutex::Locker l3(*mutex, false);
}, Mutex::Locker::AlreadyLocked);
EXPECT_TRUE(mutex->locked()); // Debug-only build
return NULL;
// Test the non-blocking variant using a second thread.
TEST(MutexTest, lockNonBlocking) {
// block=false (tryLock).
Mutex mutex;
Mutex::Locker l1(mutex, false);
EXPECT_TRUE(mutex.locked()); // Debug-only build
// First, try another locker from the same thread.
Mutex::Locker l2(mutex, false);
}, Mutex::Locker::AlreadyLocked);
EXPECT_TRUE(mutex.locked()); // Debug-only build
// Now try another locker from a different thread.
Thread thread(boost::bind(&testThread, &mutex));
// Destroying a locked mutex is a bad idea as well
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment