Line data Source code
1 : #include "../common/tdb_private.h"
2 : #include "lock-tracking.h"
3 : #define fcntl fcntl_with_lockcheck
4 : #include "../common/io.c"
5 : #include "../common/tdb.c"
6 : #include "../common/lock.c"
7 : #include "../common/freelist.c"
8 : #include "../common/traverse.c"
9 : #include "../common/transaction.c"
10 : #include "../common/error.c"
11 : #include "../common/open.c"
12 : #include "../common/check.c"
13 : #include "../common/hash.c"
14 : #include "../common/mutex.c"
15 : #include "tap-interface.h"
16 : #undef fcntl
17 : #include <stdlib.h>
18 : #include <stdbool.h>
19 : #include "external-agent.h"
20 : #include "logging.h"
21 :
22 : static struct agent *agent;
23 :
24 4 : static bool correct_key(TDB_DATA key)
25 : {
26 4 : return key.dsize == strlen("hi")
27 4 : && memcmp(key.dptr, "hi", key.dsize) == 0;
28 : }
29 :
30 4 : static bool correct_data(TDB_DATA data)
31 : {
32 4 : return data.dsize == strlen("world")
33 4 : && memcmp(data.dptr, "world", data.dsize) == 0;
34 : }
35 :
36 2 : static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
37 : void *p)
38 : {
39 2 : ok1(correct_key(key));
40 2 : ok1(correct_data(data));
41 2 : return 0;
42 : }
43 :
44 1 : static int traverse1r(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
45 : void *p)
46 : {
47 1 : ok1(correct_key(key));
48 1 : ok1(correct_data(data));
49 1 : ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
50 : == SUCCESS);
51 1 : ok1(external_agent_operation(agent, STORE, tdb_name(tdb))
52 : == SUCCESS);
53 1 : ok1(external_agent_operation(agent, TRANSACTION_COMMIT, tdb_name(tdb))
54 : == WOULD_HAVE_BLOCKED);
55 1 : tdb_traverse(tdb, traverse2, NULL);
56 :
57 : /* That should *not* release the all-records lock! */
58 1 : ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
59 : == SUCCESS);
60 1 : ok1(external_agent_operation(agent, STORE, tdb_name(tdb))
61 : == SUCCESS);
62 1 : ok1(external_agent_operation(agent, TRANSACTION_COMMIT, tdb_name(tdb))
63 : == WOULD_HAVE_BLOCKED);
64 1 : return 0;
65 : }
66 :
67 1 : static int traverse1w(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
68 : void *p)
69 : {
70 1 : ok1(correct_key(key));
71 1 : ok1(correct_data(data));
72 1 : ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
73 : == WOULD_HAVE_BLOCKED);
74 1 : tdb_traverse(tdb, traverse2, NULL);
75 :
76 : /* That should *not* release the all-records lock! */
77 1 : ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
78 : == WOULD_HAVE_BLOCKED);
79 1 : return 0;
80 : }
81 :
82 1 : int main(int argc, char *argv[])
83 : {
84 : struct tdb_context *tdb;
85 : TDB_DATA key, data;
86 :
87 : plan_tests(17);
88 1 : agent = prepare_external_agent();
89 :
90 1 : tdb = tdb_open_ex("run-nested-traverse.tdb", 1024, TDB_CLEAR_IF_FIRST,
91 : O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
92 1 : ok1(tdb);
93 :
94 1 : ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);
95 1 : ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
96 : == SUCCESS);
97 1 : ok1(external_agent_operation(agent, TRANSACTION_COMMIT, tdb_name(tdb))
98 : == SUCCESS);
99 :
100 1 : key.dsize = strlen("hi");
101 1 : key.dptr = discard_const_p(uint8_t, "hi");
102 1 : data.dptr = discard_const_p(uint8_t, "world");
103 1 : data.dsize = strlen("world");
104 :
105 1 : ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
106 1 : tdb_traverse(tdb, traverse1w, NULL);
107 1 : tdb_traverse_read(tdb, traverse1r, NULL);
108 1 : tdb_close(tdb);
109 :
110 1 : return exit_status();
111 : }
|