]> Sergey Matveev's repositories - public-inbox.git/blob - t/git-http-backend.t
4e51f2bf6bfc88d2b7eac758ebf32ce12db1fb00
[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 use strict;
4 use warnings;
5 use Test::More;
6 use File::Temp qw/tempdir/;
7 use IO::Socket;
8 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
9 use POSIX qw(dup2 setsid);
10 use Cwd qw(getcwd);
11
12 my $git_dir = $ENV{GIANT_GIT_DIR};
13 plan 'skip_all' => 'GIANT_GIT_DIR not defined' unless $git_dir;
14 foreach my $mod (qw(Danga::Socket BSD::Resource
15                         Plack::Util Plack::Builder
16                         HTTP::Date HTTP::Status Net::HTTP)) {
17         eval "require $mod";
18         plan skip_all => "$mod missing for git-http-backend.t" if $@;
19 }
20 require './t/common.perl';
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';
26 my %opts = (
27         LocalAddr => '127.0.0.1',
28         ReuseAddr => 1,
29         Proto => 'tcp',
30         Type => SOCK_STREAM,
31         Listen => 1024,
32 );
33 my $sock = IO::Socket::INET->new(%opts);
34 my $host = $sock->sockhost;
35 my $port = $sock->sockport;
36 my $pid;
37 END { kill 'TERM', $pid if defined $pid };
38
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');
49         int($buf);
50 };
51
52 {
53         ok($sock, 'sock created');
54         my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err", $psgi ];
55         ok(defined($pid = spawn_listener(undef, $cmd, [$sock])),
56            'forked httpd process successfully');
57 }
58 my $mem_a = $get_maxrss->();
59
60 SKIP: {
61         my $max = 0;
62         my $pack;
63         my $glob = "$git_dir/objects/pack/pack-*.pack";
64         foreach my $f (glob($glob)) {
65                 my $n = -s $f;
66                 if ($n > $max) {
67                         $max = $n;
68                         $pack = $f;
69                 }
70         }
71         skip "no packs found in $git_dir" unless defined $pack;
72         if ($pack !~ m!(/objects/pack/pack-[a-f0-9]{40}.pack)\z!) {
73                 skip "bad pack name: $pack";
74         }
75         my $url = $1;
76         my $http = Net::HTTP->new(Host => "$host:$port");
77         ok($http, 'Net::HTTP object created');
78         $http->write_request(GET => $url);
79         my ($code, $mess, %h) = $http->read_response_headers;
80         is(200, $code, 'got 200 success for pack');
81         is($max, $h{'Content-Length'}, 'got expected Content-Length for pack');
82         foreach my $i (1..3) {
83                 sleep 1;
84                 my $diff = $get_maxrss->() - $mem_a;
85                 note "${diff}K memory increase after $i seconds";
86                 ok($diff < 1024, 'no bloating caused by slow dumb client');
87         }
88 }
89
90 {
91         my $c = fork;
92         if ($c == 0) {
93                 setsid();
94                 exec qw(git clone -q --mirror), "http://$host:$port/",
95                         "$tmpdir/mirror.git";
96                 die "Failed start git clone: $!\n";
97         }
98         select(undef, undef, undef, 0.1);
99         foreach my $i (1..10) {
100                 is(1, kill('STOP', -$c), 'signaled clone STOP');
101                 sleep 1;
102                 ok(kill('CONT', -$c), 'continued clone');
103                 my $diff = $get_maxrss->() - $mem_a;
104                 note "${diff}K memory increase after $i seconds";
105                 ok($diff < 2048, 'no bloating caused by slow smart client');
106         }
107         ok(kill('CONT', -$c), 'continued clone');
108         is($c, waitpid($c, 0), 'reaped wayward slow clone');
109         is($?, 0, 'clone did not error out');
110         note 'clone done, fsck-ing clone result...';
111         is(0, system("git", "--git-dir=$tmpdir/mirror.git",
112                         qw(fsck --no-progress)),
113                 'fsck did not report corruption');
114
115         my $diff = $get_maxrss->() - $mem_a;
116         note "${diff}K memory increase after smart clone";
117         ok($diff < 2048, 'no bloating caused by slow smart client');
118 }
119
120 {
121         ok(kill('TERM', $pid), 'killed httpd');
122         $pid = undef;
123         waitpid(-1, 0);
124 }
125
126 done_testing();