383b74cb87d59965b5ec194aa6cedac8a6a01c6d
[ccan] / ccan / tdb2 / test / run-tdb1-no-lock-during-traverse.c
1 #include <ccan/tdb2/private.h>
2 #include <unistd.h>
3 #include "tdb1-lock-tracking.h"
4
5 #define fcntl fcntl_with_lockcheck1
6
7 #include "tdb2-source.h"
8 #include <ccan/tap/tap.h>
9 #include <stdlib.h>
10 #include <err.h>
11 #include "logging.h"
12
13 #undef fcntl
14
15 #define NUM_ENTRIES 10
16
17 static bool prepare_entries(struct tdb_context *tdb)
18 {
19         unsigned int i;
20         TDB_DATA key, data;
21
22         for (i = 0; i < NUM_ENTRIES; i++) {
23                 key.dsize = sizeof(i);
24                 key.dptr = (void *)&i;
25                 data.dsize = strlen("world");
26                 data.dptr = (void *)"world";
27
28                 if (tdb_store(tdb, key, data, 0) != TDB_SUCCESS)
29                         return false;
30         }
31         return true;
32 }
33
34 static void delete_entries(struct tdb_context *tdb)
35 {
36         unsigned int i;
37         TDB_DATA key;
38
39         for (i = 0; i < NUM_ENTRIES; i++) {
40                 key.dsize = sizeof(i);
41                 key.dptr = (void *)&i;
42
43                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
44         }
45 }
46
47 /* We don't know how many times this will run. */
48 static int delete_other(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
49                         void *private_data)
50 {
51         unsigned int i;
52         memcpy(&i, key.dptr, 4);
53         i = (i + 1) % NUM_ENTRIES;
54         key.dptr = (void *)&i;
55         if (tdb_delete(tdb, key) != TDB_SUCCESS)
56                 (*(int *)private_data)++;
57         return 0;
58 }
59
60 static int delete_self(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
61                         void *private_data)
62 {
63         ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
64         return 0;
65 }
66
67 int main(int argc, char *argv[])
68 {
69         struct tdb_context *tdb;
70         int errors = 0;
71         union tdb_attribute hsize;
72
73         hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
74         hsize.base.next = &tap_log_attr;
75         hsize.tdb1_hashsize.hsize = 1024;
76
77         plan_tests(41);
78         tdb = tdb_open("run-no-lock-during-traverse.tdb1",
79                        TDB_VERSION1, O_CREAT|O_TRUNC|O_RDWR,
80                        0600, &hsize);
81
82         ok1(tdb);
83         ok1(prepare_entries(tdb));
84         ok1(locking_errors1 == 0);
85         ok1(tdb1_lockall(tdb) == 0);
86         ok1(locking_errors1 == 0);
87         tdb1_traverse(tdb, delete_other, &errors);
88         ok1(errors == 0);
89         ok1(locking_errors1 == 0);
90         ok1(tdb1_unlockall(tdb) == 0);
91
92         ok1(prepare_entries(tdb));
93         ok1(locking_errors1 == 0);
94         ok1(tdb1_lockall(tdb) == 0);
95         ok1(locking_errors1 == 0);
96         tdb1_traverse(tdb, delete_self, NULL);
97         ok1(locking_errors1 == 0);
98         ok1(tdb1_unlockall(tdb) == 0);
99
100         ok1(prepare_entries(tdb));
101         ok1(locking_errors1 == 0);
102         ok1(tdb1_lockall(tdb) == 0);
103         ok1(locking_errors1 == 0);
104         delete_entries(tdb);
105         ok1(locking_errors1 == 0);
106         ok1(tdb1_unlockall(tdb) == 0);
107
108         ok1(tdb_close(tdb) == 0);
109
110         return exit_status();
111 }