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>
4 # Ensure buffering behavior in -httpd doesn't cause runaway memory use
10 require './t/common.perl';
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)) {
18 plan skip_all => "$mod missing for git-http-backend.t" if $@;
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;
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');
43 ok($sock, 'sock created');
44 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err", $psgi ];
45 $td = start_script($cmd, undef, { 3 => $sock });
47 my $mem_a = $get_maxrss->();
52 my $glob = "$git_dir/objects/pack/pack-*.pack";
53 foreach my $f (glob($glob)) {
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";
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');
72 # no $http->read_entity_body, here, since we want to force buffering
73 foreach my $i (1..3) {
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');
85 exec qw(git clone -q --mirror), "http://$host:$port/",
87 die "Failed start git clone: $!\n";
89 select(undef, undef, undef, 0.1);
90 foreach my $i (1..10) {
91 is(1, kill('STOP', -$c), 'signaled clone STOP');
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');
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');
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');
112 ok($td->kill, 'killed httpd');