Skip to content

Commit 9a833ca

Browse files
pks-tgitster
authored andcommitted
reftable/stack: extract function to setup stack with N tables
We're about to add two tests, and both of them will want to initialize the reftable stack with a set of N tables. Introduce a new function that handles this and refactor existing tests that use such a setup to use it. Note that this changes the exact records contained in the preexisting tests. This is fine though as we only care about the shape of the stack here, not the shape of each table. Furthermore, with this change we now start to disable auto compaction when writing the tables, as otherwise we might not end up with the expected amount of new tables added. This also slightly changes the behaviour of these tests, but the properties we care for remain intact. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ed7d2f4 commit 9a833ca

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

reftable/stack_test.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ static int write_test_ref(struct reftable_writer *wr, void *arg)
109109
return reftable_writer_add_ref(wr, ref);
110110
}
111111

112+
static void write_n_ref_tables(struct reftable_stack *st,
113+
size_t n)
114+
{
115+
struct strbuf buf = STRBUF_INIT;
116+
int disable_auto_compact;
117+
int err;
118+
119+
disable_auto_compact = st->opts.disable_auto_compact;
120+
st->opts.disable_auto_compact = 1;
121+
122+
for (size_t i = 0; i < n; i++) {
123+
struct reftable_ref_record ref = {
124+
.update_index = reftable_stack_next_update_index(st),
125+
.value_type = REFTABLE_REF_VAL1,
126+
};
127+
128+
strbuf_addf(&buf, "refs/heads/branch-%04u", (unsigned) i);
129+
ref.refname = buf.buf;
130+
set_test_hash(ref.value.val1, i);
131+
132+
err = reftable_stack_add(st, &write_test_ref, &ref);
133+
EXPECT_ERR(err);
134+
}
135+
136+
st->opts.disable_auto_compact = disable_auto_compact;
137+
strbuf_release(&buf);
138+
}
139+
112140
struct write_log_arg {
113141
struct reftable_log_record *log;
114142
uint64_t update_index;
@@ -916,25 +944,11 @@ static void test_reftable_stack_compaction_concurrent(void)
916944
struct reftable_write_options opts = { 0 };
917945
struct reftable_stack *st1 = NULL, *st2 = NULL;
918946
char *dir = get_tmp_dir(__LINE__);
919-
int err, i;
920-
int N = 3;
947+
int err;
921948

922949
err = reftable_new_stack(&st1, dir, &opts);
923950
EXPECT_ERR(err);
924-
925-
for (i = 0; i < N; i++) {
926-
char name[100];
927-
struct reftable_ref_record ref = {
928-
.refname = name,
929-
.update_index = reftable_stack_next_update_index(st1),
930-
.value_type = REFTABLE_REF_SYMREF,
931-
.value.symref = (char *) "master",
932-
};
933-
snprintf(name, sizeof(name), "branch%04d", i);
934-
935-
err = reftable_stack_add(st1, &write_test_ref, &ref);
936-
EXPECT_ERR(err);
937-
}
951+
write_n_ref_tables(st1, 3);
938952

939953
err = reftable_new_stack(&st2, dir, &opts);
940954
EXPECT_ERR(err);
@@ -965,25 +979,11 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
965979
struct reftable_write_options opts = { 0 };
966980
struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL;
967981
char *dir = get_tmp_dir(__LINE__);
968-
int err, i;
969-
int N = 3;
982+
int err;
970983

971984
err = reftable_new_stack(&st1, dir, &opts);
972985
EXPECT_ERR(err);
973-
974-
for (i = 0; i < N; i++) {
975-
char name[100];
976-
struct reftable_ref_record ref = {
977-
.refname = name,
978-
.update_index = reftable_stack_next_update_index(st1),
979-
.value_type = REFTABLE_REF_SYMREF,
980-
.value.symref = (char *) "master",
981-
};
982-
snprintf(name, sizeof(name), "branch%04d", i);
983-
984-
err = reftable_stack_add(st1, &write_test_ref, &ref);
985-
EXPECT_ERR(err);
986-
}
986+
write_n_ref_tables(st1, 3);
987987

988988
err = reftable_new_stack(&st2, dir, &opts);
989989
EXPECT_ERR(err);

0 commit comments

Comments
 (0)