1 # Copyright (C) 2016-2020 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;
11 use PublicInbox::Spawn qw(which);
13 my $git_dir = $ENV{GIANT_GIT_DIR};
14 plan 'skip_all' => 'GIANT_GIT_DIR not defined' unless $git_dir;
15 require_mods(qw(BSD::Resource Plack::Util Plack::Builder
16 HTTP::Date HTTP::Status Net::HTTP));
17 my $psgi = "./t/git-http-backend.psgi";
18 my ($tmpdir, $for_destroy) = tmpdir();
19 my $err = "$tmpdir/stderr.log";
20 my $out = "$tmpdir/stdout.log";
21 my $sock = tcp_server();
22 my $host = $sock->sockhost;
23 my $port = $sock->sockport;
26 my $get_maxrss = sub {
27 my $http = Net::HTTP->new(Host => "$host:$port");
28 ok($http, 'Net::HTTP object created for maxrss');
29 $http->write_request(GET => '/');
30 my ($code, $mess, %h) = $http->read_response_headers;
31 is($code, 200, 'success reading maxrss');
32 my $n = $http->read_entity_body(my $buf, 256);
33 ok(defined $n, 'read response body');
34 like($buf, qr/\A\d+\n\z/, 'got memory response');
35 ok(int($buf) > 0, 'got non-zero memory response');
40 ok($sock, 'sock created');
41 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err", $psgi ];
42 $td = start_script($cmd, undef, { 3 => $sock });
44 my $mem_a = $get_maxrss->();
49 my $glob = "$git_dir/objects/pack/pack-*.pack";
50 foreach my $f (glob($glob)) {
57 skip "no packs found in $git_dir" unless defined $pack;
58 if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
59 skip "bad pack name: $pack";
62 my $http = Net::HTTP->new(Host => "$host:$port");
63 ok($http, 'Net::HTTP object created');
64 $http->write_request(GET => $url);
65 my ($code, $mess, %h) = $http->read_response_headers;
66 is(200, $code, 'got 200 success for pack');
67 is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
69 # no $http->read_entity_body, here, since we want to force buffering
70 foreach my $i (1..3) {
72 my $diff = $get_maxrss->() - $mem_a;
73 note "${diff}K memory increase after $i seconds";
74 ok($diff < 1024, 'no bloating caused by slow dumb client');
78 SKIP: { # make sure Last-Modified + If-Modified-Since works with curl
80 skip 'no description', $nr unless -f "$git_dir/description";
81 my $mtime = (stat(_))[9];
82 my $curl = which('curl');
83 skip 'curl(1) not found', $nr unless $curl;
84 my $url = "http://$host:$port/description";
85 my $dst = "$tmpdir/desc";
86 is(xsys($curl, qw(-RsSf), '-o', $dst, $url), 0, 'curl -R');
87 is((stat($dst))[9], $mtime, 'curl used remote mtime');
88 is(xsys($curl, qw(-sSf), '-z', $dst, '-o', "$dst.2", $url), 0,
90 ok(!-e "$dst.2", 'no modification, nothing retrieved');
91 utime(0, 0, $dst) or die "utime failed: $!";
92 is(xsys($curl, qw(-sSfR), '-z', $dst, '-o', "$dst.2", $url), 0,
94 ok(-e "$dst.2", 'faked modification, got new file retrieved');
101 exec qw(git clone -q --mirror), "http://$host:$port/",
102 "$tmpdir/mirror.git";
103 die "Failed start git clone: $!\n";
105 select(undef, undef, undef, 0.1);
106 foreach my $i (1..10) {
107 is(1, kill('STOP', -$c), 'signaled clone STOP');
109 ok(kill('CONT', -$c), 'continued clone');
110 my $diff = $get_maxrss->() - $mem_a;
111 note "${diff}K memory increase after $i seconds";
112 ok($diff < 2048, 'no bloating caused by slow smart client');
114 ok(kill('CONT', -$c), 'continued clone');
115 is($c, waitpid($c, 0), 'reaped wayward slow clone');
116 is($?, 0, 'clone did not error out');
117 note 'clone done, fsck-ing clone result...';
118 is(0, system("git", "--git-dir=$tmpdir/mirror.git",
119 qw(fsck --no-progress)),
120 'fsck did not report corruption');
122 my $diff = $get_maxrss->() - $mem_a;
123 note "${diff}K memory increase after smart clone";
124 ok($diff < 2048, 'no bloating caused by slow smart client');
128 ok($td->kill, 'killed httpd');