]> Sergey Matveev's repositories - public-inbox.git/blob - xt/lei-sigpipe.t
lei q: do not leave temporary files after oneshot exit
[public-inbox.git] / xt / lei-sigpipe.t
1 #!perl -w
2 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use Test::More;
7 use PublicInbox::TestCommon;
8 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
9 require_mods(qw(json DBD::SQLite Search::Xapian));
10 # XXX this needs an already configured lei instance with many messages
11
12 my $do_test = sub {
13         my $env = shift // {};
14         for my $out ([], [qw(-f mboxcl2)]) {
15                 pipe(my ($r, $w)) or BAIL_OUT $!;
16                 open my $err, '+>', undef or BAIL_OUT $!;
17                 my $opt = { run_mode => 0, 1 => $w, 2 => $err };
18                 my $cmd = [qw(lei q -q -t), @$out, 'bytes:1..'];
19                 my $tp = start_script($cmd, $env, $opt);
20                 close $w;
21                 sysread($r, my $buf, 1);
22                 close $r; # trigger SIGPIPE
23                 $tp->join;
24                 ok(WIFSIGNALED($?), "signaled @$out");
25                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
26                 seek($err, 0, 0);
27                 my @err = grep(!m{mkdir /dev/null\b}, <$err>);
28                 is_deeply(\@err, [], "no errors @$out");
29         }
30 };
31
32 my ($tmp, $for_destroy) = tmpdir();
33 my $pid;
34 my $opt = { run_mode => 0, 1 => \(my $out = '') };
35 if (run_script([qw(lei daemon-pid)], undef, $opt)) {
36         chomp($pid = $out);
37         mkdir "$tmp/d" or BAIL_OUT $!;
38         local $ENV{TMPDIR} = "$tmp/d";
39         $do_test->();
40         $out = '';
41         ok(run_script([qw(lei daemon-pid)], undef, $opt), 'daemon-pid again');
42         chomp($out);
43         is($out, $pid, 'daemon-pid unchanged');
44         ok(kill(0, $pid), 'daemon still running');
45         $out = '';
46 }
47 {
48         mkdir "$tmp/1" or BAIL_OUT $!;
49         local $ENV{TMPDIR} = "$tmp/1";
50         $do_test->({XDG_RUNTIME_DIR => '/dev/null'});
51         is(unlink(glob("$tmp/1/*")), 0, 'nothing left over w/ oneshot');
52 }
53
54 # the one-shot test should be slow enough that the daemon has cleaned
55 # up in the background:
56 is_deeply([glob("$tmp/d/*")], [], 'nothing left over with daemon');
57
58 done_testing;