Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests for basic functionality and addition of nonblocking functionality + test #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions Job.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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__
Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Job.xs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions t/basic.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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";
}

{
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 {
my ( $job, @args ) = @_;
$job->spawn( $Config{perlpath}, @args );
return;
}