From 948f78403172657590d690b9255467b9ccb968cd Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 10 Sep 2023 09:31:44 +0000 Subject: [PATCH 07/11] tests: use more common variable names between tests Stuff like $u_conf, $daemon_pid, $pid_file, etc. will reduce cognitive overhead. --- t/active-unix-socket.t | 2 +- t/client_body_buffer_size.t | 6 ++---- t/heartbeat-timeout.t | 3 +-- t/integration.t | 5 ++--- t/lib.perl | 31 +++++++++++++++++++++++++++---- t/working_directory.t | 31 +++++-------------------------- 6 files changed, 38 insertions(+), 40 deletions(-) diff --git a/t/active-unix-socket.t b/t/active-unix-socket.t index 4dcc8dc6..32cb0c2e 100644 --- a/t/active-unix-socket.t +++ b/t/active-unix-socket.t @@ -15,7 +15,7 @@ my $u2 = "$tmpdir/u2.sock"; print $fh <', $uconf; +open my $conf_fh, '>', $u_conf; $conf_fh->autoflush(1); print $conf_fh < $srv }); my ($c, $status, $hdr); my $mem_class = 'StringIO'; diff --git a/t/heartbeat-timeout.t b/t/heartbeat-timeout.t index ce1f7e16..694867a4 100644 --- a/t/heartbeat-timeout.t +++ b/t/heartbeat-timeout.t @@ -6,7 +6,6 @@ use autodie; use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC); mkdir "$tmpdir/alt"; my $srv = tcp_server(); -my $u_conf = "$tmpdir/u.conf.rb"; open my $fh, '>', $u_conf; print $fh <blocking(0); diff --git a/t/integration.t b/t/integration.t index 13b07467..eb40ffc7 100644 --- a/t/integration.t +++ b/t/integration.t @@ -10,15 +10,14 @@ use autodie; our $srv = tcp_server(); our $host_port = tcp_host_port($srv); my $t0 = time; -my $conf = "$tmpdir/u.conf.rb"; -open my $conf_fh, '>', $conf; +open my $conf_fh, '>', $u_conf; $conf_fh->autoflush(1); my $u1 = "$tmpdir/u1"; print $conf_fh < $srv }); +my $ar = unicorn(qw(-E none t/integration.ru -c), $u_conf, { 3 => $srv }); my $curl = which('curl'); my $fifo = "$tmpdir/fifo"; POSIX::mkfifo($fifo, 0600) or die "mkfifo: $!"; diff --git a/t/lib.perl b/t/lib.perl index 13e390d6..244972bc 100644 --- a/t/lib.perl +++ b/t/lib.perl @@ -6,20 +6,43 @@ use v5.14; use parent qw(Exporter); use autodie; use Test::More; +use Time::HiRes qw(sleep); use IO::Socket::INET; use POSIX qw(dup2 _exit setpgid :signal_h SEEK_SET F_SETFD); use File::Temp 0.19 (); # 0.19 for ->newdir -our ($tmpdir, $errfh, $err_log); +our ($tmpdir, $errfh, $err_log, $u_sock, $u_conf, $daemon_pid, + $pid_file); our @EXPORT = qw(unicorn slurp tcp_server tcp_start unicorn - $tmpdir $errfh $err_log + $tmpdir $errfh $err_log $u_sock $u_conf $daemon_pid $pid_file SEEK_SET tcp_host_port which spawn check_stderr unix_start slurp_hdr - do_req); + do_req stop_daemon); my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!); $tmpdir = File::Temp->newdir("unicorn-$base-XXXX", TMPDIR => 1); $err_log = "$tmpdir/err.log"; +$pid_file = "$tmpdir/pid"; +$u_sock = "$tmpdir/u.sock"; +$u_conf = "$tmpdir/u.conf.rb"; open($errfh, '>>', $err_log); -END { diag slurp($err_log) if $tmpdir }; + +sub stop_daemon (;$) { + my ($is_END) = @_; + kill('TERM', $daemon_pid); + my $tries = 1000; + while (CORE::kill(0, $daemon_pid) && --$tries) { sleep(0.01) } + if ($is_END && CORE::kill(0, $daemon_pid)) { # after done_testing + CORE::kill('KILL', $daemon_pid); + die "daemon_pid=$daemon_pid did not die"; + } else { + ok(!CORE::kill(0, $daemon_pid), 'daemonized unicorn gone'); + undef $daemon_pid; + } +}; + +END { + diag slurp($err_log) if $tmpdir; + stop_daemon(1) if defined $daemon_pid; +}; sub check_stderr () { my @log = slurp($err_log); diff --git a/t/working_directory.t b/t/working_directory.t index 6c974720..f9254eb8 100644 --- a/t/working_directory.t +++ b/t/working_directory.t @@ -4,12 +4,10 @@ use v5.14; BEGIN { require './t/lib.perl' }; use autodie; mkdir "$tmpdir/alt"; -my $u_sock = "$tmpdir/u.sock"; my $ru = "$tmpdir/alt/config.ru"; -my $u_conf = "$tmpdir/u.conf.rb"; open my $fh, '>', $u_conf; print $fh <(1) if defined $pid }; - unicorn('-c', $u_conf)->join; # will daemonize -chomp($pid = slurp("$tmpdir/pid")); +chomp($daemon_pid = slurp($pid_file)); my ($status, $hdr, $bdy) = do_req($u_sock, 'GET / HTTP/1.0'); is($bdy, "1\n", 'got expected $master_ppid'); -$stop_daemon->(); +stop_daemon; check_stderr; if ('test without CLI switches in config.ru') { @@ -65,12 +44,12 @@ if ('test without CLI switches in config.ru') { close $fh; unicorn('-D', '-l', $u_sock, '-c', $u_conf)->join; # will daemonize - chomp($pid = slurp("$tmpdir/pid")); + chomp($daemon_pid = slurp($pid_file)); ($status, $hdr, $bdy) = do_req($u_sock, 'GET / HTTP/1.0'); is($bdy, "1\n", 'got expected $master_ppid'); - $stop_daemon->(); + stop_daemon; check_stderr; }