]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/common/test_mutex.cc
patches: remove fuzz and re-format
[ceph.git] / 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 static CephContext* cct;
28
29 static void do_init() {
30 if (cct == nullptr) {
31 cct = new CephContext(0);
32 lockdep_register_ceph_context(cct);
33 }
34 }
35
36 static void disable_lockdep() {
37 if (cct) {
38 lockdep_unregister_ceph_context(cct);
39 cct->put();
40 cct = nullptr;
41 }
42 }
43
44 TEST(Mutex, NormalAsserts) {
45 Mutex* m = new Mutex("Normal",false);
46 m->Lock();
47 EXPECT_THROW(m->Lock(), int);
48 }
49
50 TEST(Mutex, RecursiveWithLockdep) {
51 do_init();
52 Mutex* m = new Mutex("Recursive1",true);
53 m->Lock();
54 m->Lock();
55 m->Unlock();
56 m->Unlock();
57 delete m;
58 }
59
60 TEST(Mutex, RecursiveWithoutLockdep) {
61 disable_lockdep();
62 Mutex* m = new Mutex("Recursive2",true);
63 m->Lock();
64 m->Lock();
65 m->Unlock();
66 m->Unlock();
67 delete m;
68 }
69
70 TEST(Mutex, DeleteLocked) {
71 Mutex* m = new Mutex("Recursive3",false);
72 m->Lock();
73 PrCtl unset_dumpable;
74 EXPECT_DEATH(delete m,".*");
75 }