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