]> Sergey Matveev's repositories - public-inbox.git/blob - xt/git-http-backend.t
testcommon: add require_mods method and use it
[public-inbox.git] / xt / git-http-backend.t
1 # Copyright (C) 2016-2019 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 POSIX qw(setsid);
10 use PublicInbox::TestCommon;
11
12 my $git_dir = $ENV{GIANT_GIT_DIR};
13 plan 'skip_all' => 'GIANT_GIT_DIR not defined' unless $git_dir;
14 require_mods(qw(BSD::Resource Plack::Util Plack::Builder
15                 HTTP::Date HTTP::Status Net::HTTP));
16 my $psgi = "./t/git-http-backend.psgi";
17 my ($tmpdir, $for_destroy) = tmpdir();
18 my $err = "$tmpdir/stderr.log";
19 my $out = "$tmpdir/stdout.log";
20 my $sock = tcp_server();
21 my $host = $sock->sockhost;
22 my $port = $sock->sockport;
23 my $td;
24
25 my $get_maxrss = sub {
26         my $http = Net::HTTP->new(Host => "$host:$port");
27         ok($http, 'Net::HTTP object created for maxrss');
28         $http->write_request(GET => '/');
29         my ($code, $mess, %h) = $http->read_response_headers;
30         is($code, 200, 'success reading maxrss');
31         my $n = $http->read_entity_body(my $buf, 256);
32         ok(defined $n, 'read response body');
33         like($buf, qr/\A\d+\n\z/, 'got memory response');
34         ok(int($buf) > 0, 'got non-zero memory response');
35         int($buf);
36 };
37
38 {
39         ok($sock, 'sock created');
40         my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err", $psgi ];
41         $td = start_script($cmd, undef, { 3 => $sock });
42 }
43 my $mem_a = $get_maxrss->();
44
45 SKIP: {
46         my $max = 0;
47         my $pack;
48         my $glob = "$git_dir/objects/pack/pack-*.pack";
49         foreach my $f (glob($glob)) {
50                 my $n = -s $f;
51                 if ($n > $max) {
52                         $max = $n;
53                         $pack = $f;
54                 }
55         }
56         skip "no packs found in $git_dir" unless defined $pack;
57         if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
58                 skip "bad pack name: $pack";
59         }
60         my $url = $1;
61         my $http = Net::HTTP->new(Host => "$host:$port");
62         ok($http, 'Net::HTTP object created');
63         $http->write_request(GET => $url);
64         my ($code, $mess, %h) = $http->read_response_headers;
65         is(200, $code, 'got 200 success for pack');
66         is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
67
68         # no $http->read_entity_body, here, since we want to force buffering
69         foreach my $i (1..3) {
70                 sleep 1;
71                 my $diff = $get_maxrss->() - $mem_a;
72                 note "${diff}K memory increase after $i seconds";
73                 ok($diff < 1024, 'no bloating caused by slow dumb client');
74         }
75 }
76
77 {
78         my $c = fork;
79         if ($c == 0) {
80                 setsid();
81                 exec qw(git clone -q --mirror), "http://$host:$port/",
82                         "$tmpdir/mirror.git";
83                 die "Failed start git clone: $!\n";
84         }
85         select(undef, undef, undef, 0.1);
86         foreach my $i (1..10) {
87                 is(1, kill('STOP', -$c), 'signaled clone STOP');
88                 sleep 1;
89                 ok(kill('CONT', -$c), 'continued clone');
90                 my $diff = $get_maxrss->() - $mem_a;
91                 note "${diff}K memory increase after $i seconds";
92                 ok($diff < 2048, 'no bloating caused by slow smart client');
93         }
94         ok(kill('CONT', -$c), 'continued clone');
95         is($c, waitpid($c, 0), 'reaped wayward slow clone');
96         is($?, 0, 'clone did not error out');
97         note 'clone done, fsck-ing clone result...';
98         is(0, system("git", "--git-dir=$tmpdir/mirror.git",
99                         qw(fsck --no-progress)),
100                 'fsck did not report corruption');
101
102         my $diff = $get_maxrss->() - $mem_a;
103         note "${diff}K memory increase after smart clone";
104         ok($diff < 2048, 'no bloating caused by slow smart client');
105 }
106
107 {
108         ok($td->kill, 'killed httpd');
109         $td->join;
110 }
111
112 done_testing();