1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 use_ok 'PublicInbox::Qspawn';
8 my $cmd = [qw(sh -c), 'echo >&2 err; echo out'];
9 my $qsp = PublicInbox::Qspawn->new($cmd, {}, { 2 => 1 });
11 $qsp->psgi_qx({}, undef, sub { $res = ${$_[0]} });
12 is($res, "err\nout\n", 'captured stderr and stdout');
15 $qsp = PublicInbox::Qspawn->new($cmd, {}, { 2 => \*STDOUT });
16 $qsp->psgi_qx({}, undef, sub { $res = ${$_[0]} });
17 is($res, "err\nout\n", 'captured stderr and stdout');
26 my $limiter = PublicInbox::Qspawn::Limiter->new(1);
28 my $x = PublicInbox::Qspawn->new([qw(true)]);
30 $x->start($limiter, sub {
32 is(0, sysread($self->{rpipe}, my $buf, 1), 'read zero bytes');
33 ok(!finish_err($self), 'no error on finish');
36 is($run, 1, 'callback ran alright');
40 my $x = PublicInbox::Qspawn->new([qw(false)]);
42 $x->start($limiter, sub {
44 is(0, sysread($self->{rpipe}, my $buf, 1),
45 'read zero bytes from false');
46 ok(finish_err($self), 'error on finish');
49 is($run, 1, 'callback ran alright');
52 foreach my $cmd ([qw(sleep 1)], [qw(sh -c), 'sleep 1; false']) {
53 my $s = PublicInbox::Qspawn->new($cmd);
55 $s->start($limiter, sub {
58 is(0, sysread($self->{rpipe}, my $buf, 1), 'read zero bytes');
63 my $x = PublicInbox::Qspawn->new([qw(true)]);
64 $x->start($limiter, sub {
71 if ($cmd->[-1] =~ /false\z/) {
72 ok(finish_err($s), 'got error on false after sleep');
74 ok(!finish_err($s), 'no error on sleep');
76 ok(!finish_err($_->[0]), "true $_->[1] succeeded") foreach @t;
77 is_deeply([qw(sleep 0 1 2)], \@run, 'ran in order');