]> Sergey Matveev's repositories - public-inbox.git/blob - t/git-http-backend.t
Merge remote-tracking branch 'origin/manifest' into next
[public-inbox.git] / t / git-http-backend.t
1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use File::Temp qw/tempdir/;
7 use IO::Socket::INET;
8 use POSIX qw(setsid);
9
10 my $git_dir = $ENV{GIANT_GIT_DIR};
11 plan 'skip_all' => 'GIANT_GIT_DIR not defined' unless $git_dir;
12 foreach my $mod (qw(BSD::Resource
13                         Plack::Util Plack::Builder
14                         HTTP::Date HTTP::Status Net::HTTP)) {
15         eval "require $mod";
16         plan skip_all => "$mod missing for git-http-backend.t" if $@;
17 }
18 require './t/common.perl';
19 my $psgi = "./t/git-http-backend.psgi";
20 my $tmpdir = tempdir('pi-git-http-backend-XXXXXX', TMPDIR => 1, CLEANUP => 1);
21 my $err = "$tmpdir/stderr.log";
22 my $out = "$tmpdir/stdout.log";
23 my $httpd = 'blib/script/public-inbox-httpd';
24 my %opts = (
25         LocalAddr => '127.0.0.1',
26         ReuseAddr => 1,
27         Proto => 'tcp',
28         Type => SOCK_STREAM,
29         Listen => 1024,
30 );
31 my $sock = IO::Socket::INET->new(%opts);
32 my $host = $sock->sockhost;
33 my $port = $sock->sockport;
34 my $pid;
35 END { kill 'TERM', $pid if defined $pid };
36
37 my $get_maxrss = sub {
38         my $http = Net::HTTP->new(Host => "$host:$port");
39         ok($http, 'Net::HTTP object created for maxrss');
40         $http->write_request(GET => '/');
41         my ($code, $mess, %h) = $http->read_response_headers;
42         is($code, 200, 'success reading maxrss');
43         my $n = $http->read_entity_body(my $buf, 256);
44         ok(defined $n, 'read response body');
45         like($buf, qr/\A\d+\n\z/, 'got memory response');
46         ok(int($buf) > 0, 'got non-zero memory response');
47         int($buf);
48 };
49
50 {
51         ok($sock, 'sock created');
52         my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err", $psgi ];
53         ok(defined($pid = spawn_listener(undef, $cmd, [$sock])),
54            'forked httpd process successfully');
55 }
56 my $mem_a = $get_maxrss->();
57
58 SKIP: {
59         my $max = 0;
60         my $pack;
61         my $glob = "$git_dir/objects/pack/pack-*.pack";
62         foreach my $f (glob($glob)) {
63                 my $n = -s $f;
64                 if ($n > $max) {
65                         $max = $n;
66                         $pack = $f;
67                 }
68         }
69         skip "no packs found in $git_dir" unless defined $pack;
70         if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
71                 skip "bad pack name: $pack";
72         }
73         my $url = $1;
74         my $http = Net::HTTP->new(Host => "$host:$port");
75         ok($http, 'Net::HTTP object created');
76         $http->write_request(GET => $url);
77         my ($code, $mess, %h) = $http->read_response_headers;
78         is(200, $code, 'got 200 success for pack');
79         is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
80         foreach my $i (1..3) {
81                 sleep 1;
82                 my $diff = $get_maxrss->() - $mem_a;
83                 note "${diff}K memory increase after $i seconds";
84                 ok($diff < 1024, 'no bloating caused by slow dumb client');
85         }
86 }
87
88 {
89         my $c = fork;
90         if ($c == 0) {
91                 setsid();
92                 exec qw(git clone -q --mirror), "http://$host:$port/",
93                         "$tmpdir/mirror.git";
94                 die "Failed start git clone: $!\n";
95         }
96         select(undef, undef, undef, 0.1);
97         foreach my $i (1..10) {
98                 is(1, kill('STOP', -$c), 'signaled clone STOP');
99                 sleep 1;
100                 ok(kill('CONT', -$c), 'continued clone');
101                 my $diff = $get_maxrss->() - $mem_a;
102                 note "${diff}K memory increase after $i seconds";
103                 ok($diff < 2048, 'no bloating caused by slow smart client');
104         }
105         ok(kill('CONT', -$c), 'continued clone');
106         is($c, waitpid($c, 0), 'reaped wayward slow clone');
107         is($?, 0, 'clone did not error out');
108         note 'clone done, fsck-ing clone result...';
109         is(0, system("git", "--git-dir=$tmpdir/mirror.git",
110                         qw(fsck --no-progress)),
111                 'fsck did not report corruption');
112
113         my $diff = $get_maxrss->() - $mem_a;
114         note "${diff}K memory increase after smart clone";
115         ok($diff < 2048, 'no bloating caused by slow smart client');
116 }
117
118 {
119         ok(kill('TERM', $pid), 'killed httpd');
120         $pid = undef;
121         waitpid(-1, 0);
122 }
123
124 done_testing();