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