diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-21 11:54:28 +0000 |
commit | e6918187568dbd01842d8d1d2c808ce16a894239 (patch) | |
tree | 64f88b554b444a49f656b6c656111a145cbbaa28 /src/test/common/test_mutex_debug.cc | |
parent | Initial commit. (diff) | |
download | ceph-e6918187568dbd01842d8d1d2c808ce16a894239.tar.xz ceph-e6918187568dbd01842d8d1d2c808ce16a894239.zip |
Adding upstream version 18.2.2.upstream/18.2.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/common/test_mutex_debug.cc')
-rw-r--r-- | src/test/common/test_mutex_debug.cc | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/test/common/test_mutex_debug.cc b/src/test/common/test_mutex_debug.cc new file mode 100644 index 000000000..977dfe738 --- /dev/null +++ b/src/test/common/test_mutex_debug.cc @@ -0,0 +1,101 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 &smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2011 New Dream Network + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License version 2, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include <future> +#include <mutex> +#include <thread> + +#include "common/mutex_debug.h" + +#include "gtest/gtest.h" + + +template<typename Mutex> +static bool test_try_lock(Mutex* m) { + if (!m->try_lock()) + return false; + m->unlock(); + return true; +} + +template<typename Mutex> +static void test_lock() { + Mutex m("mutex"); + auto ttl = &test_try_lock<Mutex>; + + m.lock(); + ASSERT_TRUE(m.is_locked()); + auto f1 = std::async(std::launch::async, ttl, &m); + ASSERT_FALSE(f1.get()); + + ASSERT_TRUE(m.is_locked()); + ASSERT_TRUE(!!m); + + m.unlock(); + ASSERT_FALSE(m.is_locked()); + ASSERT_FALSE(!!m); + + auto f3 = std::async(std::launch::async, ttl, &m); + ASSERT_TRUE(f3.get()); + + ASSERT_FALSE(m.is_locked()); + ASSERT_FALSE(!!m); +} + +TEST(MutexDebug, Lock) { + test_lock<ceph::mutex_debug>(); +} + +TEST(MutexDebug, NotRecursive) { + ceph::mutex_debug m("foo"); + auto ttl = &test_try_lock<mutex_debug>; + + ASSERT_NO_THROW(m.lock()); + ASSERT_TRUE(m.is_locked()); + ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get()); + + ASSERT_THROW(m.lock(), std::system_error); + ASSERT_TRUE(m.is_locked()); + ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get()); + + ASSERT_NO_THROW(m.unlock()); + ASSERT_FALSE(m.is_locked()); + ASSERT_TRUE(std::async(std::launch::async, ttl, &m).get()); +} + +TEST(MutexRecursiveDebug, Lock) { + test_lock<ceph::mutex_recursive_debug>(); +} + + +TEST(MutexRecursiveDebug, Recursive) { + ceph::mutex_recursive_debug m("m"); + auto ttl = &test_try_lock<mutex_recursive_debug>; + + ASSERT_NO_THROW(m.lock()); + ASSERT_TRUE(m.is_locked()); + ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get()); + + ASSERT_NO_THROW(m.lock()); + ASSERT_TRUE(m.is_locked()); + ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get()); + + ASSERT_NO_THROW(m.unlock()); + ASSERT_TRUE(m.is_locked()); + ASSERT_FALSE(std::async(std::launch::async, ttl, &m).get()); + + ASSERT_NO_THROW(m.unlock()); + ASSERT_FALSE(m.is_locked()); + ASSERT_TRUE(std::async(std::launch::async, ttl, &m).get()); +} |