about summary refs log tree commit homepage
path: root/t/lei-sigpipe.t
blob: d9738b07bd8e5ddce780e8125b1207a1686cc9ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!perl -w
# Copyright (C) 2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict;
use v5.10.1;
use PublicInbox::TestCommon;
use POSIX qw(WTERMSIG WIFSIGNALED SIGPIPE);
test_lei(sub {
	my $f = "$ENV{HOME}/big.eml";
	my $imported;
	for my $out ([], [qw(-f mboxcl2)], [qw(-f text)]) {
		pipe(my ($r, $w)) or BAIL_OUT $!;
		my $size = 65536;
		if ($^O eq 'linux' && fcntl($w, 1031, 4096)) {
			$size = 4096;
		}
		unless (-f $f) {
			open my $fh, '>', $f or xbail "open $f: $!";
			print $fh <<'EOM' or xbail;
From: big@example.com
Message-ID: <big@example.com>
EOM
			print $fh 'Subject:';
			print $fh (' '.('x' x 72)."\n") x (($size / 73) + 1);
			print $fh "\nbody\n";
			close $fh or xbail "close: $!";
		}

		lei_ok(qw(import), $f) if $imported++ == 0;
		open my $errfh, '+>', "$ENV{HOME}/stderr.log" or xbail $!;
		my $opt = { run_mode => 0, 2 => $errfh, 1 => $w };
		my $cmd = [qw(lei q -q -t), @$out, 'z:1..'];
		my $tp = start_script($cmd, undef, $opt);
		close $w;
		is(sysread($r, my $buf, 1), 1, 'read one byte');
		close $r; # trigger SIGPIPE
		$tp->join;
		ok(WIFSIGNALED($?), "signaled @$out");
		is(WTERMSIG($?), SIGPIPE, "got SIGPIPE @$out");
		seek($errfh, 0, 0) or xbail $!;
		my $s = do { local $/; <$errfh> };
		is($s, '', "quiet after sigpipe @$out");
	}
});

done_testing;