about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <BOFH@YHBT.net>2023-09-10 08:27:04 +0000
committerEric Wong <BOFH@YHBT.net>2023-09-10 19:01:03 +0000
commit86aea575c331a3b5242db1c14a848928a37ff9e3 (patch)
tree1c1c9d4965c50b6cc2a88ab5e1040d5b95ed7fd9
parent1607ac966f604ec4cf383025c4c3ee296f638fff (diff)
downloadunicorn-86aea575c331a3b5242db1c14a848928a37ff9e3.tar.gz
tests: rewrite SIGWINCH && SIGTTIN test in Perl 5
No need to deal with full second sleeps, here.
-rwxr-xr-xt/t0009-winch_ttin.sh59
-rw-r--r--t/winch_ttin.t72
2 files changed, 72 insertions, 59 deletions
diff --git a/t/t0009-winch_ttin.sh b/t/t0009-winch_ttin.sh
deleted file mode 100755
index 6e56e30..0000000
--- a/t/t0009-winch_ttin.sh
+++ /dev/null
@@ -1,59 +0,0 @@
-#!/bin/sh
-. ./test-lib.sh
-t_plan 8 "SIGTTIN succeeds after SIGWINCH"
-
-t_begin "setup and start" && {
-        unicorn_setup
-cat >> $unicorn_config <<EOF
-after_fork do |server, worker|
-  # test script will block while reading from $fifo,
-  File.open("$fifo", "wb") { |fp| fp.syswrite worker.nr.to_s }
-end
-EOF
-        unicorn -D -c $unicorn_config pid.ru
-        unicorn_wait_start
-        test 0 -eq $(cat $fifo) || die "worker.nr != 0"
-}
-
-t_begin "read worker pid" && {
-        orig_worker_pid=$(curl -sSf http://$listen/)
-        test -n "$orig_worker_pid" && kill -0 $orig_worker_pid
-}
-
-t_begin "stop all workers" && {
-        kill -WINCH $unicorn_pid
-}
-
-# we have to do this next step before delivering TTIN
-# signals aren't guaranteed to delivered in order
-t_begin "wait for worker to die" && {
-        i=0
-        while kill -0 $orig_worker_pid 2>/dev/null
-        do
-                i=$(( $i + 1 ))
-                test $i -lt 600 || die "timed out"
-                sleep 1
-        done
-}
-
-t_begin "start one worker back up" && {
-        kill -TTIN $unicorn_pid
-}
-
-t_begin "wait for new worker to start" && {
-        test 0 -eq $(cat $fifo) || die "worker.nr != 0"
-        new_worker_pid=$(curl -sSf http://$listen/)
-        test -n "$new_worker_pid" && kill -0 $new_worker_pid
-        test $orig_worker_pid -ne $new_worker_pid || \
-           die "worker wasn't replaced"
-}
-
-t_begin "killing succeeds" && {
-        kill $unicorn_pid
-}
-
-t_begin "check stderr" && check_stderr
-
-dbgcat r_err
-
-t_done
diff --git a/t/winch_ttin.t b/t/winch_ttin.t
new file mode 100644
index 0000000..1a19877
--- /dev/null
+++ b/t/winch_ttin.t
@@ -0,0 +1,72 @@
+#!perl -w
+# Copyright (C) unicorn hackers <unicorn-public@yhbt.net>
+# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
+use v5.14; BEGIN { require './t/lib.perl' };
+use autodie;
+use POSIX qw(mkfifo);
+my $u_conf = "$tmpdir/u.conf.rb";
+my $u_sock = "$tmpdir/u.sock";
+my $fifo = "$tmpdir/fifo";
+mkfifo($fifo, 0666) or die "mkfifo($fifo): $!";
+
+open my $fh, '>', $u_conf;
+print $fh <<EOM;
+pid "$tmpdir/pid"
+listen "$u_sock"
+stderr_path "$err_log"
+after_fork do |server, worker|
+  # test script will block while reading from $fifo,
+  File.open("$fifo", "wb") { |fp| fp.syswrite worker.nr.to_s }
+end
+EOM
+close $fh;
+
+unicorn('-D', '-c', $u_conf, 't/integration.ru')->join;
+is($?, 0, 'daemonized properly');
+open $fh, '<', "$tmpdir/pid";
+chomp(my $pid = <$fh>);
+ok(kill(0, $pid), 'daemonized PID works');
+my $quit = sub { kill('QUIT', $pid) if $pid; $pid = undef };
+END { $quit->() };
+
+open $fh, '<', $fifo;
+my $worker_nr = <$fh>;
+close $fh;
+is($worker_nr, '0', 'initial worker spawned');
+
+my $c = unix_start($u_sock, 'GET /pid HTTP/1.0');
+my ($status, $hdr) = slurp_hdr($c);
+like($status, qr/ 200\b/, 'got 200 response');
+my $worker_pid = do { local $/; <$c> };
+like($worker_pid, qr/\A[0-9]+\n\z/s, 'PID in response');
+chomp $worker_pid;
+ok(kill(0, $worker_pid), 'worker_pid is valid');
+
+ok(kill('WINCH', $pid), 'SIGWINCH can be sent');
+
+my $tries = 1000;
+while (CORE::kill(0, $worker_pid) && --$tries) {
+        select undef, undef, undef, 0.01;
+}
+ok(!CORE::kill(0, $worker_pid), 'worker not running');
+
+ok(kill('TTIN', $pid), 'SIGTTIN to restart worker');
+
+open $fh, '<', $fifo;
+$worker_nr = <$fh>;
+close $fh;
+is($worker_nr, '0', 'worker restarted');
+
+$c = unix_start($u_sock, 'GET /pid HTTP/1.0');
+($status, $hdr) = slurp_hdr($c);
+like($status, qr/ 200\b/, 'got 200 response');
+chomp(my $new_worker_pid = do { local $/; <$c> });
+like($new_worker_pid, qr/\A[0-9]+\z/, 'got new worker PID');
+ok(kill(0, $new_worker_pid), 'got a valid worker PID');
+isnt($worker_pid, $new_worker_pid, 'worker PID changed');
+
+$quit->();
+
+check_stderr;
+undef $tmpdir;
+done_testing;