about summary refs log tree commit homepage
path: root/t
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-08-01 21:24:43 +0000
committerEric Wong <e@80x24.org>2022-08-02 19:20:11 +0000
commitc38111d6f3877cf31d28b0a0339d063df0fa58f6 (patch)
tree8bf1d2c22d96a9bc3ca9c20b9c85eab46802c5be /t
parenta55a38db0276a8ce1a09392573af6e1305cbaba9 (diff)
downloadpublic-inbox-c38111d6f3877cf31d28b0a0339d063df0fa58f6.tar.gz
This allows memory savings by allowing multiple, completely
unrelated-PSGI apps to run within the same process as IMAP,
NNTP, and POP3.
Diffstat (limited to 't')
-rw-r--r--t/alt.psgi17
-rw-r--r--t/httpd-corner.psgi8
-rw-r--r--t/httpd-corner.t39
3 files changed, 58 insertions, 6 deletions
diff --git a/t/alt.psgi b/t/alt.psgi
new file mode 100644
index 00000000..c7f42979
--- /dev/null
+++ b/t/alt.psgi
@@ -0,0 +1,17 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use v5.12;
+use warnings;
+use Plack::Builder;
+my $pi_config = $ENV{PI_CONFIG} // 'unset'; # capture ASAP
+my $app = sub {
+        my ($env) = @_;
+        $env->{'psgi.errors'}->print("ALT\n");
+        [ 200, ['Content-Type', 'text/plain'], [ $pi_config ] ]
+};
+
+builder {
+        enable 'ContentLength';
+        enable 'Head';
+        $app;
+}
diff --git a/t/httpd-corner.psgi b/t/httpd-corner.psgi
index e9a3a6b7..10cf8350 100644
--- a/t/httpd-corner.psgi
+++ b/t/httpd-corner.psgi
@@ -1,11 +1,12 @@
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # corner case tests for the generic PSGI server
 # Usage: plackup [OPTIONS] /path/to/this/file
-use strict;
+use v5.12;
 use warnings;
 use Plack::Builder;
 require Digest::SHA;
+my $pi_config = $ENV{PI_CONFIG} // 'unset'; # capture ASAP
 my $app = sub {
         my ($env) = @_;
         my $path = $env->{PATH_INFO};
@@ -114,6 +115,9 @@ my $app = sub {
         } elsif ($path eq '/url_scheme') {
                 $code = 200;
                 push @$body, $env->{'psgi.url_scheme'}
+        } elsif ($path eq '/PI_CONFIG') {
+                $code = 200;
+                push @$body, $pi_config; # show value at ->refresh_groups
         }
         [ $code, $h, $body ]
 };
diff --git a/t/httpd-corner.t b/t/httpd-corner.t
index 0a613a9e..973cc55d 100644
--- a/t/httpd-corner.t
+++ b/t/httpd-corner.t
@@ -1,4 +1,5 @@
-# Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
+#!perl -w
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # note: our HTTP server should be standalone and capable of running
 # generic PSGI/Plack apps.
@@ -19,7 +20,7 @@ ok(defined mkfifo($fifo, 0777), 'created FIFO');
 my $err = "$tmpdir/stderr.log";
 my $out = "$tmpdir/stdout.log";
 my $psgi = "./t/httpd-corner.psgi";
-my $sock = tcp_server() or die;
+my $sock = tcp_server();
 my @zmods = qw(PublicInbox::GzipFilter IO::Uncompress::Gunzip);
 
 # Make sure we don't clobber socket options set by systemd or similar
@@ -53,14 +54,40 @@ sub unix_server ($) {
 
 my $upath = "$tmpdir/s";
 my $unix = unix_server($upath);
+my $alt = tcp_server();
 my $td;
 my $spawn_httpd = sub {
         my (@args) = @_;
-        my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
-        $td = start_script($cmd, undef, { 3 => $sock, 4 => $unix });
+        my $x = tcp_host_port($alt);
+        my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi,
+                '-l', "http://$x/?psgi=t/alt.psgi,env.PI_CONFIG=/path/to/alt".
+                        ",err=$tmpdir/alt.err" ];
+        my $env = { PI_CONFIG => '/dev/null' };
+        $td = start_script($cmd, $env, { 3 => $sock, 4 => $unix, 5 => $alt });
 };
 
 $spawn_httpd->();
+{
+        my $conn = conn_for($alt, 'alt PSGI path');
+        $conn->write("GET / HTTP/1.0\r\n\r\n");
+        $conn->read(my $buf, 4096);
+        like($buf, qr!^/path/to/alt\z!sm,
+                'alt.psgi loaded on alt socket with correct env');
+
+        $conn = conn_for($sock, 'default PSGI path');
+        $conn->write("GET /PI_CONFIG HTTP/1.0\r\n\r\n");
+        $conn->read($buf, 4096);
+        like($buf, qr!^/dev/null\z!sm,
+                'default PSGI on original socket');
+        my $log = capture("$tmpdir/alt.err");
+        ok(grep(/ALT/, @$log), 'alt psgi.errors written to');
+        $log = capture($err);
+        ok(!grep(/ALT/, @$log), 'STDERR not written to');
+        is(unlink($err, "$tmpdir/alt.err"), 2, 'unlinked stderr and alt.err');
+
+        $td->kill('USR1'); # trigger reopen_logs
+}
+
 if ('test worker death') {
         my $conn = conn_for($sock, 'killed worker');
         $conn->write("GET /pid HTTP/1.1\r\nHost:example.com\r\n\r\n");
@@ -82,6 +109,10 @@ if ('test worker death') {
         like($body, qr/\A[0-9]+\z/, '/pid response');
         isnt($body, $pid, 'respawned worker');
 }
+{ # check on prior USR1 signal
+        ok(-e $err, 'stderr recreated after USR1');
+        ok(-e "$tmpdir/alt.err", 'alt.err recreated after USR1');
+}
 {
         my $conn = conn_for($sock, 'Header spaces bogus');
         $conn->write("GET /empty HTTP/1.1\r\nSpaced-Out : 3\r\n\r\n");