#!/usr/bin/perl -T
local
$SIG
{ALRM} =
sub
{
die
"timeout\n"
; };
alarm
120;
is( proc_count, 0,
"no processes running before spawning any"
);
my
$wu
;
my
$PROCS
= 10;
for
( 0 ..
$PROCS
- 1 ) {
async
sub
{
return
$_
; };
is( proc_count, 1 +
$_
, 1 +
$_
.
" workers are executing"
);
}
my
$r
= waitone;
ok(
defined
(
$r
),
"waitone() returned a defined value"
);
ok( (
$r
>= 0 ) && (
$r
<
$PROCS
),
"waitone() returned a valid return value"
);
is( proc_count,
$PROCS
- 1,
"waitone() properly reaped one process"
);
my
(
@results
) = waitall;
for
( 0 ..
$PROCS
- 1 ) {
ok(
exists
(
$results
[
$_
] ),
"Worker First Exec $_ returned properly"
);
}
is( proc_count, 0,
"no processes running after waitall()"
);
$PROCS
= 10;
asyncs(
$PROCS
,
sub
{
return
$_
; } );
is( proc_count,
$PROCS
,
"asyncs - $PROCS workers are executing"
);
$r
= waitone;
ok(
defined
(
$r
),
"asyncs - waitone() returned a defined value"
);
ok( (
$r
>= 0 ) && (
$r
<
$PROCS
),
"asyncs - waitone() returned a valid return value"
);
is( proc_count,
$PROCS
- 1,
"asyncs - waitone() properly reaped one process"
);
(
@results
) = waitall;
for
( 0 ..
$PROCS
- 1 ) {
ok(
exists
(
$results
[
$_
] ),
"asyncs - Worker First Exec $_ returned properly"
);
}
is( proc_count, 0,
"asyncs - no processes running after waitall()"
);
@results
= ();
for
( 0 ..
$PROCS
- 1 ) {
async
sub
{
return
$_
+ 100 };
}
@results
= waitall;
for
( 0 ..
$PROCS
- 1 ) {
is(
$results
[
$_
],
$_
+ 100,
"Worker Second Exec $_ returned properly"
);
}
queue
sub
{
return
'BIG'
x 500000; };
@results
= waitall;
is(
$results
[0],
'BIG'
x 500000,
'Result for big return callback as expected'
);
async
sub
{
my
@ret
;
for
(
my
$i
= 0;
$i
< 50000;
$i
++ ) {
push
@ret
,
$i
; }
return
\
@ret
;
};
@results
= waitall;
is( Scalar::Util::reftype(
$results
[0] ),
'ARRAY'
,
'Array reference properly returned'
);
my
@cmp
;
for
(
my
$i
= 0;
$i
< 50000;
$i
++ ) {
push
@cmp
,
$i
; }
is(
$results
[0], \
@cmp
,
'Array reference contains proper values'
);
async
sub
{
die
"Error!"
; };
ok dies { waitall },
'Die when child throws an error'
;
@results
= ();
async
sub
{
return
; };
@results
= waitall;
ok(
exists
(
$results
[0] ),
'Callback from undef returning fork called'
);
ok( !
defined
(
$results
[0] ),
'Callback received undef from fork returning undef'
);
my
$pid
= async
sub
{
return
'HERE'
; };
proc_wait(
$pid
);
proc_wait(
$pid
);
pass(
"Duplicate wait() call exits properly"
);
waitall;
pass(
"Unnecessary waitall() call exits properly"
);
ok( !
defined
(waitone),
'Unnecessary waitone() call exits properly'
);
my
$result
;
async
sub
{ 0 },
sub
{
$result
= 555 };
waitall;
is(
$result
, 555,
"Result from async() with callback"
);
done_testing;