]> Sergey Matveev's repositories - public-inbox.git/commitdiff
tests: "check-run" uses persistent lei daemon
authorEric Wong <e@80x24.org>
Thu, 25 Mar 2021 04:20:23 +0000 (06:20 +0200)
committerEric Wong <e@80x24.org>
Thu, 25 Mar 2021 17:59:02 +0000 (17:59 +0000)
We'll use a lei-daemon if it's already running and
TEST_LEI_DAEMON_PERSIST_DIR is set, but we can also start
one and manage it from t/run.perl

This drops "make check-run TEST_LEI_DAEMON_ONLY=1"
time by ~10% for me.

lib/PublicInbox/TestCommon.pm
t/lei-externals.t
t/run.perl

index ffff59024321100d3771dd56b5df0d28915c427a..ca165a045771b700ea55aef2572d6d500ed40943 100644 (file)
@@ -515,22 +515,30 @@ EOM
        my ($daemon_pid, $for_destroy, $daemon_xrd);
        my $tmpdir = $test_opt->{tmpdir};
        ($tmpdir, $for_destroy) = tmpdir unless $tmpdir;
+       state $persist_xrd = $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
        SKIP: {
                skip 'TEST_LEI_ONESHOT set', 1 if $ENV{TEST_LEI_ONESHOT};
                my $home = "$tmpdir/lei-daemon";
                mkdir($home, 0700) or BAIL_OUT "mkdir: $!";
                local $ENV{HOME} = $home;
-               $daemon_xrd = "$home/xdg_run";
-               mkdir($daemon_xrd, 0700) or BAIL_OUT "mkdir: $!";
+               my $persist;
+               if ($persist_xrd && !$test_opt->{daemon_only}) {
+                       $persist = $daemon_xrd = $persist_xrd;
+               } else {
+                       $daemon_xrd = "$home/xdg_run";
+                       mkdir($daemon_xrd, 0700) or BAIL_OUT "mkdir: $!";
+               }
                local $ENV{XDG_RUNTIME_DIR} = $daemon_xrd;
                $cb->();
-               lei_ok(qw(daemon-pid), \"daemon-pid after $t");
-               chomp($daemon_pid = $lei_out);
-               if ($daemon_pid) {
+               unless ($persist) {
+                       lei_ok(qw(daemon-pid), \"daemon-pid after $t");
+                       chomp($daemon_pid = $lei_out);
+                       if (!$daemon_pid) {
+                               fail("daemon not running after $t");
+                               skip 'daemon died unexpectedly', 2;
+                       }
                        ok(kill(0, $daemon_pid), "daemon running after $t");
                        lei_ok(qw(daemon-kill), \"daemon-kill after $t");
-               } else {
-                       fail("daemon not running after $t");
                }
        }; # SKIP for lei_daemon
        unless ($test_opt->{daemon_only}) {
index afd90d19ad4139b56479361792ca88472773c2ff..488bf5ad99ef07db571944db05ca0ba14af51767 100644 (file)
@@ -57,6 +57,8 @@ SKIP: {
                chomp(my $pid_after = $lei_out);
                is($pid_after, $pid_before, 'pid unchanged') or
                        skip 'daemon died', 1;
+               skip 'not killing persistent lei-daemon', 2 if
+                               $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
                lei_ok 'daemon-kill';
                my $alive = 1;
                for (1..100) {
@@ -262,6 +264,8 @@ test_lei(sub {
        }
 
        {
+               skip 'TEST_LEI_DAEMON_PERSIST_DIR in use', 1 if
+                                       $ENV{TEST_LEI_DAEMON_PERSIST_DIR};
                opendir my $dh, '.' or BAIL_OUT "opendir(.) $!";
                my $od = PublicInbox::OnDestroy->new($$, sub {
                        chdir $dh or BAIL_OUT "chdir: $!"
index e8512e183acc84cceab436b223ca947f209e1ee2..1fb1c5f0b661c8ff1aaece7fdef105966050396a 100755 (executable)
@@ -14,6 +14,7 @@ use strict;
 use v5.10.1;
 use IO::Handle; # ->autoflush
 use PublicInbox::TestCommon;
+use PublicInbox::Spawn;
 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
 use Errno qw(EINTR);
 use Fcntl qw(:seek);
@@ -40,6 +41,20 @@ $OLDERR->autoflush(1);
 
 key2sub($_) for @tests; # precache
 
+my ($for_destroy, $lei_env, $lei_daemon_pid, $owner_pid);
+if (!$ENV{TEST_LEI_DAEMON_PERSIST_DIR} &&
+               (PublicInbox::Spawn->can('recv_cmd4') ||
+                       eval { require Socket::MsgHdr })) {
+       $lei_env = {};
+       ($lei_env->{XDG_RUNTIME_DIR}, $for_destroy) = tmpdir;
+       $ENV{TEST_LEI_DAEMON_PERSIST_DIR} = $lei_env->{XDG_RUNTIME_DIR};
+       run_script([qw(lei daemon-pid)], $lei_env, { 1 => \$lei_daemon_pid });
+       chomp $lei_daemon_pid;
+       $lei_daemon_pid =~ /\A[0-9]+\z/ or die "no daemon pid: $lei_daemon_pid";
+       kill(0, $lei_daemon_pid) or die "kill $lei_daemon_pid: $!";
+       $owner_pid = $$;
+}
+
 if ($shuffle) {
        require List::Util;
 } elsif (open(my $prove_state, '<', '.prove') && eval { require YAML::XS }) {
@@ -209,3 +224,7 @@ for (my $i = $repeat; $i != 0; $i--) {
 }
 
 print $OLDOUT "1..".($repeat * scalar(@tests))."\n" if $repeat >= 0;
+if ($lei_env && $$ == $owner_pid) {
+       my $opt = {}; # 1 => $OLDOUT, 2 => $OLDERR };
+       run_script([qw(lei daemon-kill)], $lei_env, $opt);
+}