t/*.t: use write_file helper function
[unicorn.git] / t / active-unix-socket.t
blobab3c9734a00459c65e1347c18e5c9efe797d2d08
1 #!perl -w
2 # Copyright (C) unicorn hackers <unicorn-public@yhbt.net>
3 # License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
5 use v5.14; BEGIN { require './t/lib.perl' };
6 use IO::Socket::UNIX;
7 use autodie;
8 no autodie 'kill';
9 my %to_kill;
10 END { kill('TERM', values(%to_kill)) if keys %to_kill }
11 my $u1 = "$tmpdir/u1.sock";
12 my $u2 = "$tmpdir/u2.sock";
14         write_file '>', "$tmpdir/u1.conf.rb", <<EOM;
15 pid "$tmpdir/u.pid"
16 listen "$u1"
17 stderr_path "$err_log"
18 EOM
19         write_file '>', "$tmpdir/u2.conf.rb", <<EOM;
20 pid "$tmpdir/u.pid"
21 listen "$u2"
22 stderr_path "$tmpdir/err2.log"
23 EOM
25         write_file '>', "$tmpdir/u3.conf.rb", <<EOM;
26 pid "$tmpdir/u3.pid"
27 listen "$u1"
28 stderr_path "$tmpdir/err3.log"
29 EOM
32 my @uarg = qw(-D -E none t/integration.ru);
34 # this pipe will be used to notify us when all daemons die:
35 pipe(my $p0, my $p1);
36 fcntl($p1, POSIX::F_SETFD, 0);
38 # start the first instance
39 unicorn('-c', "$tmpdir/u1.conf.rb", @uarg)->join;
40 is($?, 0, 'daemonized 1st process');
41 chomp($to_kill{u1} = slurp("$tmpdir/u.pid"));
42 like($to_kill{u1}, qr/\A\d+\z/s, 'read pid file');
44 chomp(my $worker_pid = readline(unix_start($u1, 'GET /pid')));
45 like($worker_pid, qr/\A\d+\z/s, 'captured worker pid');
46 ok(kill(0, $worker_pid), 'worker is kill-able');
49 # 2nd process conflicts on PID
50 unicorn('-c', "$tmpdir/u2.conf.rb", @uarg)->join;
51 isnt($?, 0, 'conflicting PID file fails to start');
53 chomp(my $pidf = slurp("$tmpdir/u.pid"));
54 is($pidf, $to_kill{u1}, 'pid file contents unchanged after start failure');
56 chomp(my $pid2 = readline(unix_start($u1, 'GET /pid')));
57 is($worker_pid, $pid2, 'worker PID unchanged');
60 # 3rd process conflicts on socket
61 unicorn('-c', "$tmpdir/u3.conf.rb", @uarg)->join;
62 isnt($?, 0, 'conflicting UNIX socket fails to start');
64 chomp($pid2 = readline(unix_start($u1, 'GET /pid')));
65 is($worker_pid, $pid2, 'worker PID still unchanged');
67 chomp($pidf = slurp("$tmpdir/u.pid"));
68 is($pidf, $to_kill{u1}, 'pid file contents unchanged after 2nd start failure');
70 { # teardown initial process via SIGKILL
71         ok(kill('KILL', delete $to_kill{u1}), 'SIGKILL initial daemon');
72         close $p1;
73         vec(my $rvec = '', fileno($p0), 1) = 1;
74         is(select($rvec, undef, undef, 5), 1, 'timeout for pipe HUP');
75         is(my $undef = <$p0>, undef, 'process closed pipe writer at exit');
76         ok(-f "$tmpdir/u.pid", 'pid file stayed after SIGKILL');
77         ok(-S $u1, 'socket stayed after SIGKILL');
78         is(IO::Socket::UNIX->new(Peer => $u1, Type => SOCK_STREAM), undef,
79                 'fail to connect to u1');
80         for (1..50) { # wait for init process to reap worker
81                 kill(0, $worker_pid) or last;
82                 sleep 0.011;
83         }
84         ok(!kill(0, $worker_pid), 'worker gone after parent dies');
87 # restart the first instance
89         pipe($p0, $p1);
90         fcntl($p1, POSIX::F_SETFD, 0);
91         unicorn('-c', "$tmpdir/u1.conf.rb", @uarg)->join;
92         is($?, 0, 'daemonized 1st process');
93         chomp($to_kill{u1} = slurp("$tmpdir/u.pid"));
94         like($to_kill{u1}, qr/\A\d+\z/s, 'read pid file');
96         chomp($pid2 = readline(unix_start($u1, 'GET /pid')));
97         like($pid2, qr/\A\d+\z/, 'worker running');
99         ok(kill('TERM', delete $to_kill{u1}), 'SIGTERM restarted daemon');
100         close $p1;
101         vec(my $rvec = '', fileno($p0), 1) = 1;
102         is(select($rvec, undef, undef, 5), 1, 'timeout for pipe HUP');
103         is(my $undef = <$p0>, undef, 'process closed pipe writer at exit');
104         ok(!-f "$tmpdir/u.pid", 'pid file gone after SIGTERM');
105         ok(-S $u1, 'socket stays after SIGTERM');
108 check_stderr;
109 undef $tmpdir;
110 done_testing;