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 "lock_request_unit_test.h"
40 
41 namespace toku {
42 
43 // starting a lock request without immediate success should get
44 // stored in the lock request set as pending.
test_start_pending(void)45 void lock_request_unit_test::test_start_pending(void) {
46     int r;
47     locktree lt;
48     lock_request request;
49 
50     DICTIONARY_ID dict_id = { 1 };
51     lt.create(nullptr, dict_id, dbt_comparator);
52 
53     TXNID txnid_a = 1001;
54     TXNID txnid_b = 2001;
55 
56     const DBT *zero = get_dbt(0);
57     const DBT *one = get_dbt(1);
58     const DBT *two = get_dbt(2);
59 
60     // take a range lock using txnid b
61     r = lt.acquire_write_lock(txnid_b, zero, two, nullptr, false);
62     invariant_zero(r);
63 
64     lt_lock_request_info *info = lt.get_lock_request_info();
65 
66     // start a lock request for 1,1
67     // it should fail. the request should be stored and in the pending state.
68     request.create();
69     request.set(&lt, txnid_a, one, one, lock_request::type::WRITE, false);
70     r = request.start();
71     invariant(r == DB_LOCK_NOTGRANTED);
72     invariant(info->pending_lock_requests.size() == 1);
73     invariant(request.m_state == lock_request::state::PENDING);
74 
75     // should have made copies of the keys, and they should be equal
76     invariant(request.m_left_key_copy.flags == DB_DBT_MALLOC);
77     invariant(request.m_right_key_copy.flags == DB_DBT_MALLOC);
78     invariant(compare_dbts(nullptr, &request.m_left_key_copy, one) == 0);
79     invariant(compare_dbts(nullptr, &request.m_right_key_copy, one) == 0);
80 
81     // release the range lock for txnid b
82     locktree_unit_test::locktree_test_release_lock(&lt, txnid_b, zero, two);
83 
84     // now retry the lock requests.
85     // it should transition the request to successfully complete.
86     lock_request::retry_all_lock_requests(&lt);
87     invariant(info->pending_lock_requests.size() == 0);
88     invariant(request.m_state == lock_request::state::COMPLETE);
89     invariant(request.m_complete_r == 0);
90 
91     locktree_unit_test::locktree_test_release_lock(&lt, txnid_a, one, one);
92 
93     request.destroy();
94 
95     lt.release_reference();
96     lt.destroy();
97 }
98 
99 } /* namespace toku */
100 
main(void)101 int main(void) {
102     toku::lock_request_unit_test test;
103     test.test_start_pending();
104     return 0;
105 }
106 
107