1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#!/usr/bin/perl -w
=head1 NAME
parallel.t - Test suite for running multiple processes in parallel.
=cut
BEGIN {
if( $ENV{PERL_CORE} ) {
chdir '../lib/IPC/Run' if -d '../lib/IPC/Run';
unshift @INC, 'lib', '../..';
$^X = '../../../t/' . $^X;
}
}
## Handy to have when our output is intermingled with debugging output sent
## to the debugging fd.
$| = 1 ;
select STDERR ; $| = 1 ; select STDOUT ;
use strict ;
use Test ;
use IPC::Run qw( start pump finish ) ;
use UNIVERSAL qw( isa ) ;
sub Win32_MODE() ;
*Win32_MODE = \&IPC::Run::Win32_MODE ;
## Win32 does not support a lot of things that Unix does. These
## skip_unless subs help that.
##
## TODO: There are also a few things that Win32 supports (passing Win32 OS
## handles) that we should test for, conversely.
sub skip_unless_subs(&) {
if ( Win32_MODE ) {
return sub {
skip "Can't spawn subroutines on $^O", 0 ;
} ;
}
shift ;
}
my $text1 = "Hello world 1\n" ;
my $text2 = "Hello world 2\n" ;
my @perl = ( $^X ) ;
my @catter = ( @perl, '-pe1' ) ;
sub slurp($) {
my ( $f ) = @_ ;
open( S, "<$f" ) or return "$! $f" ;
my $r = join( '', <S> ) ;
close S ;
return $r ;
}
sub spit($$) {
my ( $f, $s ) = @_ ;
open( S, ">$f" ) or die "$! $f" ;
print S $s or die "$! $f" ;
close S or die "$! $f" ;
}
my ( $h1, $h2 ) ;
my ( $out1, $out2 ) ;
my @tests = (
sub {
$h1 = start \@catter, "<", \$text1, ">", \$out1 ;
ok $h1 ;
},
sub {
$h2 = start \@catter, "<", \$text2, ">", \$out2 ;
ok $h2 ;
},
sub {
pump $h1 ;
ok 1 ;
},
sub {
pump $h2 ;
ok 1 ;
},
sub {
finish $h1 ;
ok 1 ;
},
sub {
finish $h2 ;
ok 1 ;
},
) ;
plan tests => scalar @tests ;
$_->() for ( @tests ) ;
|