about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <BOFH@YHBT.net>2023-09-07 22:55:09 +0000
committerEric Wong <BOFH@YHBT.net>2023-09-10 19:01:00 +0000
commitf43c28ea10ca8d520b55f2fbb20710dd66fc4fb5 (patch)
tree53d8c8f90052b3d511f8785a80e9f0f65b1c4658
parent7624fe222f77ab7425f10ed072b103666339c24b (diff)
downloadunicorn-f43c28ea10ca8d520b55f2fbb20710dd66fc4fb5.tar.gz
We can fold some tests into one test to save on Perl startup
time (but Ruby startup time is a lost cause).
-rw-r--r--t/lib.perl12
-rw-r--r--t/reload-bad-config.t58
-rwxr-xr-xt/t0001-reload-bad-config.sh53
-rwxr-xr-xt/t0002-config-conflict.sh49
4 files changed, 65 insertions, 107 deletions
diff --git a/t/lib.perl b/t/lib.perl
index fe3404b..7de9e42 100644
--- a/t/lib.perl
+++ b/t/lib.perl
@@ -9,17 +9,19 @@ use Test::More;
 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);
-our @EXPORT = qw(unicorn slurp tcp_server tcp_start unicorn $tmpdir $errfh
+our ($tmpdir, $errfh, $err_log);
+our @EXPORT = qw(unicorn slurp tcp_server tcp_start unicorn
+        $tmpdir $errfh $err_log
         SEEK_SET tcp_host_port which spawn check_stderr unix_start slurp_hdr);
 
 my ($base) = ($0 =~ m!\b([^/]+)\.[^\.]+\z!);
 $tmpdir = File::Temp->newdir("unicorn-$base-XXXX", TMPDIR => 1);
-open($errfh, '>>', "$tmpdir/err.log");
-END { diag slurp("$tmpdir/err.log") if $tmpdir };
+$err_log = "$tmpdir/err.log";
+open($errfh, '>>', $err_log);
+END { diag slurp($err_log) if $tmpdir };
 
 sub check_stderr () {
-        my @log = slurp("$tmpdir/err.log");
+        my @log = slurp($err_log);
         diag("@log") if $ENV{V};
         my @err = grep(!/NameError.*Unicorn::Waiter/, grep(/error/i, @log));
         @err = grep(!/failed to set accept_filter=/, @err);
diff --git a/t/reload-bad-config.t b/t/reload-bad-config.t
new file mode 100644
index 0000000..c7055c7
--- /dev/null
+++ b/t/reload-bad-config.t
@@ -0,0 +1,58 @@
+#!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;
+my $srv = tcp_server();
+my $host_port = tcp_host_port($srv);
+my $ru = "$tmpdir/config.ru";
+my $u_conf = "$tmpdir/u.conf.rb";
+
+open my $fh, '>', $ru;
+print $fh <<'EOM';
+use Rack::ContentLength
+use Rack::ContentType, 'text/plain'
+config = ru = "hello world\n" # check for config variable conflicts, too
+run lambda { |env| [ 200, {}, [ ru.to_s ] ] }
+EOM
+close $fh;
+
+open $fh, '>', $u_conf;
+print $fh <<EOM;
+preload_app true
+stderr_path "$err_log"
+EOM
+close $fh;
+
+my $ar = unicorn(qw(-E none -c), $u_conf, $ru, { 3 => $srv });
+my $c = tcp_start($srv, 'GET / HTTP/1.0');
+my ($status, $hdr) = slurp_hdr($c);
+my $bdy = do { local $/; <$c> };
+like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid at start');
+is($bdy, "hello world\n", 'body matches expected');
+
+open $fh, '>>', $ru;
+say $fh '....this better be a syntax error in any version of ruby...';
+close $fh;
+
+$ar->do_kill('HUP'); # reload
+my @l;
+for (1..1000) {
+        @l = grep(/(?:done|error) reloading/, slurp($err_log)) and
+                last;
+        select undef, undef, undef, 0.011;
+}
+diag slurp($err_log) if $ENV{V};
+ok(grep(/error reloading/, @l), 'got error reloading');
+open $fh, '>', $err_log;
+close $fh;
+
+$c = tcp_start($srv, 'GET / HTTP/1.0');
+($status, $hdr) = slurp_hdr($c);
+$bdy = do { local $/; <$c> };
+like($status, qr!\AHTTP/1\.[01] 200\b!, 'status line valid afte reload');
+is($bdy, "hello world\n", 'body matches expected after reload');
+
+check_stderr;
+undef $tmpdir; # quiet t/lib.perl END{}
+done_testing;
diff --git a/t/t0001-reload-bad-config.sh b/t/t0001-reload-bad-config.sh
deleted file mode 100755
index 55bb355..0000000
--- a/t/t0001-reload-bad-config.sh
+++ /dev/null
@@ -1,53 +0,0 @@
-#!/bin/sh
-. ./test-lib.sh
-t_plan 7 "reload config.ru error with preload_app true"
-
-t_begin "setup and start" && {
-        unicorn_setup
-        rtmpfiles ru
-
-        cat > $ru <<\EOF
-use Rack::ContentLength
-use Rack::ContentType, "text/plain"
-x = { "hello" => "world" }
-run lambda { |env| [ 200, {}, [ x.inspect << "\n" ] ] }
-EOF
-        echo 'preload_app true' >> $unicorn_config
-        unicorn -D -c $unicorn_config $ru
-        unicorn_wait_start
-}
-
-t_begin "hit with curl" && {
-        out=$(curl -sSf http://$listen/)
-        test x"$out" = x'{"hello"=>"world"}'
-}
-
-t_begin "introduce syntax error in rackup file" && {
-        echo '...' >> $ru
-}
-
-t_begin "reload signal succeeds" && {
-        kill -HUP $unicorn_pid
-        while ! egrep '(done|error) reloading' $r_err >/dev/null
-        do
-                sleep 1
-        done
-
-        grep 'error reloading' $r_err >/dev/null
-        > $r_err
-}
-
-t_begin "hit with curl" && {
-        out=$(curl -sSf http://$listen/)
-        test x"$out" = x'{"hello"=>"world"}'
-}
-
-t_begin "killing succeeds" && {
-        kill $unicorn_pid
-}
-
-t_begin "check stderr" && {
-        check_stderr
-}
-
-t_done
diff --git a/t/t0002-config-conflict.sh b/t/t0002-config-conflict.sh
deleted file mode 100755
index d7b2181..0000000
--- a/t/t0002-config-conflict.sh
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/bin/sh
-. ./test-lib.sh
-t_plan 6 "config variables conflict with preload_app"
-
-t_begin "setup and start" && {
-        unicorn_setup
-        rtmpfiles ru rutmp
-
-        cat > $ru <<\EOF
-use Rack::ContentLength
-use Rack::ContentType, "text/plain"
-config = ru = { "hello" => "world" }
-run lambda { |env| [ 200, {}, [ ru.inspect << "\n" ] ] }
-EOF
-        echo 'preload_app true' >> $unicorn_config
-        unicorn -D -c $unicorn_config $ru
-        unicorn_wait_start
-}
-
-t_begin "hit with curl" && {
-        out=$(curl -sSf http://$listen/)
-        test x"$out" = x'{"hello"=>"world"}'
-}
-
-t_begin "modify rackup file" && {
-        sed -e 's/world/WORLD/' < $ru > $rutmp
-        mv $rutmp $ru
-}
-
-t_begin "reload signal succeeds" && {
-        kill -HUP $unicorn_pid
-        while ! egrep '(done|error) reloading' < $r_err >/dev/null
-        do
-                sleep 1
-        done
-
-        grep 'done reloading' $r_err >/dev/null
-}
-
-t_begin "hit with curl" && {
-        out=$(curl -sSf http://$listen/)
-        test x"$out" = x'{"hello"=>"WORLD"}'
-}
-
-t_begin "killing succeeds" && {
-        kill $unicorn_pid
-}
-
-t_done