]> Sergey Matveev's repositories - public-inbox.git/blob - t/lei-sigpipe.t
7fab9aebb0ff0a1faae0a57b2532ff9d2853aa7b
[public-inbox.git] / t / lei-sigpipe.t
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 SIG_UNBLOCK SIG_SETMASK sigprocmask);
8 use PublicInbox::OnDestroy;
9
10 # undo systemd (and similar) blocking SIGPIPE, since lei expects to be run
11 # from an interactive terminal:
12 # https://public-inbox.org/meta/20220227080422.gyqowrxomzu6gyin@sourcephile.fr/
13 my $set = POSIX::SigSet->new;
14 my $old = POSIX::SigSet->new;
15 $set->emptyset or xbail "sigemptyset $!";
16 $old->emptyset or xbail "sigemptyset $!";
17 $set->addset(SIGPIPE);
18 sigprocmask(SIG_UNBLOCK, $set, $old) or xbail "SIG_UNBLOCK: $!";
19 my $cleanup = PublicInbox::OnDestroy->new($$, sub {
20         sigprocmask(SIG_SETMASK, $old);
21 });
22
23 test_lei(sub {
24         my $f = "$ENV{HOME}/big.eml";
25         my $imported;
26         for my $out ([], [qw(-f mboxcl2)], [qw(-f text)]) {
27                 pipe(my ($r, $w)) or BAIL_OUT $!;
28                 my $size = 65536;
29                 if ($^O eq 'linux' && fcntl($w, 1031, 4096)) {
30                         $size = 4096;
31                 }
32                 unless (-f $f) {
33                         open my $fh, '>', $f or xbail "open $f: $!";
34                         print $fh <<'EOM' or xbail;
35 From: big@example.com
36 Message-ID: <big@example.com>
37 EOM
38                         print $fh 'Subject:';
39                         print $fh (' '.('x' x 72)."\n") x (($size / 73) + 1);
40                         print $fh "\nbody\n";
41                         close $fh or xbail "close: $!";
42                 }
43
44                 lei_ok(qw(import), $f) if $imported++ == 0;
45                 open my $errfh, '+>>', "$ENV{HOME}/stderr.log" or xbail $!;
46                 my $opt = { run_mode => 0, 2 => $errfh, 1 => $w };
47                 my $cmd = [qw(lei q -q -t), @$out, 'z:1..'];
48                 my $tp = start_script($cmd, undef, $opt);
49                 close $w;
50                 vec(my $rvec = '', fileno($r), 1) = 1;
51                 if (!select($rvec, undef, undef, 30)) {
52                         seek($errfh, 0, 0) or xbail $!;
53                         my $s = do { local $/; <$errfh> };
54                         xbail "lei q had no output after 30s, stderr=$s";
55                 }
56                 is(sysread($r, my $buf, 1), 1, 'read one byte');
57                 close $r; # trigger SIGPIPE
58                 $tp->join;
59                 ok(WIFSIGNALED($?), "signaled @$out");
60                 is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
61                 seek($errfh, 0, 0) or xbail $!;
62                 my $s = do { local $/; <$errfh> };
63                 is($s, '', "quiet after sigpipe @$out");
64         }
65 });
66
67 done_testing;