]> Sergey Matveev's repositories - public-inbox.git/blob - t/qspawn.t
git-http-backend: use qspawn to limit running processes
[public-inbox.git] / t / qspawn.t
1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use Test::More;
4 use_ok 'PublicInbox::Qspawn';
5 {
6         my $x = PublicInbox::Qspawn->new([qw(true)]);
7         my $run = 0;
8         $x->start(sub {
9                 my ($rpipe) = @_;
10                 is(0, sysread($rpipe, my $buf, 1), 'read zero bytes');
11                 ok(!$x->finish, 'no error on finish');
12                 $run = 1;
13         });
14         is($run, 1, 'callback ran alright');
15 }
16
17 {
18         my $x = PublicInbox::Qspawn->new([qw(false)]);
19         my $run = 0;
20         $x->start(sub {
21                 my ($rpipe) = @_;
22                 is(0, sysread($rpipe, my $buf, 1), 'read zero bytes from false');
23                 my $err = $x->finish;
24                 is($err, 256, 'error on finish');
25                 $run = 1;
26         });
27         is($run, 1, 'callback ran alright');
28 }
29
30 foreach my $cmd ([qw(sleep 1)], [qw(sh -c), 'sleep 1; false']) {
31         my $s = PublicInbox::Qspawn->new($cmd);
32         my @run;
33         $s->start(sub {
34                 my ($rpipe) = @_;
35                 push @run, 'sleep';
36                 is(0, sysread($rpipe, my $buf, 1), 'read zero bytes');
37         });
38         my $n = 0;
39         my @t = map {
40                 my $i = $n++;
41                 my $x = PublicInbox::Qspawn->new([qw(true)]);
42                 $x->start(sub {
43                         my ($rpipe) = @_;
44                         push @run, $i;
45                 });
46                 [$x, $i]
47         } (0..2);
48
49         if ($cmd->[-1] =~ /false\z/) {
50                 ok($s->finish, 'got error on false after sleep');
51         } else {
52                 ok(!$s->finish, 'no error on sleep');
53         }
54         ok(!$_->[0]->finish, "true $_->[1] succeeded") foreach @t;
55         is_deeply([qw(sleep 0 1 2)], \@run, 'ran in order');
56 }
57
58 done_testing();
59
60 1;