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 "test.h"
40 #include <stdio.h>
41 
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <memory.h>
45 #include <sys/stat.h>
46 #include <db.h>
47 
48 static void
test_txn_abort(int n)49 test_txn_abort (int n) {
50     if (verbose) printf("test_txn_abort:%d\n", n);
51 
52     int r;
53     toku_os_recursive_delete(TOKU_TEST_FILENAME);
54     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
55 
56     DB_ENV *env;
57     r = db_env_create(&env, 0); assert(r == 0);
58     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL + DB_INIT_LOG + DB_INIT_LOCK + DB_INIT_TXN + DB_PRIVATE + DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
59     if (r != 0) printf("%s:%d:%d:%s\n", __FILE__, __LINE__, r, db_strerror(r));
60     assert(r == 0);
61 
62     DB_TXN *txn = 0;
63     r = env->txn_begin(env, 0, &txn, 0); assert(r == 0);
64 
65     DB *db;
66     r = db_create(&db, env, 0); assert(r == 0);
67     r = db->open(db, txn, "test.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); assert(r == 0);
68     r = txn->commit(txn, 0); assert(r == 0);
69 
70     r = env->txn_begin(env, 0, &txn, 0); assert(r == 0);
71     int i;
72     for (i=0; i<n; i++) {
73         DBT key, val;
74         r = db->put(db, txn, dbt_init(&key, &i, sizeof i), dbt_init(&val, &i, sizeof i), 0);
75         if (r != 0) printf("%s:%d:%d:%s\n", __FILE__, __LINE__, r, db_strerror(r));
76         assert(r == 0);
77     }
78     r = txn->abort(txn);
79 #if 0
80     assert(r == 0);
81 #else
82     if (r != 0) printf("%s:%d:abort:%d\n", __FILE__, __LINE__, r);
83 #endif
84     /* walk the db, should be empty */
85     r = env->txn_begin(env, 0, &txn, 0); assert(r == 0);
86     DBC *cursor;
87     r = db->cursor(db, txn, &cursor, 0); assert(r == 0);
88     DBT key; memset(&key, 0, sizeof key);
89     DBT val; memset(&val, 0, sizeof val);
90     r = cursor->c_get(cursor, &key, &val, DB_FIRST);
91     assert(r == DB_NOTFOUND);
92     r = cursor->c_close(cursor); assert(r == 0);
93     r = txn->commit(txn, 0);
94 
95     r = db->close(db, 0); assert(r == 0);
96     r = env->close(env, 0); assert(r == 0);
97 }
98 
99 int
test_main(int argc,char * const argv[])100 test_main(int argc, char *const argv[]) {
101     int i;
102     for (i = 1; i < argc; i++) {
103         const char *arg = argv[i];
104         if (strcmp(arg, "-v") == 0 || strcmp(arg, "--verbose") == 0) {
105             verbose++;
106             continue;
107         }
108     }
109     for (i=1; i<100; i++)
110         test_txn_abort(i);
111     return 0;
112 }
113