t-reftable-stack: add test for stack iterators

reftable_stack_init_ref_iterator and reftable_stack_init_log_iterator as defined by reftable/stack.{c,h} initialize a stack iterator to iterate over the ref and log records in a reftable stack respectively. Since these functions are not exercised by any of the existing tests, add a test for them. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Chandra Pratap committed Sep 8, 2024 at 09:36 UTC 2b14ced370aea017efc32b525fd72a6dd2cc60c5
1 file changed +83
t/unit-tests/t-reftable-stack.c
+83
@@ -544,6 +544,88 @@ static void t_reftable_stack_add(void)
544 clear_dir(dir);
545 }
546
547 +static void t_reftable_stack_iterator(void)
548 +{
549 + struct reftable_write_options opts = { 0 };
550 + struct reftable_stack *st = NULL;
551 + char *dir = get_tmp_dir(__LINE__);
552 + struct reftable_ref_record refs[10] = { 0 };
553 + struct reftable_log_record logs[10] = { 0 };
554 + struct reftable_iterator it = { 0 };
555 + size_t N = ARRAY_SIZE(refs), i;
556 + int err;
557 +
558 + err = reftable_new_stack(&st, dir, &opts);
559 + check(!err);
560 +
561 + for (i = 0; i < N; i++) {
562 + refs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
563 + refs[i].update_index = i + 1;
564 + refs[i].value_type = REFTABLE_REF_VAL1;
565 + set_test_hash(refs[i].value.val1, i);
566 +
567 + logs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
568 + logs[i].update_index = i + 1;
569 + logs[i].value_type = REFTABLE_LOG_UPDATE;
570 + logs[i].value.update.email = xstrdup("johndoe@invalid");
571 + logs[i].value.update.message = xstrdup("commit\n");
572 + set_test_hash(logs[i].value.update.new_hash, i);
573 + }
574 +
575 + for (i = 0; i < N; i++) {
576 + err = reftable_stack_add(st, write_test_ref, &refs[i]);
577 + check(!err);
578 + }
579 +
580 + for (i = 0; i < N; i++) {
581 + struct write_log_arg arg = {
582 + .log = &logs[i],
583 + .update_index = reftable_stack_next_update_index(st),
584 + };
585 +
586 + err = reftable_stack_add(st, write_test_log, &arg);
587 + check(!err);
588 + }
589 +
590 + reftable_stack_init_ref_iterator(st, &it);
591 + reftable_iterator_seek_ref(&it, refs[0].refname);
592 + for (i = 0; ; i++) {
593 + struct reftable_ref_record ref = { 0 };
594 +
595 + err = reftable_iterator_next_ref(&it, &ref);
596 + if (err > 0)
597 + break;
598 + check(!err);
599 + check(reftable_ref_record_equal(&ref, &refs[i], GIT_SHA1_RAWSZ));
600 + reftable_ref_record_release(&ref);
601 + }
602 + check_int(i, ==, N);
603 +
604 + reftable_iterator_destroy(&it);
605 +
606 + reftable_stack_init_log_iterator(st, &it);
607 + reftable_iterator_seek_log(&it, logs[0].refname);
608 + for (i = 0; ; i++) {
609 + struct reftable_log_record log = { 0 };
610 +
611 + err = reftable_iterator_next_log(&it, &log);
612 + if (err > 0)
613 + break;
614 + check(!err);
615 + check(reftable_log_record_equal(&log, &logs[i], GIT_SHA1_RAWSZ));
616 + reftable_log_record_release(&log);
617 + }
618 + check_int(i, ==, N);
619 +
620 + reftable_stack_destroy(st);
621 + reftable_iterator_destroy(&it);
622 + for (i = 0; i < N; i++) {
623 + reftable_ref_record_release(&refs[i]);
624 + reftable_log_record_release(&logs[i]);
625 + }
626 + clear_dir(dir);
627 +}
628 +
629 static void t_reftable_stack_log_normalize(void)
630 {
631 int err = 0;
@@ -1225,6 +1307,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
1307 TEST(t_reftable_stack_compaction_concurrent_clean(), "compaction with unclean stack shutdown");
1308 TEST(t_reftable_stack_compaction_with_locked_tables(), "compaction with locked tables");
1309 TEST(t_reftable_stack_hash_id(), "read stack with wrong hash ID");
1310 + TEST(t_reftable_stack_iterator(), "log and ref iterator for reftable stack");
1311 TEST(t_reftable_stack_lock_failure(), "stack addition with lockfile failure");
1312 TEST(t_reftable_stack_log_normalize(), "log messages should be normalized");
1313 TEST(t_reftable_stack_read_across_reload(), "stack iterators work across reloads");