Async-Simple-Pool

 view release on metacpan or  search on metacpan

t/05-task-fork.t  view on Meta::CPAN

#!perl -T

use Modern::Perl;
use POSIX;
use Test::Spec;
use Test::Exception;
use Time::HiRes qw/ sleep /;

# use lib '../lib';

use Async::Simple::Task::Fork;

if ( $^O =~ /^(dos|os2|MSWin32|NetWare)$/ ) {
    plan tests => 1;

    describe 'All' => sub {
        it 'fallback' => sub {
            ok( 1, 'Your OS does not support threads... fallback to tmpfiles' );
        }
    };

    runtests unless caller;

    exit;
};


plan tests => 18;

my $task;
my $timeout    = 1;
my $sub        = sub { sleep $timeout; return $_[0]-1  };
my $sub_scalar = sub { return $_[0]+1                  };
my $sub_hash   = sub { return { y => $_[0]->{x}+1 }    };
my $sub_array  = sub { return [ reverse @{ $_[0] } ]   };

describe 'All' => sub {

    describe 'init data' => sub {

#         it 'init errors' => sub {
#             # Create a fork process, which will wait for data and execute &$sub if data will be passed
#             ok( ! eval{ Async::Simple::Task::Fork->new( ) } && $@, 'no params' );
#             undef $@;
#             ok( ! eval{ Async::Simple::Task::Fork->new( task => 1234 ) } && $@, 'bad params' );
#             undef $@;
#         };

        it 'init ok' => sub {
            my $task = Async::Simple::Task::Fork->new( task => $sub, id => 12345 );
            my $pid = $task->pid;

            isa_ok( $task, 'Async::Simple::Task::Fork', 'successful init' );
            isa_ok( $task->reader, 'GLOB', 'have a reader' );
            isa_ok( $task->writer, 'GLOB', 'have a writer' );
            isa_ok( $task->task, 'CODE', 'task exists and is a CODE' );

            is( $task->timeout, 0.01, 'default timeout' );
            is( $task->id, 12345, 'id found' );
            ok( $task->pid =~ /^\d{2,}$/, 'fork done, pid setted' );
            is( $task->kill_on_exit, 1, 'default kill_on_exit' );
            is( waitpid( $task->pid, WNOHANG ), 0, 'kid is active' );
            undef $task;



( run in 0.943 second using v1.01-cache-2.11-cpan-39bf76dae61 )