lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / lei-sigpipe.t
blobb9fd88a672ee54b721b3c277351dd5b83afdd8ae
1 #!perl -w
2 # Copyright (C) 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 PublicInbox::TestCommon;
7 use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
8 use PublicInbox::OnDestroy;
9 use PublicInbox::Syscall qw($F_SETPIPE_SZ);
10 use autodie qw(close open pipe seek sysread);
11 use PublicInbox::IO qw(write_file);
12 my $inboxdir = $ENV{GIANT_INBOX_DIR};
13 SKIP: {
14         $inboxdir // skip 'GIANT_INBOX_DIR unset to test large results', 1;
15         require PublicInbox::Inbox;
16         my $ibx = PublicInbox::Inbox->new({
17                 name => 'unconfigured-test',
18                 address => [ "test\@example.com" ],
19                 inboxdir => $inboxdir,
20         });
21         $ibx->search or xbail "GIANT_INBOX_DIR=$inboxdir has no search";
24 # undo systemd (and similar) ignoring SIGPIPE, since lei expects to be run
25 # from an interactive terminal:
26 # https://public-inbox.org/meta/20220227080422.gyqowrxomzu6gyin@sourcephile.fr/
27 my $oldSIGPIPE = $SIG{PIPE};
28 $SIG{PIPE} = 'DEFAULT';
29 my $cleanup = on_destroy(sub { $SIG{PIPE} = $oldSIGPIPE });
31 test_lei(sub {
32         my $f = "$ENV{HOME}/big.eml";
33         my $imported;
34         for my $out ([], [qw(-f mboxcl2)], [qw(-f text)]) {
35                 pipe(my $r, my $w);
36                 my $size = $F_SETPIPE_SZ && fcntl($w, $F_SETPIPE_SZ, 4096) ?
37                         4096 : 65536;
38                 unless (-f $f) {
39                         my $fh = write_file '>', $f, <<'EOM';
40 From: big@example.com
41 Message-ID: <big@example.com>
42 EOM
43                         print $fh 'Subject:';
44                         print $fh (' '.('x' x 72)."\n") x (($size / 73) + 1);
45                         print $fh "\nbody\n";
46                         close $fh;
47                 }
49                 lei_ok(qw(import), $f) if $imported++ == 0;
50                 open my $errfh, '+>>', "$ENV{HOME}/stderr.log";
51                 my $opt = { run_mode => 0, 2 => $errfh, 1 => $w };
52                 my $cmd = [qw(lei q -q -t), @$out, 'z:1..'];
53                 push @$cmd, '--only='.$inboxdir if defined $inboxdir;
54                 my $tp = start_script($cmd, undef, $opt);
55                 close $w;
56                 vec(my $rvec = '', fileno($r), 1) = 1;
57                 if (!select($rvec, undef, undef, 30)) {
58                         seek($errfh, 0, 0);
59                         my $s = do { local $/; <$errfh> };
60                         xbail "lei q had no output after 30s, stderr=$s";
61                 }
62                 is(sysread($r, my $buf, 1), 1, 'read one byte');
63                 close $r; # trigger SIGPIPE
64                 $tp->join;
65                 ok(WIFSIGNALED($?), "signaled @$out");
66                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
67                 seek($errfh, 0, 0);
68                 my $s = do { local $/; <$errfh> };
69                 is($s, '', "quiet after sigpipe @$out");
70         }
71 });
73 done_testing;