1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6
7
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22 ----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39 #include "manager_unit_test.h"
40 #include "locktree_unit_test.h"
41 #include "lock_request_unit_test.h"
42
43 namespace toku {
44
assert_status(LTM_STATUS ltm_status,const char * keyname,uint64_t v)45 static void assert_status(LTM_STATUS ltm_status, const char *keyname, uint64_t v) {
46 TOKU_ENGINE_STATUS_ROW key_status = NULL;
47 // lookup keyname in status
48 for (int i = 0; ; i++) {
49 TOKU_ENGINE_STATUS_ROW status = <m_status->status[i];
50 if (status->keyname == NULL)
51 break;
52 if (strcmp(status->keyname, keyname) == 0) {
53 key_status = status;
54 break;
55 }
56 }
57 assert(key_status);
58 assert(key_status->value.num == v);
59 }
60
test_status(void)61 void manager_unit_test::test_status(void) {
62 locktree_manager mgr;
63 mgr.create(nullptr, nullptr, nullptr, nullptr);
64
65 LTM_STATUS_S status;
66 mgr.get_status(&status);
67 assert_status(&status, "LTM_WAIT_COUNT", 0);
68 assert_status(&status, "LTM_TIMEOUT_COUNT", 0);
69
70 DICTIONARY_ID dict_id = { .dictid = 1 };
71 locktree *lt = mgr.get_lt(dict_id, dbt_comparator, nullptr);
72 int r;
73 TXNID txnid_a = 1001;
74 TXNID txnid_b = 2001;
75 const DBT *one = get_dbt(1);
76
77 // txn a write locks one
78 r = lt->acquire_write_lock(txnid_a, one, one, nullptr, false);
79 assert(r == 0);
80
81 // txn b tries to write lock one, conflicts, waits, and fails to lock one
82 lock_request request_b;
83 request_b.create();
84 request_b.set(lt, txnid_b, one, one, lock_request::type::WRITE, false);
85 r = request_b.start();
86 assert(r == DB_LOCK_NOTGRANTED);
87 r = request_b.wait(1000);
88 assert(r == DB_LOCK_NOTGRANTED);
89 request_b.destroy();
90
91 range_buffer buffer;
92 buffer.create();
93 buffer.append(one, one);
94 lt->release_locks(txnid_a, &buffer);
95 buffer.destroy();
96
97 assert(lt->m_rangetree->is_empty() && lt->m_sto_buffer.is_empty());
98
99 // assert that wait counters incremented
100 mgr.get_status(&status);
101 assert_status(&status, "LTM_WAIT_COUNT", 1);
102 assert_status(&status, "LTM_TIMEOUT_COUNT", 1);
103
104 // assert that wait counters are persistent after the lock tree is destroyed
105 mgr.release_lt(lt);
106 mgr.get_status(&status);
107 assert_status(&status, "LTM_WAIT_COUNT", 1);
108 assert_status(&status, "LTM_TIMEOUT_COUNT", 1);
109
110 mgr.destroy();
111 }
112
113 } /* namespace toku */
114
main(void)115 int main(void) {
116 toku::manager_unit_test test;
117 test.test_status();
118 return 0;
119 }
120