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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
use POSIX;
use FindBin;
use Mojo::File qw(tempfile path);
use lib ("$FindBin::Bin/lib", "../lib", "lib");
use Mojo::IOLoop::ReadWriteProcess qw(parallel batch process pool);
subtest parallel => sub {
my $n_proc = 4;
my $fired;
my $c = parallel(
code => sub { sleep 2; print "Hello world\n"; },
kill_sleeptime => 1,
sleeptime_during_kill => 1,
separate_err => 1,
set_pipes => 1,
$n_proc
);
isa_ok($c, "Mojo::IOLoop::ReadWriteProcess::Pool");
is $c->size(), $n_proc;
$c->once(stop => sub { $fired++; });
$c->start();
$c->each(sub { my $p = shift; $p->wait; is $p->getline(), "Hello world\n"; });
$c->wait_stop;
is $fired, $n_proc;
$c->once(stop => sub { $fired++ });
my $b = $c->restart();
is $b, $c;
sleep 3;
$c->wait_stop;
is $fired, $n_proc * 2;
};
subtest batch => sub {
my @stack;
my $n_proc = 2;
my $fired;
push(
@stack,
process(
code => sub { sleep 2; print "Hello world\n" },
separate_err => 0,
set_pipes => 1
)) for (1 .. $n_proc);
my $c = batch @stack;
isa_ok($c, "Mojo::IOLoop::ReadWriteProcess::Pool");
is $c->size(), $n_proc;
$c->once(stop => sub { $fired++; });
my @procs = $c->start();
$c->each(sub { my $p = shift; $p->wait; is $p->getline(), "Hello world\n"; });
$c->wait_stop;
is $fired, $n_proc;
is scalar(@procs), $n_proc;
$c->add(
code => sub { print "Hello world 3\n" },
separate_err => 0,
set_pipes => 1
);
$c->start();
is $c->last->getline, "Hello world 3\n";
$c->wait_stop();
my $result;
$c->add(code => sub { return 40 + 2 }, separate_err => 0, set_pipes => 0);
$c->last->on(
stop => sub {
$result = shift->return_status;
});
$c->last->start()->wait_stop();
is $result, 42;
};
subtest "Working with pools" => sub {
my $n_proc = 5;
my $number = 1;
my $pool = batch;
for (1 .. $n_proc) {
$pool->add(
code => sub {
my $self = shift;
my $number = shift;
sleep 2;
return 40 + $number;
},
args => $number,
set_pipes => 0,
separate_err => 0,
kill_sleeptime => 1,
sleeptime_during_kill => 1,
);
$number++;
}
my $results;
$pool->once(stop => sub { $results->{+shift()->return_status}++; });
$pool->start->wait_stop;
my $i = 1;
for (1 .. $n_proc) {
is $results->{40 + $i}, 1;
$i++;
}
ok $pool->get(0) != $pool->get(1);
ok $pool->get(3);
$pool->remove(3);
is $pool->get(3), undef;
};
subtest maximum_processes => sub {
my $p = pool();
$p->maximum_processes(1);
$p->add(sub { print "Hello\n" });
$p->add(sub { print "Wof\n" });
$p->add(sub { print "Wof2\n" });
is $p->get(1), undef;
is $p->size, 1;
is $p->maximum_processes, 1;
};
subtest stress_test => sub {
plan skip_all => "set STRESS_TEST=1 (be careful)" unless $ENV{STRESS_TEST};
# Push the maximum_processes boundaries and let's see events are fired.
my $n_proc = 2000;
my $fired;
my $p = pool;
$p->maximum_processes($n_proc);
$p->add(
code => sub { sleep 3; exit(20) },
internal_pipes => 0,
set_pipes => 0
) for 1 .. $n_proc;
$p->once(stop => sub { $fired++ });
$p->start->wait;
is $fired, $n_proc;
$p->each(sub { is $_->exit_status, "20" });
};
done_testing;
|