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