Skip to content

Commit

Permalink
t/unit-tests: introduce reftable library
Browse files Browse the repository at this point in the history
We have recently migrated all of the reftable unit tests that were part
of the reftable library into our own unit testing framework. As part of
that migration we have duplicated some of the functionality that was
part of the reftable test framework into each of the migrated test
suites. This was a sensible decision to not have all of the migrations
dependent on each other, but now that the migration is done it makes
sense to deduplicate the functionality again.

Introduce a new reftable test library that hosts some shared code and
adapt tests to use it.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
pks-t authored and gitster committed Sep 9, 2024
1 parent e7e6d74 commit 4d76732
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 179 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@ UNIT_TEST_PROGRAMS += t-urlmatch-normalization
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-oid.o
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/lib-reftable.o

# xdiff and reftable libs may in turn depend on what is in libgit.a
GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(LIB_FILE)
Expand Down
93 changes: 93 additions & 0 deletions t/unit-tests/lib-reftable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "lib-reftable.h"
#include "test-lib.h"
#include "reftable/constants.h"
#include "reftable/writer.h"

void t_reftable_set_hash(uint8_t *p, int i, uint32_t id)
{
memset(p, (uint8_t)i, hash_size(id));
}

static ssize_t strbuf_writer_write(void *b, const void *data, size_t sz)
{
strbuf_add(b, data, sz);
return sz;
}

static int strbuf_writer_flush(void *arg UNUSED)
{
return 0;
}

struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf,
struct reftable_write_options *opts)
{
return reftable_new_writer(&strbuf_writer_write,
&strbuf_writer_flush,
buf, opts);
}

void t_reftable_write_to_buf(struct strbuf *buf,
struct reftable_ref_record *refs,
size_t nrefs,
struct reftable_log_record *logs,
size_t nlogs,
struct reftable_write_options *_opts)
{
struct reftable_write_options opts = { 0 };
const struct reftable_stats *stats;
struct reftable_writer *writer;
uint64_t min = 0xffffffff;
uint64_t max = 0;
int ret;

if (_opts)
opts = *_opts;

for (size_t i = 0; i < nrefs; i++) {
uint64_t ui = refs[i].update_index;
if (ui > max)
max = ui;
if (ui < min)
min = ui;
}
for (size_t i = 0; i < nlogs; i++) {
uint64_t ui = logs[i].update_index;
if (ui > max)
max = ui;
if (ui < min)
min = ui;
}

writer = t_reftable_strbuf_writer(buf, &opts);
reftable_writer_set_limits(writer, min, max);

if (nrefs) {
ret = reftable_writer_add_refs(writer, refs, nrefs);
check_int(ret, ==, 0);
}

if (nlogs) {
ret = reftable_writer_add_logs(writer, logs, nlogs);
check_int(ret, ==, 0);
}

ret = reftable_writer_close(writer);
check_int(ret, ==, 0);

stats = reftable_writer_stats(writer);
for (size_t i = 0; i < stats->ref_stats.blocks; i++) {
size_t off = i * (opts.block_size ? opts.block_size
: DEFAULT_BLOCK_SIZE);
if (!off)
off = header_size(opts.hash_id == GIT_SHA256_FORMAT_ID ? 2 : 1);
check_char(buf->buf[off], ==, 'r');
}

if (nrefs)
check_int(stats->ref_stats.blocks, >, 0);
if (nlogs)
check_int(stats->log_stats.blocks, >, 0);

reftable_writer_free(writer);
}
20 changes: 20 additions & 0 deletions t/unit-tests/lib-reftable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef LIB_REFTABLE_H
#define LIB_REFTABLE_H

#include "git-compat-util.h"
#include "strbuf.h"
#include "reftable/reftable-writer.h"

void t_reftable_set_hash(uint8_t *p, int i, uint32_t id);

struct reftable_writer *t_reftable_strbuf_writer(struct strbuf *buf,
struct reftable_write_options *opts);

void t_reftable_write_to_buf(struct strbuf *buf,
struct reftable_ref_record *refs,
size_t nrecords,
struct reftable_log_record *logs,
size_t nlogs,
struct reftable_write_options *opts);

#endif
87 changes: 11 additions & 76 deletions t/unit-tests/t-reftable-merged.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license that can be found in the LICENSE file or at
*/

#include "test-lib.h"
#include "lib-reftable.h"
#include "reftable/blocksource.h"
#include "reftable/constants.h"
#include "reftable/merged.h"
Expand All @@ -15,91 +16,23 @@ license that can be found in the LICENSE file or at
#include "reftable/reftable-merged.h"
#include "reftable/reftable-writer.h"

static ssize_t strbuf_add_void(void *b, const void *data, const size_t sz)
{
strbuf_add(b, data, sz);
return sz;
}

static int noop_flush(void *arg UNUSED)
{
return 0;
}

static void write_test_table(struct strbuf *buf,
struct reftable_ref_record refs[], const size_t n)
{
uint64_t min = 0xffffffff;
uint64_t max = 0;
size_t i;
int err;

struct reftable_write_options opts = {
.block_size = 256,
};
struct reftable_writer *w = NULL;
for (i = 0; i < n; i++) {
uint64_t ui = refs[i].update_index;
if (ui > max)
max = ui;
if (ui < min)
min = ui;
}

w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
reftable_writer_set_limits(w, min, max);

for (i = 0; i < n; i++) {
uint64_t before = refs[i].update_index;
int n = reftable_writer_add_ref(w, &refs[i]);
check_int(n, ==, 0);
check_int(before, ==, refs[i].update_index);
}

err = reftable_writer_close(w);
check(!err);

reftable_writer_free(w);
}

static void write_test_log_table(struct strbuf *buf, struct reftable_log_record logs[],
const size_t n, const uint64_t update_index)
{
int err;

struct reftable_write_options opts = {
.block_size = 256,
.exact_log_message = 1,
};
struct reftable_writer *w = NULL;
w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
reftable_writer_set_limits(w, update_index, update_index);

for (size_t i = 0; i < n; i++) {
int err = reftable_writer_add_log(w, &logs[i]);
check(!err);
}

err = reftable_writer_close(w);
check(!err);

reftable_writer_free(w);
}

static struct reftable_merged_table *
merged_table_from_records(struct reftable_ref_record **refs,
struct reftable_block_source **source,
struct reftable_reader ***readers, const size_t *sizes,
struct strbuf *buf, const size_t n)
{
struct reftable_merged_table *mt = NULL;
struct reftable_write_options opts = {
.block_size = 256,
};
int err;

REFTABLE_CALLOC_ARRAY(*readers, n);
REFTABLE_CALLOC_ARRAY(*source, n);

for (size_t i = 0; i < n; i++) {
write_test_table(&buf[i], refs[i], sizes[i]);
t_reftable_write_to_buf(&buf[i], refs[i], sizes[i], NULL, 0, &opts);
block_source_from_strbuf(&(*source)[i], &buf[i]);

err = reftable_reader_new(&(*readers)[i], &(*source)[i],
Expand Down Expand Up @@ -268,13 +201,17 @@ merged_table_from_log_records(struct reftable_log_record **logs,
struct strbuf *buf, const size_t n)
{
struct reftable_merged_table *mt = NULL;
struct reftable_write_options opts = {
.block_size = 256,
.exact_log_message = 1,
};
int err;

REFTABLE_CALLOC_ARRAY(*readers, n);
REFTABLE_CALLOC_ARRAY(*source, n);

for (size_t i = 0; i < n; i++) {
write_test_log_table(&buf[i], logs[i], sizes[i], i + 1);
t_reftable_write_to_buf(&buf[i], NULL, 0, logs[i], sizes[i], &opts);
block_source_from_strbuf(&(*source)[i], &buf[i]);

err = reftable_reader_new(&(*readers)[i], &(*source)[i],
Expand Down Expand Up @@ -402,9 +339,7 @@ static void t_default_write_opts(void)
{
struct reftable_write_options opts = { 0 };
struct strbuf buf = STRBUF_INIT;
struct reftable_writer *w =
reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);

struct reftable_writer *w = t_reftable_strbuf_writer(&buf, &opts);
struct reftable_ref_record rec = {
.refname = (char *) "master",
.update_index = 1,
Expand Down
Loading

0 comments on commit 4d76732

Please sign in to comment.