From 76f9d929d1ac5b0f7f6a3ce5cd1386035ae8819e Mon Sep 17 00:00:00 2001 From: Christian Walde Date: Sun, 16 Mar 2014 14:08:35 +0100 Subject: [PATCH 1/2] functionality test --- t/basic.t | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 t/basic.t diff --git a/t/basic.t b/t/basic.t new file mode 100644 index 0000000..c4fc37e --- /dev/null +++ b/t/basic.t @@ -0,0 +1,33 @@ +use strict; +use warnings; +use Test::More; +use Win32::Job; +use Config; +use File::Temp qw/ tempfile /; + +{ + my @outs = map [tempfile], 1 .. 2; + my $job = Win32::Job->new; + spawn_perl( $job, qq{perl -e "print 5$_"}, { stdout => $outs[$_][0] } ) for 0 .. 1; + ok $job->run( 5 * 60 ), "processes succeeded"; + close $_->[0] for @outs; + @outs = map { open my $fh, '<', $_->[1]; local $/; my $s = <$fh>; $s } @outs; + is $outs[0], 50, "first output is correct"; + is $outs[1], 51, "second output is correct"; +} + +{ + my $job = Win32::Job->new; + my $t = time; + spawn_perl( $job, qq{perl -e "sleep 5"} ); + ok !$job->run( 2 ), "process was timed out"; + cmp_ok time - $t, '<', 5, "less than the sleep time was used"; +} + +done_testing; + +sub spawn_perl { + my ( $job, @args ) = @_; + $job->spawn( $Config{perlpath}, @args ); + return; +} From b5f43e40823abf27788adde30cbe556a8c0132b6 Mon Sep 17 00:00:00 2001 From: Christian Walde Date: Sun, 16 Mar 2014 14:10:09 +0100 Subject: [PATCH 2/2] patch for non-blocking job runs (RT#45622) + test --- Job.pm | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- Job.xs | 6 ++++++ t/basic.t | 12 ++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/Job.pm b/Job.pm index c6c5cfc..8263cf7 100644 --- a/Job.pm +++ b/Job.pm @@ -9,6 +9,8 @@ $VERSION = '0.05'; use constant WIN32s => 0; use constant WIN9X => 1; use constant WINNT => 2; +use constant STILL_ACTIVE_EXITCODE => 259; +use constant KILLED_EXITCODE => 293; require Win32 unless defined &Win32::GetOSVersion; my @ver = Win32::GetOSVersion; @@ -22,6 +24,13 @@ die "Win32::Job is not supported on $ver[0]" unless ( Win32::Job->bootstrap($VERSION); +sub is_running { + my ( $self ) = @_; + my @return_values = map $_->{exitcode}, values %{ $self->status }; + my $processes_running = grep { $_ == STILL_ACTIVE_EXITCODE } @return_values; + return $processes_running; +} + 1; __END__ @@ -286,6 +295,32 @@ the file becomes larger than a certain limit: }, 1); print "Mod1 built ok!\n" if $ok; +=item 4 + +start() + + start() + +Start the job, but don't wait for it to finish. Has no return value. You can +check whether it's finished with is_running() or looking at the result of +status(). + +Here's equivalent code to the watch() example, except using start(): + + use Win32::Job; + $job = Win32::Job->new; + $job->spawn("cmd", q{cmd /C "cd Mod1 && nmake"}, { + stdin => 'NUL', # the NUL device + stdout => 'stdout.log', + stderr => 'stdout.log', + }); + $job->start(); + while ( $job->is_running() ) { + sleep(1); + $job->kill if -s "stdout.log" > 1_000_000; + } + print "Mod1 built ok!\n" if $ok; + =item 5 status() @@ -304,8 +339,11 @@ containing the following keys: =item exitcode -The exit code returned by the process. If the process was killed because -of a timeout, the value is 293. +The exit code returned by the process. If the process was killed, the value is +293, if the process is still running, the value is 259. + +You can also use the constant Win32::Job::KILLED_EXITCODE or +Win32::Job::STILL_ACTIVE_EXITCODE. =item time @@ -326,6 +364,14 @@ Kills all processes and subprocesses in the Job. Has no return value. Sets the exit code to all processes killed to 293, which you can check for in the status() return value. +=item 7 + +is_running() + + is_running() + +Returns the number of processes currently running. + =back =head1 SEE ALSO diff --git a/Job.xs b/Job.xs index b55552e..b9eb31a 100644 --- a/Job.xs +++ b/Job.xs @@ -638,6 +638,12 @@ watch(self, callback, interval, ...) OUTPUT: RETVAL +void +start(self) + JOB_T self + CODE: + resume_threads(aTHX_ self->procs); + HV* status(self) JOB_T self diff --git a/t/basic.t b/t/basic.t index c4fc37e..d855d19 100644 --- a/t/basic.t +++ b/t/basic.t @@ -24,6 +24,18 @@ use File::Temp qw/ tempfile /; cmp_ok time - $t, '<', 5, "less than the sleep time was used"; } +{ + my $job = Win32::Job->new; + spawn_perl( $job, qq{perl -e "sleep 1"} ); + $job->start; + my $counter; + while ( $job->is_running ) { $counter++ } + my ( $process ) = values %{ $job->status }; + ok !$process->{exitcode}, "process succeeded"; + cmp_ok $process->{time}{elapsed}, '>', 0.9, "process did sleep"; + cmp_ok $counter, '>', 1, "work was done while waiting"; +} + done_testing; sub spawn_perl {