about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <BOFH@YHBT.net>2023-09-10 09:31:44 +0000
committerEric Wong <BOFH@YHBT.net>2023-09-10 19:01:03 +0000
commit948f78403172657590d690b9255467b9ccb968cd (patch)
treefbb08b4839c920097df5212f22c18d9e93d18a71
parent29885f0d95aaa8e1d1f6cf3b791d9f08338a511e (diff)
downloadunicorn-948f78403172657590d690b9255467b9ccb968cd.tar.gz
Stuff like $u_conf, $daemon_pid, $pid_file, etc. will
reduce cognitive overhead.
-rw-r--r--t/active-unix-socket.t2
-rw-r--r--t/client_body_buffer_size.t6
-rw-r--r--t/heartbeat-timeout.t3
-rw-r--r--t/integration.t5
-rw-r--r--t/lib.perl31
-rw-r--r--t/working_directory.t31
6 files changed, 38 insertions, 40 deletions
diff --git a/t/active-unix-socket.t b/t/active-unix-socket.t
index 4dcc8dc..32cb0c2 100644
--- a/t/active-unix-socket.t
+++ b/t/active-unix-socket.t
@@ -15,7 +15,7 @@ my $u2 = "$tmpdir/u2.sock";
         print $fh <<EOM;
 pid "$tmpdir/u.pid"
 listen "$u1"
-stderr_path "$tmpdir/err.log"
+stderr_path "$err_log"
 EOM
         close $fh;
 
diff --git a/t/client_body_buffer_size.t b/t/client_body_buffer_size.t
index 3067f28..d479901 100644
--- a/t/client_body_buffer_size.t
+++ b/t/client_body_buffer_size.t
@@ -4,16 +4,14 @@
 
 use v5.14; BEGIN { require './t/lib.perl' };
 use autodie;
-my $uconf = "$tmpdir/u.conf.rb";
-
-open my $conf_fh, '>', $uconf;
+open my $conf_fh, '>', $u_conf;
 $conf_fh->autoflush(1);
 print $conf_fh <<EOM;
 client_body_buffer_size 0
 EOM
 my $srv = tcp_server();
 my $host_port = tcp_host_port($srv);
-my @uarg = (qw(-E none t/client_body_buffer_size.ru -c), $uconf);
+my @uarg = (qw(-E none t/client_body_buffer_size.ru -c), $u_conf);
 my $ar = unicorn(@uarg, { 3 => $srv });
 my ($c, $status, $hdr);
 my $mem_class = 'StringIO';
diff --git a/t/heartbeat-timeout.t b/t/heartbeat-timeout.t
index ce1f7e1..694867a 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 <<EOM;
 pid "$tmpdir/pid"
@@ -23,7 +22,7 @@ like($status, qr!\AHTTP/1\.[01] 200\b!, 'PID request succeeds');
 like($wpid, qr/\A[0-9]+\z/, 'worker is running');
 
 my $t0 = clock_gettime(CLOCK_MONOTONIC);
-$c = tcp_start($srv, 'GET /block-forever HTTP/1.0');
+my $c = tcp_start($srv, 'GET /block-forever HTTP/1.0');
 vec(my $rvec = '', fileno($c), 1) = 1;
 is(select($rvec, undef, undef, 6), 1, 'got readiness');
 $c->blocking(0);
diff --git a/t/integration.t b/t/integration.t
index 13b0746..eb40ffc 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 <<EOM;
 early_hints true
 listen "$u1"
 EOM
-my $ar = unicorn(qw(-E none t/integration.ru -c), $conf, { 3 => $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 13e390d..244972b 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 6c97472..f9254eb 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 <<EOM;
-pid "$tmpdir/pid"
+pid "$pid_file"
 preload_app true
 stderr_path "$err_log"
 working_directory "$tmpdir/alt" # the whole point of this test
@@ -30,32 +28,13 @@ $common_ru
 EOM
 close $fh;
 
-my $pid;
-my $stop_daemon = sub {
-        my ($is_END) = @_;
-        kill('TERM', $pid);
-        my $tries = 1000;
-        while (CORE::kill(0, $pid) && --$tries) {
-                select undef, undef, undef, 0.01;
-        }
-        if ($is_END && CORE::kill(0, $pid)) {
-                CORE::kill('KILL', $pid);
-                die "daemonized PID=$pid did not die";
-        } else {
-                ok(!CORE::kill(0, $pid), 'daemonized unicorn gone');
-                undef $pid;
-        }
-};
-
-END { $stop_daemon->(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;
 }