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