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