]> Sergey Matveev's repositories - public-inbox.git/blob - t/spawn.t
doc: re-add missing 1.6 release notes
[public-inbox.git] / t / spawn.t
1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::Spawn qw(which spawn popen_rd);
7 use PublicInbox::Sigfd;
8
9 {
10         my $true = which('true');
11         ok($true, "'true' command found with which()");
12 }
13
14 {
15         my $pid = spawn(['true']);
16         ok($pid, 'spawned process');
17         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
18         is($?, 0, 'true exited successfully');
19 }
20
21 { # ensure waitpid(-1, 0) and SIGCHLD works in spawned process
22         my $script = <<'EOF';
23 $| = 1; # unbuffer stdout
24 defined(my $pid = fork) or die "fork: $!";
25 if ($pid == 0) { exit }
26 elsif ($pid > 0) {
27         my $waited = waitpid(-1, 0);
28         $waited == $pid or die "mismatched child $pid != $waited";
29         $? == 0 or die "child err: $>";
30         $SIG{CHLD} = sub { print "HI\n"; exit };
31         print "RDY $$\n";
32         select(undef, undef, undef, 0.01) while 1;
33 }
34 EOF
35         my $oldset = PublicInbox::DS::block_signals();
36         my $rd = popen_rd([$^X, '-e', $script]);
37         diag 'waiting for child to reap grandchild...';
38         chomp(my $line = readline($rd));
39         my ($rdy, $pid) = split(' ', $line);
40         is($rdy, 'RDY', 'got ready signal, waitpid(-1) works in child');
41         ok(kill('CHLD', $pid), 'sent SIGCHLD to child');
42         is(readline($rd), "HI\n", '$SIG{CHLD} works in child');
43         ok(close $rd, 'popen_rd close works');
44         PublicInbox::DS::sig_setmask($oldset);
45 }
46
47 {
48         my ($r, $w);
49         pipe $r, $w or die "pipe failed: $!";
50         my $pid = spawn(['echo', 'hello world'], undef, { 1 => fileno($w) });
51         close $w or die "close pipe[1] failed: $!";
52         is(<$r>, "hello world\n", 'read stdout of spawned from pipe');
53         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
54         is($?, 0, 'true exited successfully');
55 }
56
57 {
58         my ($r, $w);
59         pipe $r, $w or die "pipe failed: $!";
60         my $pid = spawn(['sh', '-c', 'echo $HELLO'],
61                 { 'HELLO' => 'world' }, { 1 => $w });
62         close $w or die "close pipe[1] failed: $!";
63         is(<$r>, "world\n", 'read stdout of spawned from pipe');
64         is(waitpid($pid, 0), $pid, 'waitpid succeeds on spawned process');
65         is($?, 0, 'sh exited successfully');
66 }
67
68 {
69         my $fh = popen_rd([qw(echo hello)]);
70         ok(fileno($fh) >= 0, 'tied fileno works');
71         my $l = <$fh>;
72         is($l, "hello\n", 'tied readline works');
73         $l = <$fh>;
74         ok(!$l, 'tied readline works for EOF');
75 }
76
77 {
78         my $fh = popen_rd([qw(printf foo\nbar)]);
79         ok(fileno($fh) >= 0, 'tied fileno works');
80         my @line = <$fh>;
81         is_deeply(\@line, [ "foo\n", 'bar' ], 'wantarray works on readline');
82 }
83
84 {
85         my $fh = popen_rd([qw(echo hello)]);
86         my $buf;
87         is(sysread($fh, $buf, 6), 6, 'sysread got 6 bytes');
88         is($buf, "hello\n", 'tied gets works');
89         is(sysread($fh, $buf, 6), 0, 'sysread got EOF');
90         $? = 1;
91         ok(close($fh), 'close succeeds');
92         is($?, 0, '$? set properly');
93 }
94
95 {
96         my $fh = popen_rd([qw(false)]);
97         ok(!close($fh), 'close fails on false');
98         isnt($?, 0, '$? set properly: '.$?);
99 }
100
101 { # ->CLOSE vs ->DESTROY waitpid caller distinction
102         my @c;
103         my $fh = popen_rd(['true'], undef, { cb => sub { @c = caller } });
104         ok(close($fh), '->CLOSE fired and successful');
105         ok(scalar(@c), 'callback fired by ->CLOSE');
106         ok(grep(!m[/PublicInbox/DS\.pm\z], @c), 'callback not invoked by DS');
107
108         @c = ();
109         $fh = popen_rd(['true'], undef, { cb => sub { @c = caller } });
110         undef $fh; # ->DESTROY
111         ok(scalar(@c), 'callback fired by ->DESTROY');
112         ok(grep(!m[/PublicInbox/ProcessPipe\.pm\z], @c),
113                 'callback not invoked by ProcessPipe');
114 }
115
116 { # children don't wait on siblings
117         use POSIX qw(_exit);
118         pipe(my ($r, $w)) or BAIL_OUT $!;
119         my $cb = sub { warn "x=$$\n" };
120         my $fh = popen_rd(['cat'], undef, { 0 => $r, cb => $cb });
121         my $pp = tied *$fh;
122         my $pid = fork // BAIL_OUT $!;
123         local $SIG{__WARN__} = sub { _exit(1) };
124         if ($pid == 0) {
125                 local $SIG{__DIE__} = sub { _exit(2) };
126                 undef $fh;
127                 _exit(0);
128         }
129         waitpid($pid, 0);
130         is($?, 0, 'forked process exited');
131         my @w;
132         local $SIG{__WARN__} = sub { push @w, @_ };
133         close $w;
134         close $fh;
135         is($?, 0, 'cat exited');
136         is_deeply(\@w, [ "x=$$\n" ], 'callback fired from owner');
137 }
138
139 SKIP: {
140         eval {
141                 require BSD::Resource;
142                 defined(BSD::Resource::RLIMIT_CPU())
143         } or skip 'BSD::Resource::RLIMIT_CPU missing', 3;
144         my ($r, $w);
145         pipe($r, $w) or die "pipe: $!";
146         my $cmd = ['sh', '-c', 'while true; do :; done'];
147         my $fd = fileno($w);
148         my $opt = { RLIMIT_CPU => [ 1, 1 ], RLIMIT_CORE => [ 0, 0 ], 1 => $fd };
149         my $pid = spawn($cmd, undef, $opt);
150         close $w or die "close(w): $!";
151         my $rset = '';
152         vec($rset, fileno($r), 1) = 1;
153         ok(select($rset, undef, undef, 5), 'child died before timeout');
154         is(waitpid($pid, 0), $pid, 'XCPU child process reaped');
155         isnt($?, 0, 'non-zero exit status');
156 }
157
158 done_testing();
159
160 1;