lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / httpd-unix.t
blob0b620bd6eea41693aa71cf9c8525443e841fafe8
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Tests for binding Unix domain sockets
5 use v5.12;
6 use PublicInbox::TestCommon;
7 use Errno qw(EADDRINUSE);
8 use Cwd qw(abs_path);
9 use Carp qw(croak);
10 use autodie qw(close);
11 require_mods(qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status));
12 use IO::Socket::UNIX;
13 use POSIX qw(mkfifo);
14 require PublicInbox::Sigfd;
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $unix = "$tmpdir/unix.sock";
17 my $psgi = './t/httpd-corner.psgi';
18 my $out = "$tmpdir/out.log";
19 my $err = "$tmpdir/err.log";
20 my $td;
22 my $register_exit_fifo = sub {
23         my ($s, $f) = @_;
24         my $sock = new_sock($s);
25         ok($sock->write("GET /exit-fifo$f HTTP/1.0\r\n\r\n"),
26                 'request exit-fifo');
27         ok($sock->read(my $buf, 4096), 'read exit-fifo response');
28         like($buf, qr!\r\n\r\nfifo \Q$f\E registered\z!, 'set exit fifo');
31 my $spawn_httpd = sub {
32         my (@args) = @_;
33         my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
34         $td = start_script($cmd);
38         require PublicInbox::Daemon;
39         my $l = "$tmpdir/named.sock";
40         my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
41                                         Type => SOCK_STREAM);
42         is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
45 ok(!-S $unix, 'UNIX socket does not exist, yet');
46 my $f1 = "$tmpdir/f1";
47 mkfifo($f1, 0600);
49         local $ENV{TEST_OPEN_FIFO} = $f1;
50         $spawn_httpd->("-l$unix", '-W0');
51         open my $fh, '<', $f1 or xbail "open($f1): $!";
52         is(my $hi = <$fh>, "hi\n", 'got FIFO greeting');
54 ok(-S $unix, 'UNIX socket was bound by -httpd');
56 sub new_sock ($) {
57         IO::Socket::UNIX->new(Peer => $_[0], Type => SOCK_STREAM)
58                 // xbail "E: $! connecting to $_[0]";
61 sub check_sock ($) {
62         my ($unix) = @_;
63         my $sock = new_sock($unix);
64         ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
65                 'wrote req to server');
66         ok($sock->read(my $buf, 4096), 'read response');
67         like($buf, qr!\r\n\r\n127\.0\.0\.1 0\z!,
68                 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
71 check_sock($unix);
73 { # do not clobber existing socket
74         my %err = ( 'linux' => EADDRINUSE, 'freebsd' => EADDRINUSE );
75         open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
76         open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
77         my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
78         my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
79         $ftd->join;
80         isnt($?, 0, 'httpd failure set $?');
81         SKIP: {
82                 my $ec = $err{$^O} or
83                         skip("not sure if $^O fails with EADDRINUSE", 1);
84                 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
85         };
86         open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
87         local $/;
88         my $e = <$fh>;
89         like($e, qr/no listeners bound/i, 'got error message');
90         is(-s "$tmpdir/1", 0, 'stdout was empty');
94         is($td->kill, 1, 'terminate existing process');
95         $td->join;
96         is($?, 0, 'existing httpd exited successfully');
97         ok(-S $unix, 'unix socket still exists');
100 # portable Perl can delay or miss signal dispatches due to races,
101 # so disable some tests on systems lacking signalfd(2) or EVFILT_SIGNAL
102 my $has_sigfd = PublicInbox::Sigfd->new({}) ? 1 : $ENV{TEST_UNRELIABLE};
103 PublicInbox::DS::Reset() if $has_sigfd;
105 sub delay_until {
106         my ($cond, $msg) = @_;
107         my $end = time + 30;
108         do {
109                 return if $cond->();
110                 tick(0.012);
111         } until (time > $end);
112         Carp::confess($msg // 'condition failed');
115 SKIP: {
116         require_mods('Net::Server::Daemonize', 52);
117         $has_sigfd or skip('signalfd / EVFILT_SIGNAL not available', 52);
118         my $pid_file = "$tmpdir/pid";
119         my $read_pid = sub {
120                 my $f = shift;
121                 open my $fh, '<', $f or die "open $f failed: $!";
122                 my $pid = do { local $/; <$fh> };
123                 chomp($pid) or die("pid file not ready $!");
124                 $pid;
125         };
127         for my $w (qw(-W0 -W1)) {
128                 my ($p0, $p1) = quit_waiter_pipe;
129                 # wait for daemonization
130                 $spawn_httpd->("-l$unix", '-D', '-P', $pid_file, $w);
131                 close $p1;
132                 $td->join;
133                 is($?, 0, "daemonized $w process");
134                 check_sock($unix);
135                 ok(-s $pid_file, "$w pid file written");
136                 my $pid = $read_pid->($pid_file);
137                 no_pollerfd($pid) if $w eq '-W1';
138                 is(kill('TERM', $pid), 1, "signaled daemonized $w process");
139                 delete $td->{-extra}; # drop tail(1) process
140                 wait_for_eof($p0, "httpd $w quit pipe");
141                 ok(!-e $pid_file, "$w pid file unlinked at exit");
142         }
144         my $httpd = abs_path('blib/script/public-inbox-httpd');
145         $psgi = abs_path($psgi);
146         my $opt = { run_mode => 0 };
147         my @args = ("-l$unix", '-D', '-P', $pid_file, -1, $out, -2, $err);
149         if ('USR2 upgrades with workers') {
150                 my ($p0, $p1) = quit_waiter_pipe;
151                 $td = start_script([$httpd, @args, $psgi], undef, $opt);
152                 close $p1;
153                 $td->join;
154                 is($?, 0, "daemonized process again");
155                 check_sock($unix);
156                 ok(-s $pid_file, 'pid file written');
157                 my $pid = $read_pid->($pid_file);
159                 # stop worker to ensure check_sock below hits $new_pid
160                 kill('TTOU', $pid) or die "TTOU failed: $!";
162                 kill('USR2', $pid) or die "USR2 failed: $!";
163                 delay_until(sub {
164                         $pid != (eval { $read_pid->($pid_file) } // $pid)
165                 });
166                 my $new_pid = $read_pid->($pid_file);
167                 isnt($new_pid, $pid, 'new child started');
168                 ok($new_pid > 0, '$new_pid valid');
169                 delay_until(sub { -s "$pid_file.oldbin" });
170                 my $old_pid = $read_pid->("$pid_file.oldbin");
171                 is($old_pid, $pid, '.oldbin pid file written');
172                 ok($old_pid > 0, '$old_pid valid');
174                 check_sock($unix); # ensures $new_pid is ready to receive signals
176                 # first, back out of the upgrade
177                 kill('QUIT', $new_pid) or die "kill new PID failed: $!";
178                 delay_until(sub {
179                         $pid == (eval { $read_pid->($pid_file) } // 0)
180                 });
182                 delay_until(sub { !kill(0, $new_pid) }, 'new PID really died');
184                 is($read_pid->($pid_file), $pid, 'old PID file restored');
185                 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
187                 # retry USR2 upgrade
188                 kill('USR2', $pid) or die "USR2 failed: $!";
189                 delay_until(sub {
190                         $pid != (eval { $read_pid->($pid_file) } // $pid)
191                 });
192                 $new_pid = $read_pid->($pid_file);
193                 isnt($new_pid, $pid, 'new child started again');
194                 $old_pid = $read_pid->("$pid_file.oldbin");
195                 is($old_pid, $pid, '.oldbin pid file written');
197                 # drop the old parent
198                 kill('QUIT', $old_pid) or die "QUIT failed: $!";
199                 delay_until(sub { !kill(0, $old_pid) }, 'old PID really died');
201                 ok(!-f "$pid_file.oldbin", '.oldbin PID file gone');
203                 # drop the new child
204                 check_sock($unix);
205                 kill('QUIT', $new_pid) or die "QUIT failed: $!";
207                 wait_for_eof($p0, 'new process');
208                 ok(!-f $pid_file, 'PID file is gone');
209         }
211         if ('try USR2 without workers (-W0)') {
212                 my ($p0, $p1) = quit_waiter_pipe;
213                 $td = start_script([$httpd, @args, '-W0', $psgi], undef, $opt);
214                 close $p1;
215                 $td->join;
216                 is($?, 0, 'daemonized w/o workers');
217                 $register_exit_fifo->($unix, $f1);
218                 my $pid = $read_pid->($pid_file);
220                 # replace running process
221                 kill('USR2', $pid) or xbail "USR2 failed: $!";
222                 open my $fh, '<', $f1 or xbail "open($f1): $!";
223                 is(my $bye = <$fh>, "bye from $pid\n", 'got FIFO bye');
225                 check_sock($unix);
226                 $pid = $read_pid->($pid_file);
227                 kill('QUIT', $pid) or xbail "USR2 failed: $!";
229                 wait_for_eof($p0, '-W0 USR2 test pipe');
230                 ok(!-f $pid_file, 'PID file is gone');
231         }
234 done_testing();