1 # Copyright (C) 2016-2021 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, $port) = tcp_host_port($sock);
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 my $cmd = [ '-httpd', '-W0', "--stdout=$out", "--stderr=$err", $psgi ];
40 $td = start_script($cmd, undef, { 3 => $sock });
42 my $mem_a = $get_maxrss->();
47 my $glob = "$git_dir/objects/pack/pack-*.pack";
48 foreach my $f (glob($glob)) {
55 skip "no packs found in $git_dir" unless defined $pack;
56 if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
57 skip "bad pack name: $pack";
60 my $http = Net::HTTP->new(Host => "$host:$port");
61 ok($http, 'Net::HTTP object created');
62 $http->write_request(GET => $url);
63 my ($code, $mess, %h) = $http->read_response_headers;
64 is(200, $code, 'got 200 success for pack');
65 is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
67 # no $http->read_entity_body, here, since we want to force buffering
68 foreach my $i (1..3) {
70 my $diff = $get_maxrss->() - $mem_a;
71 note "${diff}K memory increase after $i seconds";
72 ok($diff < 1024, 'no bloating caused by slow dumb client');
76 SKIP: { # make sure Last-Modified + If-Modified-Since works with curl
78 skip 'no description', $nr unless -f "$git_dir/description";
79 my $mtime = (stat(_))[9];
80 my $curl = which('curl');
81 skip 'curl(1) not found', $nr unless $curl;
82 my $url = "http://$host:$port/description";
83 my $dst = "$tmpdir/desc";
84 is(xsys($curl, qw(-RsSf), '-o', $dst, $url), 0, 'curl -R');
85 is((stat($dst))[9], $mtime, 'curl used remote mtime');
86 is(xsys($curl, qw(-sSf), '-z', $dst, '-o', "$dst.2", $url), 0,
88 ok(!-e "$dst.2", 'no modification, nothing retrieved');
89 utime(0, 0, $dst) or die "utime failed: $!";
90 is(xsys($curl, qw(-sSfR), '-z', $dst, '-o', "$dst.2", $url), 0,
92 ok(-e "$dst.2", 'faked modification, got new file retrieved');
99 exec qw(git clone -q --mirror), "http://$host:$port/",
100 "$tmpdir/mirror.git";
101 die "Failed start git clone: $!\n";
103 select(undef, undef, undef, 0.1);
104 foreach my $i (1..10) {
105 is(1, kill('STOP', -$c), 'signaled clone STOP');
107 ok(kill('CONT', -$c), 'continued clone');
108 my $diff = $get_maxrss->() - $mem_a;
109 note "${diff}K memory increase after $i seconds";
110 ok($diff < 2048, 'no bloating caused by slow smart client');
112 ok(kill('CONT', -$c), 'continued clone');
113 is($c, waitpid($c, 0), 'reaped wayward slow clone');
114 is($?, 0, 'clone did not error out');
115 note 'clone done, fsck-ing clone result...';
116 is(0, system("git", "--git-dir=$tmpdir/mirror.git",
117 qw(fsck --no-progress)),
118 'fsck did not report corruption');
120 my $diff = $get_maxrss->() - $mem_a;
121 note "${diff}K memory increase after smart clone";
122 ok($diff < 2048, 'no bloating caused by slow smart client');
126 ok($td->kill, 'killed httpd');