]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-no-lock-during-traverse.c
tdb: add Bob Jenkins lookup3 hash as helper hash.
[ccan] / ccan / tdb / test / run-no-lock-during-traverse.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 #include "lock-tracking.h"
4
5 #define fcntl fcntl_with_lockcheck
6
7 #include <ccan/tdb/tdb.h>
8 #include <ccan/tdb/io.c>
9 #include <ccan/tdb/tdb.c>
10 #include <ccan/tdb/lock.c>
11 #include <ccan/tdb/freelist.c>
12 #include <ccan/tdb/traverse.c>
13 #include <ccan/tdb/transaction.c>
14 #include <ccan/tdb/error.c>
15 #include <ccan/tdb/open.c>
16 #include <ccan/tdb/check.c>
17 #include <ccan/tdb/hash.c>
18 #include <ccan/tap/tap.h>
19 #include <stdlib.h>
20 #include <err.h>
21 #include "logging.h"
22
23 #undef fcntl
24
25 #define NUM_ENTRIES 10
26
27 static bool prepare_entries(struct tdb_context *tdb)
28 {
29         unsigned int i;
30         TDB_DATA key, data;
31         
32         for (i = 0; i < NUM_ENTRIES; i++) {
33                 key.dsize = sizeof(i);
34                 key.dptr = (void *)&i;
35                 data.dsize = strlen("world");
36                 data.dptr = (void *)"world";
37
38                 if (tdb_store(tdb, key, data, 0) != 0)
39                         return false;
40         }
41         return true;
42 }
43
44 static void delete_entries(struct tdb_context *tdb)
45 {
46         unsigned int i;
47         TDB_DATA key;
48
49         for (i = 0; i < NUM_ENTRIES; i++) {
50                 key.dsize = sizeof(i);
51                 key.dptr = (void *)&i;
52
53                 ok1(tdb_delete(tdb, key) == 0);
54         }
55 }
56
57 /* We don't know how many times this will run. */
58 static int delete_other(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
59                         void *private_data)
60 {
61         unsigned int i;
62         memcpy(&i, key.dptr, 4);
63         i = (i + 1) % NUM_ENTRIES;
64         key.dptr = (void *)&i;
65         if (tdb_delete(tdb, key) != 0)
66                 (*(int *)private_data)++;
67         return 0;
68 }
69
70 static int delete_self(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
71                         void *private_data)
72 {
73         ok1(tdb_delete(tdb, key) == 0);
74         return 0;
75 }
76
77 int main(int argc, char *argv[])
78 {
79         struct tdb_context *tdb;
80         int errors = 0;
81
82         plan_tests(41);
83         tdb = tdb_open_ex("run-no-lock-during-traverse.tdb",
84                           1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR,
85                           0600, &taplogctx, NULL);
86
87         ok1(tdb);
88         ok1(prepare_entries(tdb));
89         ok1(locking_errors == 0);
90         ok1(tdb_lockall(tdb) == 0);
91         ok1(locking_errors == 0);
92         tdb_traverse(tdb, delete_other, &errors);
93         ok1(errors == 0);
94         ok1(locking_errors == 0);
95         ok1(tdb_unlockall(tdb) == 0);
96
97         ok1(prepare_entries(tdb));
98         ok1(locking_errors == 0);
99         ok1(tdb_lockall(tdb) == 0);
100         ok1(locking_errors == 0);
101         tdb_traverse(tdb, delete_self, NULL);
102         ok1(locking_errors == 0);
103         ok1(tdb_unlockall(tdb) == 0);
104
105         ok1(prepare_entries(tdb));
106         ok1(locking_errors == 0);
107         ok1(tdb_lockall(tdb) == 0);
108         ok1(locking_errors == 0);
109         delete_entries(tdb);
110         ok1(locking_errors == 0);
111         ok1(tdb_unlockall(tdb) == 0);
112
113         ok1(tdb_close(tdb) == 0);       
114
115         return exit_status();
116 }