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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
plan skip_all => 'set TEST_SUBPROCESS to enable this test (developer only!)'
unless $ENV{TEST_SUBPROCESS} || $ENV{TEST_ALL};
use Mojo::IOLoop;
use Mojo::IOLoop::Subprocess;
# Huge result
my ($fail, $result, @start);
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->on(spawn => sub { push @start, shift->pid });
$subprocess->run(
sub { shift->pid . $$ . ('x' x 100000) },
sub {
my ($subprocess, $err, $two) = @_;
$fail = $err;
$result .= $two;
}
);
$result = $$;
ok !$subprocess->pid, 'no process id available yet';
Mojo::IOLoop->start;
ok $subprocess->pid, 'process id available';
ok !$fail, 'no error';
is $result, $$ . 0 . $subprocess->pid . ('x' x 100000), 'right result';
is_deeply \@start, [$subprocess->pid], 'spawn event has been emitted once';
# Custom event loop
($fail, $result) = ();
my $loop = Mojo::IOLoop->new;
$loop->subprocess(
sub {'♥'},
sub {
my ($subprocess, $err, @results) = @_;
$fail = $err;
$result = \@results;
}
);
$loop->start;
ok !$fail, 'no error';
is_deeply $result, ['♥'], 'right structure';
# Multiple return values
($fail, $result) = ();
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub { return '♥', [{two => 2}], 3 },
sub {
my ($subprocess, $err, @results) = @_;
$fail = $err;
$result = \@results;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is_deeply $result, ['♥', [{two => 2}], 3], 'right structure';
# Event loop in subprocess
($fail, $result) = ();
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub {
my $result;
Mojo::IOLoop->next_tick(sub { $result = 23 });
Mojo::IOLoop->start;
return $result;
},
sub {
my ($subprocess, $err, $twenty_three) = @_;
$fail = $err;
$result = $twenty_three;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is $result, 23, 'right result';
# Concurrent subprocesses
($fail, $result) = ();
Mojo::IOLoop->delay(
sub {
my $delay = shift;
Mojo::IOLoop->subprocess(sub {1}, $delay->begin);
Mojo::IOLoop->subprocess->run(sub {2}, $delay->begin);
},
sub {
my ($delay, $err1, $result1, $err2, $result2) = @_;
$fail = $err1 || $err2;
$result = [$result1, $result2];
}
)->wait;
ok !$fail, 'no error';
is_deeply $result, [1, 2], 'right structure';
# No result
($fail, $result) = ();
Mojo::IOLoop::Subprocess->new->run(
sub {return},
sub {
my ($subprocess, $err, @results) = @_;
$fail = $err;
$result = \@results;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is_deeply $result, [], 'right structure';
# Stream inherited from previous subprocesses
($fail, $result) = ();
my $delay = Mojo::IOLoop->delay;
my $me = $$;
for (0 .. 1) {
my $end = $delay->begin;
my $subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub { 1 + 1 },
sub {
my ($subprocess, $err, $two) = @_;
$fail ||= $err;
push @$result, $two;
is $me, $$, 'we are the parent';
$end->();
}
);
}
$delay->wait;
ok !$fail, 'no error';
is_deeply $result, [2, 2], 'right structure';
# Exception
$fail = undef;
Mojo::IOLoop::Subprocess->new->run(
sub { die 'Whatever' },
sub {
my ($subprocess, $err) = @_;
$fail = $err;
}
);
Mojo::IOLoop->start;
like $fail, qr/Whatever/, 'right error';
# Non-zero exit status
$fail = undef;
Mojo::IOLoop::Subprocess->new->run(
sub { exit 3 },
sub {
my ($subprocess, $err) = @_;
$fail = $err;
}
);
Mojo::IOLoop->start;
like $fail, qr/Storable/, 'right error';
# Serialization error
$fail = undef;
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->deserialize(sub { die 'Whatever' });
$subprocess->run(
sub { 1 + 1 },
sub {
my ($subprocess, $err) = @_;
$fail = $err;
}
);
Mojo::IOLoop->start;
like $fail, qr/Whatever/, 'right error';
# Progress
($fail, $result) = (undef, undef);
my @progress;
$subprocess = Mojo::IOLoop::Subprocess->new;
$subprocess->run(
sub {
my $s = shift;
$s->progress(20);
$s->progress({percentage => 45});
$s->progress({percentage => 90}, {long_data => [1 .. 1e5]});
'yay';
},
sub {
my ($subprocess, $err, @res) = @_;
$fail = $err;
$result = \@res;
}
);
$subprocess->on(
progress => sub {
my ($subprocess, @args) = @_;
push @progress, \@args;
}
);
Mojo::IOLoop->start;
ok !$fail, 'no error';
is_deeply $result, ['yay'], 'correct result';
is_deeply \@progress,
[[20], [{percentage => 45}], [{percentage => 90}, {long_data => [1 .. 1e5]}]],
'correct progress';
done_testing();
|