Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / common / test_mutex.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  */
7
8 #include <common/Mutex.h>
9 #include "gtest/gtest.h"
10 #include "common/ceph_context.h"
11 #include "common/config.h"
12 #include "include/coredumpctl.h"
13
14 /*
15  * Override normal ceph assert.
16  * It is needed to prevent hang when we assert() and THEN still wait on lock().
17  */
18 namespace ceph
19 {
20   void __ceph_assert_fail(const char *assertion, const char *file, int line,
21         const char *func)
22   {
23     throw 0;
24   }
25 }
26
27 void do_init() {
28   static CephContext* cct = nullptr;
29   if (cct == nullptr) {
30     cct = new CephContext(0);
31     lockdep_register_ceph_context(cct);
32   }
33 }
34
35 TEST(Mutex, NormalAsserts) {
36   Mutex* m = new Mutex("Normal",false);
37   m->Lock();
38   EXPECT_THROW(m->Lock(), int);
39 }
40
41 TEST(Mutex, RecursiveWithLockdep) {
42   do_init();
43   g_lockdep = 1;
44   Mutex* m = new Mutex("Recursive1",true);
45   m->Lock();
46   m->Lock();
47   m->Unlock();
48   m->Unlock();
49   delete m;
50 }
51
52 TEST(Mutex, RecursiveWithoutLockdep) {
53   do_init();
54   g_lockdep = 0;
55   Mutex* m = new Mutex("Recursive2",true);
56   m->Lock();
57   m->Lock();
58   m->Unlock();
59   m->Unlock();
60   delete m;
61 }
62
63 TEST(Mutex, DeleteLocked) {
64   Mutex* m = new Mutex("Recursive3",false);
65   m->Lock();
66   PrCtl unset_dumpable;
67   EXPECT_DEATH(delete m,".*");
68 }