usage: report vsnprintf(3) failure
[git.git] / reftable / stack_test.c
blob509f4866236024f5546100da7121996f1c963e08
1 /*
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
7 */
9 #include "stack.h"
11 #include "system.h"
13 #include "reftable-reader.h"
14 #include "merged.h"
15 #include "basics.h"
16 #include "record.h"
17 #include "test_framework.h"
18 #include "reftable-tests.h"
19 #include "reader.h"
21 #include <sys/types.h>
22 #include <dirent.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);
35 int len = 0;
36 struct dirent *d;
37 if (!dir)
38 return 0;
40 while ((d = readdir(dir))) {
41 if (!strcmp(d->d_name, "..") || !strcmp(d->d_name, "."))
42 continue;
43 len++;
45 closedir(dir);
46 return len;
50 * Work linenumber into the tempdir, so we can see which tests forget to
51 * cleanup.
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);
59 return template;
62 static char *get_tmp_dir(int linenumber)
64 char *dir = get_tmp_template(linenumber);
65 EXPECT(mkdtemp(dir));
66 return dir;
69 static void test_read_file(void)
71 char *fn = get_tmp_template(__LINE__);
72 int fd = mkstemp(fn);
73 char out[1024] = "line1\n\nline2\nline3";
74 int n, err;
75 char **names = NULL;
76 char *want[] = { "line1", "line2", "line3" };
77 int i = 0;
79 EXPECT(fd > 0);
80 n = write_in_full(fd, out, strlen(out));
81 EXPECT(n == strlen(out));
82 err = close(fd);
83 EXPECT(err >= 0);
85 err = read_lines(fn, &names);
86 EXPECT_ERR(err);
88 for (i = 0; names[i]; i++) {
89 EXPECT(0 == strcmp(want[i], names[i]));
91 free_names(names);
92 (void) remove(fn);
95 static void test_parse_names(void)
97 char buf[] = "line\n";
98 char **names = NULL;
99 parse_names(buf, strlen(buf), &names);
101 EXPECT(NULL != names[0]);
102 EXPECT(0 == strcmp(names[0], "line"));
103 EXPECT(NULL == names[1]);
104 free_names(names);
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;
147 int err;
148 struct reftable_ref_record ref = {
149 .refname = "HEAD",
150 .update_index = 1,
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);
157 EXPECT_ERR(err);
159 err = reftable_stack_add(st, &write_test_ref, &ref);
160 EXPECT_ERR(err);
162 err = reftable_stack_read_ref(st, ref.refname, &dest);
163 EXPECT_ERR(err);
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);
169 EXPECT_ERR(err);
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);
178 EXPECT(!err);
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);
187 EXPECT(!err);
188 EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
189 #else
190 (void) stat_result;
191 #endif
193 reftable_ref_record_release(&dest);
194 reftable_stack_destroy(st);
195 strbuf_release(&scratch);
196 clear_dir(dir);
197 umask(mask);
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__);
207 int err;
208 struct reftable_ref_record ref1 = {
209 .refname = "HEAD",
210 .update_index = 1,
211 .value_type = REFTABLE_REF_SYMREF,
212 .value.symref = "master",
214 struct reftable_ref_record ref2 = {
215 .refname = "branch2",
216 .update_index = 2,
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);
226 EXPECT_ERR(err);
228 err = reftable_new_stack(&st2, dir, cfg);
229 EXPECT_ERR(err);
231 err = reftable_stack_add(st1, &write_test_ref, &ref1);
232 EXPECT_ERR(err);
234 err = reftable_stack_add(st2, &write_test_ref, &ref2);
235 EXPECT(err == REFTABLE_LOCK_ERROR);
237 err = reftable_stack_reload(st2);
238 EXPECT_ERR(err);
240 err = reftable_stack_add(st2, &write_test_ref, &ref2);
241 EXPECT_ERR(err);
242 reftable_stack_destroy(st1);
243 reftable_stack_destroy(st2);
244 clear_dir(dir);
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;
253 int err;
254 struct reftable_addition *add = NULL;
256 struct reftable_ref_record ref = {
257 .refname = "HEAD",
258 .update_index = 1,
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);
266 EXPECT_ERR(err);
268 reftable_addition_destroy(add);
270 err = reftable_stack_new_addition(&add, st);
271 EXPECT_ERR(err);
273 err = reftable_addition_add(add, &write_test_ref, &ref);
274 EXPECT_ERR(err);
276 err = reftable_addition_commit(add);
277 EXPECT_ERR(err);
279 reftable_addition_destroy(add);
281 err = reftable_stack_read_ref(st, ref.refname, &dest);
282 EXPECT_ERR(err);
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);
288 clear_dir(dir);
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;
297 int i, n = 20, err;
299 err = reftable_new_stack(&st, dir, cfg);
300 EXPECT_ERR(err);
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",
308 char name[100];
310 snprintf(name, sizeof(name), "branch%04d", i);
311 ref.refname = name;
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);
321 EXPECT_ERR(err);
323 err = reftable_addition_add(add, &write_test_ref, &ref);
324 EXPECT_ERR(err);
326 err = reftable_addition_commit(add);
327 EXPECT_ERR(err);
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.
336 if (i != n)
337 EXPECT(st->merged->stack_len == i + 1);
338 else
339 EXPECT(st->merged->stack_len == 1);
342 reftable_stack_destroy(st);
343 clear_dir(dir);
346 static void test_reftable_stack_validate_refname(void)
348 struct reftable_write_options cfg = { 0 };
349 struct reftable_stack *st = NULL;
350 int err;
351 char *dir = get_tmp_dir(__LINE__);
353 int i;
354 struct reftable_ref_record ref = {
355 .refname = "a/b",
356 .update_index = 1,
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);
363 EXPECT_ERR(err);
365 err = reftable_stack_add(st, &write_test_ref, &ref);
366 EXPECT_ERR(err);
368 for (i = 0; i < ARRAY_SIZE(additions); i++) {
369 struct reftable_ref_record ref = {
370 .refname = additions[i],
371 .update_index = 1,
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);
381 clear_dir(dir);
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;
395 int err;
396 struct reftable_ref_record ref1 = {
397 .refname = "name1",
398 .update_index = 1,
399 .value_type = REFTABLE_REF_SYMREF,
400 .value.symref = "master",
402 struct reftable_ref_record ref2 = {
403 .refname = "name2",
404 .update_index = 1,
405 .value_type = REFTABLE_REF_SYMREF,
406 .value.symref = "master",
409 err = reftable_new_stack(&st, dir, cfg);
410 EXPECT_ERR(err);
412 err = reftable_stack_add(st, &write_test_ref, &ref1);
413 EXPECT_ERR(err);
415 err = reftable_stack_add(st, &write_test_ref, &ref2);
416 EXPECT(err == REFTABLE_API_ERROR);
417 reftable_stack_destroy(st);
418 clear_dir(dir);
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;
427 int err, i;
429 err = reftable_new_stack(&st, dir, cfg);
430 EXPECT_ERR(err);
431 for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) {
432 err = reftable_stack_add(st, &write_error, &i);
433 EXPECT(err == i);
436 reftable_stack_destroy(st);
437 clear_dir(dir);
440 static void test_reftable_stack_add(void)
442 int i = 0;
443 int err = 0;
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);
457 EXPECT_ERR(err);
458 st->disable_auto_compact = 1;
460 for (i = 0; i < N; i++) {
461 char buf[256];
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]);
479 EXPECT_ERR(err);
482 for (i = 0; i < N; i++) {
483 struct write_log_arg arg = {
484 .log = &logs[i],
485 .update_index = reftable_stack_next_update_index(st),
487 int err = reftable_stack_add(st, &write_test_log, &arg);
488 EXPECT_ERR(err);
491 err = reftable_stack_compact_all(st, NULL);
492 EXPECT_ERR(err);
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);
498 EXPECT_ERR(err);
499 EXPECT(reftable_ref_record_equal(&dest, refs + i,
500 GIT_SHA1_RAWSZ));
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);
507 EXPECT_ERR(err);
508 EXPECT(reftable_log_record_equal(&dest, logs + i,
509 GIT_SHA1_RAWSZ));
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);
517 EXPECT(!err);
518 EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
520 strbuf_reset(&path);
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);
526 EXPECT(!err);
527 EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions);
528 #else
529 (void) stat_result;
530 #endif
532 /* cleanup */
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);
539 clear_dir(dir);
542 static void test_reftable_stack_log_normalize(void)
544 int err = 0;
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",
554 .update_index = 1,
555 .value_type = REFTABLE_LOG_UPDATE,
556 .value = { .update = {
557 .new_hash = h1,
558 .old_hash = h2,
559 } } };
560 struct reftable_log_record dest = {
561 .update_index = 0,
563 struct write_log_arg arg = {
564 .log = &input,
565 .update_index = 1,
568 err = reftable_new_stack(&st, dir, cfg);
569 EXPECT_ERR(err);
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);
577 EXPECT_ERR(err);
579 err = reftable_stack_read_log(st, input.refname, &dest);
580 EXPECT_ERR(err);
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);
586 EXPECT_ERR(err);
587 err = reftable_stack_read_log(st, input.refname, &dest);
588 EXPECT_ERR(err);
589 EXPECT(0 == strcmp(dest.value.update.message, "two\n"));
591 /* cleanup */
592 reftable_stack_destroy(st);
593 reftable_log_record_release(&dest);
594 clear_dir(dir);
597 static void test_reftable_stack_tombstone(void)
599 int i = 0;
600 char *dir = get_tmp_dir(__LINE__);
602 struct reftable_write_options cfg = { 0 };
603 struct reftable_stack *st = NULL;
604 int err;
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);
613 EXPECT_ERR(err);
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;
620 if (i % 2 == 0) {
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;
628 if (i % 2 == 0) {
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]);
639 EXPECT_ERR(err);
642 for (i = 0; i < N; i++) {
643 struct write_log_arg arg = {
644 .log = &logs[i],
645 .update_index = reftable_stack_next_update_index(st),
647 int err = reftable_stack_add(st, &write_test_log, &arg);
648 EXPECT_ERR(err);
651 err = reftable_stack_read_ref(st, "branch", &dest);
652 EXPECT(err == 1);
653 reftable_ref_record_release(&dest);
655 err = reftable_stack_read_log(st, "branch", &log_dest);
656 EXPECT(err == 1);
657 reftable_log_record_release(&log_dest);
659 err = reftable_stack_compact_all(st, NULL);
660 EXPECT_ERR(err);
662 err = reftable_stack_read_ref(st, "branch", &dest);
663 EXPECT(err == 1);
665 err = reftable_stack_read_log(st, "branch", &log_dest);
666 EXPECT(err == 1);
667 reftable_ref_record_release(&dest);
668 reftable_log_record_release(&log_dest);
670 /* cleanup */
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]);
676 clear_dir(dir);
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;
685 int err;
687 struct reftable_ref_record ref = {
688 .refname = "master",
689 .value_type = REFTABLE_REF_SYMREF,
690 .value.symref = "target",
691 .update_index = 1,
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);
700 EXPECT_ERR(err);
702 err = reftable_stack_add(st, &write_test_ref, &ref);
703 EXPECT_ERR(err);
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);
711 EXPECT_ERR(err);
713 err = reftable_stack_read_ref(st_default, "master", &dest);
714 EXPECT_ERR(err);
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);
720 clear_dir(dir);
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 */
735 size_t seglen = 0;
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);
745 reftable_free(segs);
748 static void test_sizes_to_segments_empty(void)
750 size_t seglen = 0;
751 struct segment *segs = sizes_to_segments(&seglen, NULL, 0);
752 EXPECT(seglen == 0);
753 reftable_free(segs);
756 static void test_sizes_to_segments_all_equal(void)
758 uint64_t sizes[] = { 5, 5 };
759 size_t seglen = 0;
760 struct segment *segs =
761 sizes_to_segments(&seglen, sizes, ARRAY_SIZE(sizes));
762 EXPECT(seglen == 1);
763 EXPECT(segs[0].start == 0);
764 EXPECT(segs[0].end == 2);
765 reftable_free(segs);
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 */
772 struct segment min =
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;
794 int i = 0;
795 int err;
796 struct reftable_log_expiry_config expiry = {
797 .time = 10,
799 struct reftable_log_record log = { NULL };
802 err = reftable_new_stack(&st, dir, cfg);
803 EXPECT_ERR(err);
805 for (i = 1; i <= N; i++) {
806 char buf[256];
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 = {
820 .log = &logs[i],
821 .update_index = reftable_stack_next_update_index(st),
823 int err = reftable_stack_add(st, &write_test_log, &arg);
824 EXPECT_ERR(err);
827 err = reftable_stack_compact_all(st, NULL);
828 EXPECT_ERR(err);
830 err = reftable_stack_compact_all(st, &expiry);
831 EXPECT_ERR(err);
833 err = reftable_stack_read_log(st, logs[9].refname, &log);
834 EXPECT(err == 1);
836 err = reftable_stack_read_log(st, logs[11].refname, &log);
837 EXPECT_ERR(err);
839 expiry.min_update_index = 15;
840 err = reftable_stack_compact_all(st, &expiry);
841 EXPECT_ERR(err);
843 err = reftable_stack_read_log(st, logs[14].refname, &log);
844 EXPECT(err == 1);
846 err = reftable_stack_read_log(st, logs[16].refname, &log);
847 EXPECT_ERR(err);
849 /* cleanup */
850 reftable_stack_destroy(st);
851 for (i = 0; i <= N; i++) {
852 reftable_log_record_release(&logs[i]);
854 clear_dir(dir);
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);
861 return 0;
864 static void test_empty_add(void)
866 struct reftable_write_options cfg = { 0 };
867 struct reftable_stack *st = NULL;
868 int err;
869 char *dir = get_tmp_dir(__LINE__);
871 struct reftable_stack *st2 = NULL;
874 err = reftable_new_stack(&st, dir, cfg);
875 EXPECT_ERR(err);
877 err = reftable_stack_add(st, &write_nothing, NULL);
878 EXPECT_ERR(err);
880 err = reftable_new_stack(&st2, dir, cfg);
881 EXPECT_ERR(err);
882 clear_dir(dir);
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__);
893 int err, i;
894 int N = 100;
896 err = reftable_new_stack(&st, dir, cfg);
897 EXPECT_ERR(err);
899 st->disable_auto_compact = 1; /* call manually below for coverage. */
900 for (i = 0; i < N; i++) {
901 char name[100];
902 struct reftable_ref_record ref = {
903 .refname = name,
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);
911 EXPECT_ERR(err);
913 err = reftable_stack_auto_compact(st);
914 EXPECT_ERR(err);
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);
922 clear_dir(dir);
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__);
931 int err, i, n = 20;
933 err = reftable_new_stack(&st, dir, cfg);
934 EXPECT_ERR(err);
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);
955 EXPECT_ERR(err);
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.
962 if (i != n)
963 EXPECT(st->merged->stack_len == i + 1);
964 else
965 EXPECT(st->merged->stack_len == 1);
968 reftable_stack_destroy(st);
969 strbuf_release(&refname);
970 clear_dir(dir);
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__);
979 int err, i;
980 int N = 3;
982 err = reftable_new_stack(&st1, dir, cfg);
983 EXPECT_ERR(err);
985 for (i = 0; i < N; i++) {
986 char name[100];
987 struct reftable_ref_record ref = {
988 .refname = name,
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);
996 EXPECT_ERR(err);
999 err = reftable_new_stack(&st2, dir, cfg);
1000 EXPECT_ERR(err);
1002 err = reftable_stack_compact_all(st1, NULL);
1003 EXPECT_ERR(err);
1005 reftable_stack_destroy(st1);
1006 reftable_stack_destroy(st2);
1008 EXPECT(count_dir_entries(dir) == 2);
1009 clear_dir(dir);
1012 static void unclean_stack_close(struct reftable_stack *st)
1014 /* break abstraction boundary to simulate unclean shutdown. */
1015 int i = 0;
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__);
1029 int err, i;
1030 int N = 3;
1032 err = reftable_new_stack(&st1, dir, cfg);
1033 EXPECT_ERR(err);
1035 for (i = 0; i < N; i++) {
1036 char name[100];
1037 struct reftable_ref_record ref = {
1038 .refname = name,
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);
1046 EXPECT_ERR(err);
1049 err = reftable_new_stack(&st2, dir, cfg);
1050 EXPECT_ERR(err);
1052 err = reftable_stack_compact_all(st1, NULL);
1053 EXPECT_ERR(err);
1055 unclean_stack_close(st1);
1056 unclean_stack_close(st2);
1058 err = reftable_new_stack(&st3, dir, cfg);
1059 EXPECT_ERR(err);
1061 err = reftable_stack_clean(st3);
1062 EXPECT_ERR(err);
1063 EXPECT(count_dir_entries(dir) == 2);
1065 reftable_stack_destroy(st1);
1066 reftable_stack_destroy(st2);
1067 reftable_stack_destroy(st3);
1069 clear_dir(dir);
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);
1100 return 0;