1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use File::Temp qw/tempdir/;
8 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
9 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
10 use POSIX qw(dup2 setsid);
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(Danga::Socket BSD::Resource
16 Plack::Util Plack::Builder
17 HTTP::Date HTTP::Status Net::HTTP)) {
19 plan skip_all => "$mod missing for git-http-backend.t" if $@;
21 my $psgi = getcwd()."/t/git-http-backend.psgi";
22 my $tmpdir = tempdir('pi-git-http-backend-XXXXXX', TMPDIR => 1, CLEANUP => 1);
23 my $err = "$tmpdir/stderr.log";
24 my $out = "$tmpdir/stdout.log";
25 my $httpd = 'blib/script/public-inbox-httpd';
27 LocalAddr => '127.0.0.1',
33 my $sock = IO::Socket::INET->new(%opts);
34 my $host = $sock->sockhost;
35 my $port = $sock->sockport;
37 END { kill 'TERM', $pid if defined $pid };
39 my $get_maxrss = sub {
40 my $http = Net::HTTP->new(Host => "$host:$port");
41 ok($http, 'Net::HTTP object created for maxrss');
42 $http->write_request(GET => '/');
43 my ($code, $mess, %h) = $http->read_response_headers;
44 is($code, 200, 'success reading maxrss');
45 my $n = $http->read_entity_body(my $buf, 256);
46 ok(defined $n, 'read response body');
47 like($buf, qr/\A\d+\n\z/, 'got memory response');
48 ok(int($buf) > 0, 'got non-zero memory response');
53 ok($sock, 'sock created');
55 if ($pid == 0) { # pretend to be systemd
56 fcntl($sock, F_SETFD, 0);
57 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
58 $ENV{LISTEN_PID} = $$;
60 $ENV{TEST_CHUNK} = '1';
61 exec $httpd, "--stdout=$out", "--stderr=$err", $psgi;
64 ok(defined $pid, 'forked httpd process successfully');
66 my $mem_a = $get_maxrss->();
71 my $glob = "$git_dir/objects/pack/pack-*.pack";
72 foreach my $f (glob($glob)) {
79 skip "no packs found in $git_dir" unless defined $pack;
80 if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
81 skip "bad pack name: $pack";
84 my $http = Net::HTTP->new(Host => "$host:$port");
85 ok($http, 'Net::HTTP object created');
86 $http->write_request(GET => $url);
87 my ($code, $mess, %h) = $http->read_response_headers;
88 is(200, $code, 'got 200 success for pack');
89 is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
90 foreach my $i (1..3) {
92 my $diff = $get_maxrss->() - $mem_a;
93 note "${diff}K memory increase after $i seconds";
94 ok($diff < 1024, 'no bloating caused by slow dumb client');
102 exec qw(git clone -q --mirror), "http://$host:$port/",
103 "$tmpdir/mirror.git";
104 die "Failed start git clone: $!\n";
106 select(undef, undef, undef, 0.1);
107 foreach my $i (1..10) {
108 is(1, kill('STOP', -$c), 'signaled clone STOP');
110 ok(kill('CONT', -$c), 'continued clone');
111 my $diff = $get_maxrss->() - $mem_a;
112 note "${diff}K memory increase after $i seconds";
113 ok($diff < 2048, 'no bloating caused by slow smart client');
115 ok(kill('CONT', -$c), 'continued clone');
116 is($c, waitpid($c, 0), 'reaped wayward slow clone');
117 is($?, 0, 'clone did not error out');
118 note 'clone done, fsck-ing clone result...';
119 is(0, system("git", "--git-dir=$tmpdir/mirror.git",
120 qw(fsck --no-progress)),
121 'fsck did not report corruption');
123 my $diff = $get_maxrss->() - $mem_a;
124 note "${diff}K memory increase after smart clone";
125 ok($diff < 2048, 'no bloating caused by slow smart client');
129 ok(kill('TERM', $pid), 'killed httpd');