From 75281499320b9a4368aaa281ae282e55c47ac488 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 20 Dec 2011 17:37:45 +1030 Subject: [PATCH] tdb: test exponential recovery expansion problem. --- ccan/tdb/test/run-transaction-expand.c | 78 ++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ccan/tdb/test/run-transaction-expand.c diff --git a/ccan/tdb/test/run-transaction-expand.c b/ccan/tdb/test/run-transaction-expand.c new file mode 100644 index 00000000..463cb106 --- /dev/null +++ b/ccan/tdb/test/run-transaction-expand.c @@ -0,0 +1,78 @@ +/* We need this otherwise fcntl locking fails. */ +#define _FILE_OFFSET_BITS 64 +#define _XOPEN_SOURCE 500 +#include + +/* Speed up the tests: setting TDB_NOSYNC removed recovery altogether. */ +static inline int fake_fsync(int fd) +{ + return 0; +} +#define fsync fake_fsync + +#ifdef MS_SYNC +static inline int fake_msync(void *addr, size_t length, int flags) +{ + return 0; +} +#define msync fake_msync +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "logging.h" + +static void write_record(struct tdb_context *tdb, size_t extra_len, + TDB_DATA *data) +{ + TDB_DATA key; + key.dsize = strlen("hi"); + key.dptr = (void *)"hi"; + + data->dptr = realloc(data->dptr, data->dsize + extra_len); + memset(data->dptr + data->dsize, 'x', extra_len); + data->dsize += extra_len; + tdb_transaction_start(tdb); + tdb_store(tdb, key, *data, TDB_REPLACE); + tdb_transaction_commit(tdb); + diag("TDB size = %zu", (size_t)tdb->map_size); +} + +int main(int argc, char *argv[]) +{ + struct tdb_context *tdb; + size_t i; + TDB_DATA data; + + plan_tests(2); + tdb = tdb_open_ex("run-transaction-expand.tdb", + 1024, TDB_CLEAR_IF_FIRST, + O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL); + ok1(tdb); + + data.dsize = 0; + data.dptr = NULL; + + /* Simulate a slowly growing record. */ + for (i = 0; i < 1000; i++) + write_record(tdb, getpagesize(), &data); + + /* We should only be about 3 times larger than largest record. */ + ok1(tdb->map_size < 3 * i * getpagesize()); + tdb_close(tdb); + free(data.dptr); + + return exit_status(); +} -- 2.39.2