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