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 // test the kill callback.  the lock wait is killed 1/2 of the way through the wait.
40 
41 #include "lock_request_unit_test.h"
42 
43 namespace toku {
44 
45 const uint64_t my_lock_wait_time = 10 * 1000; // 10 seconds
46 const uint64_t my_killed_time = 1 * 1000;
47 
48 static int killed_calls = 0;
49 static uint64_t t_last_kill;
50 static uint64_t t_do_kill;
51 
my_killed_callback(void)52 static int my_killed_callback(void) {
53     uint64_t t_now = toku_current_time_microsec();
54     if (t_now == t_last_kill)
55         return 0;
56     assert(t_now >= t_last_kill);
57     t_last_kill = t_now;
58     killed_calls++;
59     if (t_now >= t_do_kill)
60         return 1;
61     else
62         return 0;
63 }
64 
65 // make sure deadlocks are detected when a lock request starts
test_wait_time_callback(void)66 void lock_request_unit_test::test_wait_time_callback(void) {
67     int r;
68     locktree lt;
69 
70     DICTIONARY_ID dict_id = { 1 };
71     lt.create(nullptr, dict_id, dbt_comparator);
72 
73     TXNID txnid_a = 1001;
74     lock_request request_a;
75     request_a.create();
76 
77     TXNID txnid_b = 2001;
78     lock_request request_b;
79     request_b.create();
80 
81     const DBT *one = get_dbt(1);
82 
83     // a locks 'one'
84     request_a.set(&lt, txnid_a, one, one, lock_request::type::WRITE, false);
85     r = request_a.start();
86     assert_zero(r);
87 
88     // b tries to lock 'one'
89     request_b.set(&lt, txnid_b, one, one, lock_request::type::WRITE, false);
90     r = request_b.start();
91     assert(r == DB_LOCK_NOTGRANTED);
92 
93     uint64_t t_start = toku_current_time_microsec();
94     t_last_kill = t_start;
95     t_do_kill = t_start + my_lock_wait_time * 1000 / 2;
96     r = request_b.wait(my_lock_wait_time, my_killed_time, my_killed_callback);
97     assert(r == DB_LOCK_NOTGRANTED);
98 
99     uint64_t t_end = toku_current_time_microsec();
100     assert(t_end > t_start);
101     uint64_t t_delta = t_end - t_start;
102     // fprintf(stderr, "delta=%" PRIu64 "\n", t_delta);
103     assert(t_delta >= my_lock_wait_time / 2);
104 
105     // fprintf(stderr, "killed_calls=%d\n", killed_calls);
106     assert(killed_calls > 0);
107 
108     request_b.destroy();
109 
110     release_lock_and_retry_requests(&lt, txnid_a, one, one);
111     request_a.destroy();
112 
113     lt.release_reference();
114     lt.destroy();
115 }
116 
117 } /* namespace toku */
118 
main(void)119 int main(void) {
120     toku::lock_request_unit_test test;
121     test.test_wait_time_callback();
122     return 0;
123 }
124 
125