2 Copyright 2020 Google LLC
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
13 #include "reftable-reader.h"
17 #include "test_framework.h"
18 #include "reftable-tests.h"
21 #include <sys/types.h>
24 static void clear_dir(const char *dirname
)
26 struct strbuf path
= STRBUF_INIT
;
27 strbuf_addstr(&path
, dirname
);
28 remove_dir_recursively(&path
, 0);
29 strbuf_release(&path
);
32 static int count_dir_entries(const char *dirname
)
34 DIR *dir
= opendir(dirname
);
40 while ((d
= readdir(dir
))) {
41 if (!strcmp(d
->d_name
, "..") || !strcmp(d
->d_name
, "."))
50 * Work linenumber into the tempdir, so we can see which tests forget to
53 static char *get_tmp_template(int linenumber
)
55 const char *tmp
= getenv("TMPDIR");
56 static char template[1024];
57 snprintf(template, sizeof(template) - 1, "%s/stack_test-%d.XXXXXX",
58 tmp
? tmp
: "/tmp", linenumber
);
62 static char *get_tmp_dir(int linenumber
)
64 char *dir
= get_tmp_template(linenumber
);
69 static void test_read_file(void)
71 char *fn
= get_tmp_template(__LINE__
);
73 char out
[1024] = "line1\n\nline2\nline3";
76 char *want
[] = { "line1", "line2", "line3" };
80 n
= write_in_full(fd
, out
, strlen(out
));
81 EXPECT(n
== strlen(out
));
85 err
= read_lines(fn
, &names
);
88 for (i
= 0; names
[i
]; i
++) {
89 EXPECT(0 == strcmp(want
[i
], names
[i
]));
95 static void test_parse_names(void)
97 char buf
[] = "line\n";
99 parse_names(buf
, strlen(buf
), &names
);
101 EXPECT(NULL
!= names
[0]);
102 EXPECT(0 == strcmp(names
[0], "line"));
103 EXPECT(NULL
== names
[1]);
107 static void test_names_equal(void)
109 char *a
[] = { "a", "b", "c", NULL
};
110 char *b
[] = { "a", "b", "d", NULL
};
111 char *c
[] = { "a", "b", NULL
};
113 EXPECT(names_equal(a
, a
));
114 EXPECT(!names_equal(a
, b
));
115 EXPECT(!names_equal(a
, c
));
118 static int write_test_ref(struct reftable_writer
*wr
, void *arg
)
120 struct reftable_ref_record
*ref
= arg
;
121 reftable_writer_set_limits(wr
, ref
->update_index
, ref
->update_index
);
122 return reftable_writer_add_ref(wr
, ref
);
125 struct write_log_arg
{
126 struct reftable_log_record
*log
;
127 uint64_t update_index
;
130 static int write_test_log(struct reftable_writer
*wr
, void *arg
)
132 struct write_log_arg
*wla
= arg
;
134 reftable_writer_set_limits(wr
, wla
->update_index
, wla
->update_index
);
135 return reftable_writer_add_log(wr
, wla
->log
);
138 static void test_reftable_stack_add_one(void)
140 char *dir
= get_tmp_dir(__LINE__
);
141 struct strbuf scratch
= STRBUF_INIT
;
142 int mask
= umask(002);
143 struct reftable_write_options cfg
= {
144 .default_permissions
= 0660,
146 struct reftable_stack
*st
= NULL
;
148 struct reftable_ref_record ref
= {
151 .value_type
= REFTABLE_REF_SYMREF
,
152 .value
.symref
= "master",
154 struct reftable_ref_record dest
= { NULL
};
155 struct stat stat_result
= { 0 };
156 err
= reftable_new_stack(&st
, dir
, cfg
);
159 err
= reftable_stack_add(st
, &write_test_ref
, &ref
);
162 err
= reftable_stack_read_ref(st
, ref
.refname
, &dest
);
164 EXPECT(0 == strcmp("master", dest
.value
.symref
));
165 EXPECT(st
->readers_len
> 0);
167 printf("testing print functionality:\n");
168 err
= reftable_stack_print_directory(dir
, GIT_SHA1_FORMAT_ID
);
171 err
= reftable_stack_print_directory(dir
, GIT_SHA256_FORMAT_ID
);
172 EXPECT(err
== REFTABLE_FORMAT_ERROR
);
174 #ifndef GIT_WINDOWS_NATIVE
175 strbuf_addstr(&scratch
, dir
);
176 strbuf_addstr(&scratch
, "/tables.list");
177 err
= stat(scratch
.buf
, &stat_result
);
179 EXPECT((stat_result
.st_mode
& 0777) == cfg
.default_permissions
);
181 strbuf_reset(&scratch
);
182 strbuf_addstr(&scratch
, dir
);
183 strbuf_addstr(&scratch
, "/");
184 /* do not try at home; not an external API for reftable. */
185 strbuf_addstr(&scratch
, st
->readers
[0]->name
);
186 err
= stat(scratch
.buf
, &stat_result
);
188 EXPECT((stat_result
.st_mode
& 0777) == cfg
.default_permissions
);
193 reftable_ref_record_release(&dest
);
194 reftable_stack_destroy(st
);
195 strbuf_release(&scratch
);
200 static void test_reftable_stack_uptodate(void)
202 struct reftable_write_options cfg
= { 0 };
203 struct reftable_stack
*st1
= NULL
;
204 struct reftable_stack
*st2
= NULL
;
205 char *dir
= get_tmp_dir(__LINE__
);
208 struct reftable_ref_record ref1
= {
211 .value_type
= REFTABLE_REF_SYMREF
,
212 .value
.symref
= "master",
214 struct reftable_ref_record ref2
= {
215 .refname
= "branch2",
217 .value_type
= REFTABLE_REF_SYMREF
,
218 .value
.symref
= "master",
222 /* simulate multi-process access to the same stack
223 by creating two stacks for the same directory.
225 err
= reftable_new_stack(&st1
, dir
, cfg
);
228 err
= reftable_new_stack(&st2
, dir
, cfg
);
231 err
= reftable_stack_add(st1
, &write_test_ref
, &ref1
);
234 err
= reftable_stack_add(st2
, &write_test_ref
, &ref2
);
235 EXPECT(err
== REFTABLE_LOCK_ERROR
);
237 err
= reftable_stack_reload(st2
);
240 err
= reftable_stack_add(st2
, &write_test_ref
, &ref2
);
242 reftable_stack_destroy(st1
);
243 reftable_stack_destroy(st2
);
247 static void test_reftable_stack_transaction_api(void)
249 char *dir
= get_tmp_dir(__LINE__
);
251 struct reftable_write_options cfg
= { 0 };
252 struct reftable_stack
*st
= NULL
;
254 struct reftable_addition
*add
= NULL
;
256 struct reftable_ref_record ref
= {
259 .value_type
= REFTABLE_REF_SYMREF
,
260 .value
.symref
= "master",
262 struct reftable_ref_record dest
= { NULL
};
265 err
= reftable_new_stack(&st
, dir
, cfg
);
268 reftable_addition_destroy(add
);
270 err
= reftable_stack_new_addition(&add
, st
);
273 err
= reftable_addition_add(add
, &write_test_ref
, &ref
);
276 err
= reftable_addition_commit(add
);
279 reftable_addition_destroy(add
);
281 err
= reftable_stack_read_ref(st
, ref
.refname
, &dest
);
283 EXPECT(REFTABLE_REF_SYMREF
== dest
.value_type
);
284 EXPECT(0 == strcmp("master", dest
.value
.symref
));
286 reftable_ref_record_release(&dest
);
287 reftable_stack_destroy(st
);
291 static void test_reftable_stack_transaction_api_performs_auto_compaction(void)
293 char *dir
= get_tmp_dir(__LINE__
);
294 struct reftable_write_options cfg
= {0};
295 struct reftable_addition
*add
= NULL
;
296 struct reftable_stack
*st
= NULL
;
299 err
= reftable_new_stack(&st
, dir
, cfg
);
302 for (i
= 0; i
<= n
; i
++) {
303 struct reftable_ref_record ref
= {
304 .update_index
= reftable_stack_next_update_index(st
),
305 .value_type
= REFTABLE_REF_SYMREF
,
306 .value
.symref
= "master",
310 snprintf(name
, sizeof(name
), "branch%04d", i
);
314 * Disable auto-compaction for all but the last runs. Like this
315 * we can ensure that we indeed honor this setting and have
316 * better control over when exactly auto compaction runs.
318 st
->disable_auto_compact
= i
!= n
;
320 err
= reftable_stack_new_addition(&add
, st
);
323 err
= reftable_addition_add(add
, &write_test_ref
, &ref
);
326 err
= reftable_addition_commit(add
);
329 reftable_addition_destroy(add
);
332 * The stack length should grow continuously for all runs where
333 * auto compaction is disabled. When enabled, we should merge
334 * all tables in the stack.
337 EXPECT(st
->merged
->stack_len
== i
+ 1);
339 EXPECT(st
->merged
->stack_len
== 1);
342 reftable_stack_destroy(st
);
346 static void test_reftable_stack_validate_refname(void)
348 struct reftable_write_options cfg
= { 0 };
349 struct reftable_stack
*st
= NULL
;
351 char *dir
= get_tmp_dir(__LINE__
);
354 struct reftable_ref_record ref
= {
357 .value_type
= REFTABLE_REF_SYMREF
,
358 .value
.symref
= "master",
360 char *additions
[] = { "a", "a/b/c" };
362 err
= reftable_new_stack(&st
, dir
, cfg
);
365 err
= reftable_stack_add(st
, &write_test_ref
, &ref
);
368 for (i
= 0; i
< ARRAY_SIZE(additions
); i
++) {
369 struct reftable_ref_record ref
= {
370 .refname
= additions
[i
],
372 .value_type
= REFTABLE_REF_SYMREF
,
373 .value
.symref
= "master",
376 err
= reftable_stack_add(st
, &write_test_ref
, &ref
);
377 EXPECT(err
== REFTABLE_NAME_CONFLICT
);
380 reftable_stack_destroy(st
);
384 static int write_error(struct reftable_writer
*wr
, void *arg
)
386 return *((int *)arg
);
389 static void test_reftable_stack_update_index_check(void)
391 char *dir
= get_tmp_dir(__LINE__
);
393 struct reftable_write_options cfg
= { 0 };
394 struct reftable_stack
*st
= NULL
;
396 struct reftable_ref_record ref1
= {
399 .value_type
= REFTABLE_REF_SYMREF
,
400 .value
.symref
= "master",
402 struct reftable_ref_record ref2
= {
405 .value_type
= REFTABLE_REF_SYMREF
,
406 .value
.symref
= "master",
409 err
= reftable_new_stack(&st
, dir
, cfg
);
412 err
= reftable_stack_add(st
, &write_test_ref
, &ref1
);
415 err
= reftable_stack_add(st
, &write_test_ref
, &ref2
);
416 EXPECT(err
== REFTABLE_API_ERROR
);
417 reftable_stack_destroy(st
);
421 static void test_reftable_stack_lock_failure(void)
423 char *dir
= get_tmp_dir(__LINE__
);
425 struct reftable_write_options cfg
= { 0 };
426 struct reftable_stack
*st
= NULL
;
429 err
= reftable_new_stack(&st
, dir
, cfg
);
431 for (i
= -1; i
!= REFTABLE_EMPTY_TABLE_ERROR
; i
--) {
432 err
= reftable_stack_add(st
, &write_error
, &i
);
436 reftable_stack_destroy(st
);
440 static void test_reftable_stack_add(void)
444 struct reftable_write_options cfg
= {
445 .exact_log_message
= 1,
446 .default_permissions
= 0660,
448 struct reftable_stack
*st
= NULL
;
449 char *dir
= get_tmp_dir(__LINE__
);
450 struct reftable_ref_record refs
[2] = { { NULL
} };
451 struct reftable_log_record logs
[2] = { { NULL
} };
452 struct strbuf path
= STRBUF_INIT
;
453 struct stat stat_result
;
454 int N
= ARRAY_SIZE(refs
);
456 err
= reftable_new_stack(&st
, dir
, cfg
);
458 st
->disable_auto_compact
= 1;
460 for (i
= 0; i
< N
; i
++) {
462 snprintf(buf
, sizeof(buf
), "branch%02d", i
);
463 refs
[i
].refname
= xstrdup(buf
);
464 refs
[i
].update_index
= i
+ 1;
465 refs
[i
].value_type
= REFTABLE_REF_VAL1
;
466 set_test_hash(refs
[i
].value
.val1
, i
);
468 logs
[i
].refname
= xstrdup(buf
);
469 logs
[i
].update_index
= N
+ i
+ 1;
470 logs
[i
].value_type
= REFTABLE_LOG_UPDATE
;
472 logs
[i
].value
.update
.new_hash
= reftable_malloc(GIT_SHA1_RAWSZ
);
473 logs
[i
].value
.update
.email
= xstrdup("identity@invalid");
474 set_test_hash(logs
[i
].value
.update
.new_hash
, i
);
477 for (i
= 0; i
< N
; i
++) {
478 int err
= reftable_stack_add(st
, &write_test_ref
, &refs
[i
]);
482 for (i
= 0; i
< N
; i
++) {
483 struct write_log_arg arg
= {
485 .update_index
= reftable_stack_next_update_index(st
),
487 int err
= reftable_stack_add(st
, &write_test_log
, &arg
);
491 err
= reftable_stack_compact_all(st
, NULL
);
494 for (i
= 0; i
< N
; i
++) {
495 struct reftable_ref_record dest
= { NULL
};
497 int err
= reftable_stack_read_ref(st
, refs
[i
].refname
, &dest
);
499 EXPECT(reftable_ref_record_equal(&dest
, refs
+ i
,
501 reftable_ref_record_release(&dest
);
504 for (i
= 0; i
< N
; i
++) {
505 struct reftable_log_record dest
= { NULL
};
506 int err
= reftable_stack_read_log(st
, refs
[i
].refname
, &dest
);
508 EXPECT(reftable_log_record_equal(&dest
, logs
+ i
,
510 reftable_log_record_release(&dest
);
513 #ifndef GIT_WINDOWS_NATIVE
514 strbuf_addstr(&path
, dir
);
515 strbuf_addstr(&path
, "/tables.list");
516 err
= stat(path
.buf
, &stat_result
);
518 EXPECT((stat_result
.st_mode
& 0777) == cfg
.default_permissions
);
521 strbuf_addstr(&path
, dir
);
522 strbuf_addstr(&path
, "/");
523 /* do not try at home; not an external API for reftable. */
524 strbuf_addstr(&path
, st
->readers
[0]->name
);
525 err
= stat(path
.buf
, &stat_result
);
527 EXPECT((stat_result
.st_mode
& 0777) == cfg
.default_permissions
);
533 reftable_stack_destroy(st
);
534 for (i
= 0; i
< N
; i
++) {
535 reftable_ref_record_release(&refs
[i
]);
536 reftable_log_record_release(&logs
[i
]);
538 strbuf_release(&path
);
542 static void test_reftable_stack_log_normalize(void)
545 struct reftable_write_options cfg
= {
548 struct reftable_stack
*st
= NULL
;
549 char *dir
= get_tmp_dir(__LINE__
);
551 uint8_t h1
[GIT_SHA1_RAWSZ
] = { 0x01 }, h2
[GIT_SHA1_RAWSZ
] = { 0x02 };
553 struct reftable_log_record input
= { .refname
= "branch",
555 .value_type
= REFTABLE_LOG_UPDATE
,
556 .value
= { .update
= {
560 struct reftable_log_record dest
= {
563 struct write_log_arg arg
= {
568 err
= reftable_new_stack(&st
, dir
, cfg
);
571 input
.value
.update
.message
= "one\ntwo";
572 err
= reftable_stack_add(st
, &write_test_log
, &arg
);
573 EXPECT(err
== REFTABLE_API_ERROR
);
575 input
.value
.update
.message
= "one";
576 err
= reftable_stack_add(st
, &write_test_log
, &arg
);
579 err
= reftable_stack_read_log(st
, input
.refname
, &dest
);
581 EXPECT(0 == strcmp(dest
.value
.update
.message
, "one\n"));
583 input
.value
.update
.message
= "two\n";
584 arg
.update_index
= 2;
585 err
= reftable_stack_add(st
, &write_test_log
, &arg
);
587 err
= reftable_stack_read_log(st
, input
.refname
, &dest
);
589 EXPECT(0 == strcmp(dest
.value
.update
.message
, "two\n"));
592 reftable_stack_destroy(st
);
593 reftable_log_record_release(&dest
);
597 static void test_reftable_stack_tombstone(void)
600 char *dir
= get_tmp_dir(__LINE__
);
602 struct reftable_write_options cfg
= { 0 };
603 struct reftable_stack
*st
= NULL
;
605 struct reftable_ref_record refs
[2] = { { NULL
} };
606 struct reftable_log_record logs
[2] = { { NULL
} };
607 int N
= ARRAY_SIZE(refs
);
608 struct reftable_ref_record dest
= { NULL
};
609 struct reftable_log_record log_dest
= { NULL
};
612 err
= reftable_new_stack(&st
, dir
, cfg
);
615 /* even entries add the refs, odd entries delete them. */
616 for (i
= 0; i
< N
; i
++) {
617 const char *buf
= "branch";
618 refs
[i
].refname
= xstrdup(buf
);
619 refs
[i
].update_index
= i
+ 1;
621 refs
[i
].value_type
= REFTABLE_REF_VAL1
;
622 set_test_hash(refs
[i
].value
.val1
, i
);
625 logs
[i
].refname
= xstrdup(buf
);
626 /* update_index is part of the key. */
627 logs
[i
].update_index
= 42;
629 logs
[i
].value_type
= REFTABLE_LOG_UPDATE
;
630 logs
[i
].value
.update
.new_hash
=
631 reftable_malloc(GIT_SHA1_RAWSZ
);
632 set_test_hash(logs
[i
].value
.update
.new_hash
, i
);
633 logs
[i
].value
.update
.email
=
634 xstrdup("identity@invalid");
637 for (i
= 0; i
< N
; i
++) {
638 int err
= reftable_stack_add(st
, &write_test_ref
, &refs
[i
]);
642 for (i
= 0; i
< N
; i
++) {
643 struct write_log_arg arg
= {
645 .update_index
= reftable_stack_next_update_index(st
),
647 int err
= reftable_stack_add(st
, &write_test_log
, &arg
);
651 err
= reftable_stack_read_ref(st
, "branch", &dest
);
653 reftable_ref_record_release(&dest
);
655 err
= reftable_stack_read_log(st
, "branch", &log_dest
);
657 reftable_log_record_release(&log_dest
);
659 err
= reftable_stack_compact_all(st
, NULL
);
662 err
= reftable_stack_read_ref(st
, "branch", &dest
);
665 err
= reftable_stack_read_log(st
, "branch", &log_dest
);
667 reftable_ref_record_release(&dest
);
668 reftable_log_record_release(&log_dest
);
671 reftable_stack_destroy(st
);
672 for (i
= 0; i
< N
; i
++) {
673 reftable_ref_record_release(&refs
[i
]);
674 reftable_log_record_release(&logs
[i
]);
679 static void test_reftable_stack_hash_id(void)
681 char *dir
= get_tmp_dir(__LINE__
);
683 struct reftable_write_options cfg
= { 0 };
684 struct reftable_stack
*st
= NULL
;
687 struct reftable_ref_record ref
= {
689 .value_type
= REFTABLE_REF_SYMREF
,
690 .value
.symref
= "target",
693 struct reftable_write_options cfg32
= { .hash_id
= GIT_SHA256_FORMAT_ID
};
694 struct reftable_stack
*st32
= NULL
;
695 struct reftable_write_options cfg_default
= { 0 };
696 struct reftable_stack
*st_default
= NULL
;
697 struct reftable_ref_record dest
= { NULL
};
699 err
= reftable_new_stack(&st
, dir
, cfg
);
702 err
= reftable_stack_add(st
, &write_test_ref
, &ref
);
705 /* can't read it with the wrong hash ID. */
706 err
= reftable_new_stack(&st32
, dir
, cfg32
);
707 EXPECT(err
== REFTABLE_FORMAT_ERROR
);
709 /* check that we can read it back with default config too. */
710 err
= reftable_new_stack(&st_default
, dir
, cfg_default
);
713 err
= reftable_stack_read_ref(st_default
, "master", &dest
);
716 EXPECT(reftable_ref_record_equal(&ref
, &dest
, GIT_SHA1_RAWSZ
));
717 reftable_ref_record_release(&dest
);
718 reftable_stack_destroy(st
);
719 reftable_stack_destroy(st_default
);
723 static void test_log2(void)
725 EXPECT(1 == fastlog2(3));
726 EXPECT(2 == fastlog2(4));
727 EXPECT(2 == fastlog2(5));
730 static void test_sizes_to_segments(void)
732 uint64_t sizes
[] = { 2, 3, 4, 5, 7, 9 };
733 /* .................0 1 2 3 4 5 */
736 struct segment
*segs
=
737 sizes_to_segments(&seglen
, sizes
, ARRAY_SIZE(sizes
));
738 EXPECT(segs
[2].log
== 3);
739 EXPECT(segs
[2].start
== 5);
740 EXPECT(segs
[2].end
== 6);
742 EXPECT(segs
[1].log
== 2);
743 EXPECT(segs
[1].start
== 2);
744 EXPECT(segs
[1].end
== 5);
748 static void test_sizes_to_segments_empty(void)
751 struct segment
*segs
= sizes_to_segments(&seglen
, NULL
, 0);
756 static void test_sizes_to_segments_all_equal(void)
758 uint64_t sizes
[] = { 5, 5 };
760 struct segment
*segs
=
761 sizes_to_segments(&seglen
, sizes
, ARRAY_SIZE(sizes
));
763 EXPECT(segs
[0].start
== 0);
764 EXPECT(segs
[0].end
== 2);
768 static void test_suggest_compaction_segment(void)
770 uint64_t sizes
[] = { 128, 64, 17, 16, 9, 9, 9, 16, 16 };
771 /* .................0 1 2 3 4 5 6 */
773 suggest_compaction_segment(sizes
, ARRAY_SIZE(sizes
));
774 EXPECT(min
.start
== 2);
775 EXPECT(min
.end
== 7);
778 static void test_suggest_compaction_segment_nothing(void)
780 uint64_t sizes
[] = { 64, 32, 16, 8, 4, 2 };
781 struct segment result
=
782 suggest_compaction_segment(sizes
, ARRAY_SIZE(sizes
));
783 EXPECT(result
.start
== result
.end
);
786 static void test_reflog_expire(void)
788 char *dir
= get_tmp_dir(__LINE__
);
790 struct reftable_write_options cfg
= { 0 };
791 struct reftable_stack
*st
= NULL
;
792 struct reftable_log_record logs
[20] = { { NULL
} };
793 int N
= ARRAY_SIZE(logs
) - 1;
796 struct reftable_log_expiry_config expiry
= {
799 struct reftable_log_record log
= { NULL
};
802 err
= reftable_new_stack(&st
, dir
, cfg
);
805 for (i
= 1; i
<= N
; i
++) {
807 snprintf(buf
, sizeof(buf
), "branch%02d", i
);
809 logs
[i
].refname
= xstrdup(buf
);
810 logs
[i
].update_index
= i
;
811 logs
[i
].value_type
= REFTABLE_LOG_UPDATE
;
812 logs
[i
].value
.update
.time
= i
;
813 logs
[i
].value
.update
.new_hash
= reftable_malloc(GIT_SHA1_RAWSZ
);
814 logs
[i
].value
.update
.email
= xstrdup("identity@invalid");
815 set_test_hash(logs
[i
].value
.update
.new_hash
, i
);
818 for (i
= 1; i
<= N
; i
++) {
819 struct write_log_arg arg
= {
821 .update_index
= reftable_stack_next_update_index(st
),
823 int err
= reftable_stack_add(st
, &write_test_log
, &arg
);
827 err
= reftable_stack_compact_all(st
, NULL
);
830 err
= reftable_stack_compact_all(st
, &expiry
);
833 err
= reftable_stack_read_log(st
, logs
[9].refname
, &log
);
836 err
= reftable_stack_read_log(st
, logs
[11].refname
, &log
);
839 expiry
.min_update_index
= 15;
840 err
= reftable_stack_compact_all(st
, &expiry
);
843 err
= reftable_stack_read_log(st
, logs
[14].refname
, &log
);
846 err
= reftable_stack_read_log(st
, logs
[16].refname
, &log
);
850 reftable_stack_destroy(st
);
851 for (i
= 0; i
<= N
; i
++) {
852 reftable_log_record_release(&logs
[i
]);
855 reftable_log_record_release(&log
);
858 static int write_nothing(struct reftable_writer
*wr
, void *arg
)
860 reftable_writer_set_limits(wr
, 1, 1);
864 static void test_empty_add(void)
866 struct reftable_write_options cfg
= { 0 };
867 struct reftable_stack
*st
= NULL
;
869 char *dir
= get_tmp_dir(__LINE__
);
871 struct reftable_stack
*st2
= NULL
;
874 err
= reftable_new_stack(&st
, dir
, cfg
);
877 err
= reftable_stack_add(st
, &write_nothing
, NULL
);
880 err
= reftable_new_stack(&st2
, dir
, cfg
);
883 reftable_stack_destroy(st
);
884 reftable_stack_destroy(st2
);
887 static void test_reftable_stack_auto_compaction(void)
889 struct reftable_write_options cfg
= { 0 };
890 struct reftable_stack
*st
= NULL
;
891 char *dir
= get_tmp_dir(__LINE__
);
896 err
= reftable_new_stack(&st
, dir
, cfg
);
899 st
->disable_auto_compact
= 1; /* call manually below for coverage. */
900 for (i
= 0; i
< N
; i
++) {
902 struct reftable_ref_record ref
= {
904 .update_index
= reftable_stack_next_update_index(st
),
905 .value_type
= REFTABLE_REF_SYMREF
,
906 .value
.symref
= "master",
908 snprintf(name
, sizeof(name
), "branch%04d", i
);
910 err
= reftable_stack_add(st
, &write_test_ref
, &ref
);
913 err
= reftable_stack_auto_compact(st
);
915 EXPECT(i
< 3 || st
->merged
->stack_len
< 2 * fastlog2(i
));
918 EXPECT(reftable_stack_compaction_stats(st
)->entries_written
<
919 (uint64_t)(N
* fastlog2(N
)));
921 reftable_stack_destroy(st
);
925 static void test_reftable_stack_add_performs_auto_compaction(void)
927 struct reftable_write_options cfg
= { 0 };
928 struct reftable_stack
*st
= NULL
;
929 struct strbuf refname
= STRBUF_INIT
;
930 char *dir
= get_tmp_dir(__LINE__
);
933 err
= reftable_new_stack(&st
, dir
, cfg
);
936 for (i
= 0; i
<= n
; i
++) {
937 struct reftable_ref_record ref
= {
938 .update_index
= reftable_stack_next_update_index(st
),
939 .value_type
= REFTABLE_REF_SYMREF
,
940 .value
.symref
= "master",
944 * Disable auto-compaction for all but the last runs. Like this
945 * we can ensure that we indeed honor this setting and have
946 * better control over when exactly auto compaction runs.
948 st
->disable_auto_compact
= i
!= n
;
950 strbuf_reset(&refname
);
951 strbuf_addf(&refname
, "branch-%04d", i
);
952 ref
.refname
= refname
.buf
;
954 err
= reftable_stack_add(st
, &write_test_ref
, &ref
);
958 * The stack length should grow continuously for all runs where
959 * auto compaction is disabled. When enabled, we should merge
960 * all tables in the stack.
963 EXPECT(st
->merged
->stack_len
== i
+ 1);
965 EXPECT(st
->merged
->stack_len
== 1);
968 reftable_stack_destroy(st
);
969 strbuf_release(&refname
);
973 static void test_reftable_stack_compaction_concurrent(void)
975 struct reftable_write_options cfg
= { 0 };
976 struct reftable_stack
*st1
= NULL
, *st2
= NULL
;
977 char *dir
= get_tmp_dir(__LINE__
);
982 err
= reftable_new_stack(&st1
, dir
, cfg
);
985 for (i
= 0; i
< N
; i
++) {
987 struct reftable_ref_record ref
= {
989 .update_index
= reftable_stack_next_update_index(st1
),
990 .value_type
= REFTABLE_REF_SYMREF
,
991 .value
.symref
= "master",
993 snprintf(name
, sizeof(name
), "branch%04d", i
);
995 err
= reftable_stack_add(st1
, &write_test_ref
, &ref
);
999 err
= reftable_new_stack(&st2
, dir
, cfg
);
1002 err
= reftable_stack_compact_all(st1
, NULL
);
1005 reftable_stack_destroy(st1
);
1006 reftable_stack_destroy(st2
);
1008 EXPECT(count_dir_entries(dir
) == 2);
1012 static void unclean_stack_close(struct reftable_stack
*st
)
1014 /* break abstraction boundary to simulate unclean shutdown. */
1016 for (; i
< st
->readers_len
; i
++) {
1017 reftable_reader_free(st
->readers
[i
]);
1019 st
->readers_len
= 0;
1020 FREE_AND_NULL(st
->readers
);
1023 static void test_reftable_stack_compaction_concurrent_clean(void)
1025 struct reftable_write_options cfg
= { 0 };
1026 struct reftable_stack
*st1
= NULL
, *st2
= NULL
, *st3
= NULL
;
1027 char *dir
= get_tmp_dir(__LINE__
);
1032 err
= reftable_new_stack(&st1
, dir
, cfg
);
1035 for (i
= 0; i
< N
; i
++) {
1037 struct reftable_ref_record ref
= {
1039 .update_index
= reftable_stack_next_update_index(st1
),
1040 .value_type
= REFTABLE_REF_SYMREF
,
1041 .value
.symref
= "master",
1043 snprintf(name
, sizeof(name
), "branch%04d", i
);
1045 err
= reftable_stack_add(st1
, &write_test_ref
, &ref
);
1049 err
= reftable_new_stack(&st2
, dir
, cfg
);
1052 err
= reftable_stack_compact_all(st1
, NULL
);
1055 unclean_stack_close(st1
);
1056 unclean_stack_close(st2
);
1058 err
= reftable_new_stack(&st3
, dir
, cfg
);
1061 err
= reftable_stack_clean(st3
);
1063 EXPECT(count_dir_entries(dir
) == 2);
1065 reftable_stack_destroy(st1
);
1066 reftable_stack_destroy(st2
);
1067 reftable_stack_destroy(st3
);
1072 int stack_test_main(int argc
, const char *argv
[])
1074 RUN_TEST(test_empty_add
);
1075 RUN_TEST(test_log2
);
1076 RUN_TEST(test_names_equal
);
1077 RUN_TEST(test_parse_names
);
1078 RUN_TEST(test_read_file
);
1079 RUN_TEST(test_reflog_expire
);
1080 RUN_TEST(test_reftable_stack_add
);
1081 RUN_TEST(test_reftable_stack_add_one
);
1082 RUN_TEST(test_reftable_stack_auto_compaction
);
1083 RUN_TEST(test_reftable_stack_add_performs_auto_compaction
);
1084 RUN_TEST(test_reftable_stack_compaction_concurrent
);
1085 RUN_TEST(test_reftable_stack_compaction_concurrent_clean
);
1086 RUN_TEST(test_reftable_stack_hash_id
);
1087 RUN_TEST(test_reftable_stack_lock_failure
);
1088 RUN_TEST(test_reftable_stack_log_normalize
);
1089 RUN_TEST(test_reftable_stack_tombstone
);
1090 RUN_TEST(test_reftable_stack_transaction_api
);
1091 RUN_TEST(test_reftable_stack_transaction_api_performs_auto_compaction
);
1092 RUN_TEST(test_reftable_stack_update_index_check
);
1093 RUN_TEST(test_reftable_stack_uptodate
);
1094 RUN_TEST(test_reftable_stack_validate_refname
);
1095 RUN_TEST(test_sizes_to_segments
);
1096 RUN_TEST(test_sizes_to_segments_all_equal
);
1097 RUN_TEST(test_sizes_to_segments_empty
);
1098 RUN_TEST(test_suggest_compaction_segment
);
1099 RUN_TEST(test_suggest_compaction_segment_nothing
);