]> Sergey Matveev's repositories - public-inbox.git/blob - t/gcf2_client.t
gcf2: transparently retry on missing OID
[public-inbox.git] / t / gcf2_client.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use PublicInbox::TestCommon;
6 use Test::More;
7 use Cwd qw(getcwd);
8 use PublicInbox::Import;
9
10 require_mods('PublicInbox::Gcf2');
11 use_ok 'PublicInbox::Gcf2Client';
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $git_a = "$tmpdir/a.git";
14 my $git_b = "$tmpdir/b.git";
15 PublicInbox::Import::init_bare($git_a);
16 PublicInbox::Import::init_bare($git_b);
17 my $fi_data = './t/git.fast-import-data';
18 my $rdr = {};
19 open $rdr->{0}, '<', $fi_data or BAIL_OUT $!;
20 xsys([qw(git fast-import --quiet)], { GIT_DIR => $git_a }, $rdr);
21 is($?, 0, 'fast-import succeeded');
22
23 my $tree = 'fdbc43725f21f485051c17463b50185f4c3cf88c';
24 my $called = 0;
25 my $err_f = "$tmpdir/err";
26 {
27         local $ENV{PATH} = getcwd()."/blib/script:$ENV{PATH}";
28         open my $err, '>', $err_f or BAIL_OUT $!;
29         my $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
30         $gcf2c->add_git_dir($git_a);
31
32         $gcf2c->cat_async($tree, sub {
33                 my ($bref, $oid, $type, $size, $arg) = @_;
34                 is($oid, $tree, 'got expected OID');
35                 is($size, 30, 'got expected length');
36                 is($type, 'tree', 'got tree type');
37                 is(length($$bref), 30, 'got a tree');
38                 is($arg, 'hi', 'arg passed');
39                 $called++;
40         }, 'hi');
41         $gcf2c->cat_async_wait;
42
43         open $err, '<', $err_f or BAIL_OUT $!;
44         my $estr = do { local $/; <$err> };
45         is($estr, '', 'nothing in stderr');
46
47         my $trunc = substr($tree, 0, 39);
48         $gcf2c->cat_async($trunc, sub {
49                 my ($bref, $oid, $type, $size, $arg) = @_;
50                 is(undef, $bref, 'missing bref is undef');
51                 is($oid, $trunc, 'truncated OID printed');
52                 is($type, 'missing', 'type is "missing"');
53                 is($size, undef, 'size is undef');
54                 is($arg, 'bye', 'arg passed when missing');
55                 $called++;
56         }, 'bye');
57         $gcf2c->cat_async_wait;
58
59         open $err, '<', $err_f or BAIL_OUT $!;
60         $estr = do { local $/; <$err> };
61         like($estr, qr/retrying/, 'warned about retry');
62
63         # try failed alternates lookup
64         open $err, '>', $err_f or BAIL_OUT $!;
65         $gcf2c = PublicInbox::Gcf2Client::new({ 2 => $err });
66         $gcf2c->add_git_dir($git_b);
67         $gcf2c->cat_async($tree, sub {
68                 my ($bref, $oid, $type, $size, $arg) = @_;
69                 is(undef, $bref, 'missing bref from alt is undef');
70                 $called++;
71         });
72         $gcf2c->cat_async_wait;
73         open $err, '<', $err_f or BAIL_OUT $!;
74         $estr = do { local $/; <$err> };
75         like($estr, qr/retrying/, 'warned about retry before alt update');
76
77         # now try successful alternates lookup
78         open my $alt, '>>', "$git_b/objects/info/alternates" or BAIL_OUT $!;
79         print $alt "$git_a/objects\n" or BAIL_OUT $!;
80         close $alt or BAIL_OUT;
81         my $expect = xqx(['git', "--git-dir=$git_a", qw(cat-file tree), $tree]);
82         $gcf2c->cat_async($tree, sub {
83                 my ($bref, $oid, $type, $size, $arg) = @_;
84                 is($oid, $tree, 'oid match on alternates retry');
85                 is($$bref, $expect, 'tree content matched');
86                 $called++;
87         });
88         $gcf2c->cat_async_wait;
89 }
90 is($called, 4, 'cat_async callbacks hit');
91 done_testing;