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 "locktree_unit_test.h"
40 
41 #include <toku_time.h>
42 
43 __attribute__((__unused__))
current_time_usec(void)44 static long current_time_usec(void) {
45     struct timeval t;
46     gettimeofday(&t, NULL);
47     return t.tv_usec + t.tv_sec * 1000000;
48 }
49 
50 namespace toku {
51 
52 // test write lock conflicts when read or write locks exist
53 // test read lock conflicts when write locks exist
test_conflicts(void)54 void locktree_unit_test::test_conflicts(void) {
55     locktree lt;
56 
57     DICTIONARY_ID dict_id = { 1 };
58     lt.create(nullptr, dict_id, dbt_comparator);
59 
60     int r;
61     TXNID txnid_a = 1001;
62     TXNID txnid_b = 2001;
63     const DBT *zero = get_dbt(0);
64     const DBT *one = get_dbt(1);
65     const DBT *two = get_dbt(2);
66     const DBT *three = get_dbt(3);
67     const DBT *four = get_dbt(4);
68     const DBT *five = get_dbt(5);
69 
70     for (int test_run = 0; test_run < 2; test_run++) {
71         // test_run == 0 means test with read lock
72         // test_run == 1 means test with write lock
73 #define ACQUIRE_LOCK(txn, left, right, conflicts) \
74         test_run == 0 ? lt.acquire_read_lock(txn, left, right, conflicts, false) \
75             : lt.acquire_write_lock(txn, left, right, conflicts, false)
76 
77         // acquire some locks for txnid_a
78         r = ACQUIRE_LOCK(txnid_a, one, one, nullptr);
79         invariant(r == 0);
80         r = ACQUIRE_LOCK(txnid_a, three, four, nullptr);
81         invariant(r == 0);
82 
83 #undef ACQUIRE_LOCK
84 
85         for (int sub_test_run = 0; sub_test_run < 2; sub_test_run++) {
86         // sub_test_run == 0 means test read lock on top of write lock
87         // sub_test_run == 1 means test write lock on top of write lock
88         // if test_run == 0, then read locks exist. only test write locks.
89 #define ACQUIRE_LOCK(txn, left, right, conflicts) \
90         sub_test_run == 0 && test_run == 1 ? \
91             lt.acquire_read_lock(txn, left, right, conflicts, false) \
92           : lt.acquire_write_lock(txn, left, right, conflicts, false)
93             // try to get point write locks for txnid_b, should fail
94             r = ACQUIRE_LOCK(txnid_b, one, one, nullptr);
95             invariant(r == DB_LOCK_NOTGRANTED);
96             r = ACQUIRE_LOCK(txnid_b, three, three, nullptr);
97             invariant(r == DB_LOCK_NOTGRANTED);
98             r = ACQUIRE_LOCK(txnid_b, four, four, nullptr);
99             invariant(r == DB_LOCK_NOTGRANTED);
100 
101             // try to get some overlapping range write locks for txnid_b, should fail
102             r = ACQUIRE_LOCK(txnid_b, zero, two, nullptr);
103             invariant(r == DB_LOCK_NOTGRANTED);
104             r = ACQUIRE_LOCK(txnid_b, four, five, nullptr);
105             invariant(r == DB_LOCK_NOTGRANTED);
106             r = ACQUIRE_LOCK(txnid_b, two, three, nullptr);
107             invariant(r == DB_LOCK_NOTGRANTED);
108 #undef ACQUIRE_LOCK
109         }
110 
111         lt.remove_overlapping_locks_for_txnid(txnid_a, one, one);
112         lt.remove_overlapping_locks_for_txnid(txnid_a, three, four);
113         invariant(no_row_locks(&lt));
114     }
115 
116     lt.release_reference();
117     lt.destroy();
118 }
119 
120 } /* namespace toku */
121 
main(void)122 int main(void) {
123     toku::locktree_unit_test test;
124     test.test_conflicts();
125     return 0;
126 }
127