From: Adam Kupczyk Date: Wed, 29 Jun 2016 14:28:45 +0000 (+0200) Subject: common: Added tests for mutex X-Git-Tag: v12.0.1~3^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=fa60e388ddd83e2c0553f00f769ebb3d2a6c238c;p=ceph.git common: Added tests for mutex Signed-off-by: Adam Kupczyk --- diff --git a/src/test/common/test_mutex.cc b/src/test/common/test_mutex.cc new file mode 100644 index 000000000000..38e2739cb821 --- /dev/null +++ b/src/test/common/test_mutex.cc @@ -0,0 +1,66 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 &smarttab +/* + * Ceph - scalable distributed file system + * + */ + +#include +#include "gtest/gtest.h" +#include "common/ceph_context.h" +#include "common/config.h" + +/* + * Override normal ceph assert. + * It is needed to prevent hang when we assert() and THEN still wait on lock(). + */ +namespace ceph +{ + void __ceph_assert_fail(const char *assertion, const char *file, int line, + const char *func) + { + throw 0; + } +} + +void do_init() { + static CephContext* cct = nullptr; + if (cct == nullptr) { + cct = new CephContext(0); + lockdep_register_ceph_context(cct); + } +} + +TEST(MutexDebug, NormalAsserts) { + Mutex* m = new Mutex("Normal",false); + m->Lock(); + EXPECT_THROW(m->Lock(), int); +} + +TEST(MutexDebug, RecursiveWithLockdep) { + do_init(); + g_lockdep = 1; + Mutex* m = new Mutex("Recursive1",true); + m->Lock(); + m->Lock(); + m->Unlock(); + m->Unlock(); + delete m; +} + +TEST(MutexDebug, RecursiveWithoutLockdep) { + do_init(); + g_lockdep = 0; + Mutex* m = new Mutex("Recursive2",true); + m->Lock(); + m->Lock(); + m->Unlock(); + m->Unlock(); + delete m; +} + +TEST(MutexDebug, DeleteLocked) { + Mutex* m = new Mutex("Recursive3",false); + m->Lock(); + EXPECT_DEATH(delete m,".*"); +}