]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common: Added tests for mutex
authorAdam Kupczyk <akupczyk@mirantis.com>
Wed, 29 Jun 2016 14:28:45 +0000 (16:28 +0200)
committerAdam Kupczyk <akupczyk@mirantis.com>
Mon, 13 Mar 2017 16:39:29 +0000 (17:39 +0100)
Signed-off-by: Adam Kupczyk <akupczyk@mirantis.com>
src/test/common/test_mutex.cc [new file with mode: 0644]

diff --git a/src/test/common/test_mutex.cc b/src/test/common/test_mutex.cc
new file mode 100644 (file)
index 0000000..38e2739
--- /dev/null
@@ -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 <common/Mutex.h>
+#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,".*");
+}