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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Aug 8, 2024 at 16:06 UTC 9a833ca35dfcefa0e431da5f7dcdc2e58f38469d
1 file changed +32 -32
reftable/stack_test.c
+32 -32
@@ -109,6 +109,34 @@ static int write_test_ref(struct reftable_writer *wr, void *arg)
109 return reftable_writer_add_ref(wr, ref);
110 }
111
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 +
140 struct write_log_arg {
141 struct reftable_log_record *log;
142 uint64_t update_index;
@@ -916,25 +944,11 @@ static void test_reftable_stack_compaction_concurrent(void)
944 struct reftable_write_options opts = { 0 };
945 struct reftable_stack *st1 = NULL, *st2 = NULL;
946 char *dir = get_tmp_dir(__LINE__);
919 - int err, i;
920 - int N = 3;
947 + int err;
948
949 err = reftable_new_stack(&st1, dir, &opts);
950 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);
952
953 err = reftable_new_stack(&st2, dir, &opts);
954 EXPECT_ERR(err);
@@ -965,25 +979,11 @@ static void test_reftable_stack_compaction_concurrent_clean(void)
979 struct reftable_write_options opts = { 0 };
980 struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL;
981 char *dir = get_tmp_dir(__LINE__);
968 - int err, i;
969 - int N = 3;
982 + int err;
983
984 err = reftable_new_stack(&st1, dir, &opts);
985 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);
987
988 err = reftable_new_stack(&st2, dir, &opts);
989 EXPECT_ERR(err);