1 #include "../common/tdb_private.h"
3 /* Speed up the tests, but do the actual sync tests. */
4 static unsigned int sync_counts = 0;
5 static inline int fake_fsync(int fd)
10 #define fsync fake_fsync
13 static inline int fake_msync(void *addr, size_t length, int flags)
18 #define msync fake_msync
22 static inline int fake_fdatasync(int fd)
27 #define fdatasync fake_fdatasync
30 #include "../common/io.c"
31 #include "../common/tdb.c"
32 #include "../common/lock.c"
33 #include "../common/freelist.c"
34 #include "../common/traverse.c"
35 #include "../common/transaction.c"
36 #include "../common/error.c"
37 #include "../common/open.c"
38 #include "../common/check.c"
39 #include "../common/hash.c"
40 #include "tap-interface.h"
45 static void write_record(struct tdb_context *tdb, size_t extra_len,
49 key.dsize = strlen("hi");
50 key.dptr = (void *)"hi";
52 data->dsize += extra_len;
53 tdb_transaction_start(tdb);
54 tdb_store(tdb, key, *data, TDB_REPLACE);
55 tdb_transaction_commit(tdb);
58 int main(int argc, char *argv[])
60 struct tdb_context *tdb;
63 struct tdb_record rec;
66 /* Do *not* suppress sync for this test; we do it ourselves. */
67 unsetenv("TDB_NO_FSYNC");
70 tdb = tdb_open_ex("run-transaction-expand.tdb",
71 1024, TDB_CLEAR_IF_FIRST,
72 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
76 data.dptr = calloc(1000, getpagesize());
78 /* Simulate a slowly growing record. */
79 for (i = 0; i < 1000; i++)
80 write_record(tdb, getpagesize(), &data);
82 tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off);
83 tdb_read(tdb, off, &rec, sizeof(rec), DOCONV());
84 diag("TDB size = %zu, recovery = %u-%u",
85 (size_t)tdb->map_size, off, off + sizeof(rec) + rec.rec_len);
87 /* We should only be about 5 times larger than largest record. */
88 ok1(tdb->map_size < 6 * i * getpagesize());
91 tdb = tdb_open_ex("run-transaction-expand.tdb",
92 1024, TDB_CLEAR_IF_FIRST,
93 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
98 /* Simulate a slowly growing record, repacking to keep
99 * recovery area at end. */
100 for (i = 0; i < 1000; i++) {
101 write_record(tdb, getpagesize(), &data);
106 tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &off);
107 tdb_read(tdb, off, &rec, sizeof(rec), DOCONV());
108 diag("TDB size = %zu, recovery = %u-%u",
109 (size_t)tdb->map_size, off, off + sizeof(rec) + rec.rec_len);
111 /* We should only be about 4 times larger than largest record. */
112 ok1(tdb->map_size < 5 * i * getpagesize());
114 /* We should have synchronized multiple times. */
119 return exit_status();