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 use PublicInbox::TestCommon;
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;
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');
39 ok($sock, 'sock created');
40 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err", $psgi ];
41 $td = start_script($cmd, undef, { 3 => $sock });
43 my $mem_a = $get_maxrss->();
48 my $glob = "$git_dir/objects/pack/pack-*.pack";
49 foreach my $f (glob($glob)) {
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";
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');
68 # no $http->read_entity_body, here, since we want to force buffering
69 foreach my $i (1..3) {
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');
81 exec qw(git clone -q --mirror), "http://$host:$port/",
83 die "Failed start git clone: $!\n";
85 select(undef, undef, undef, 0.1);
86 foreach my $i (1..10) {
87 is(1, kill('STOP', -$c), 'signaled clone STOP');
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');
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');
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');
108 ok($td->kill, 'killed httpd');