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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
#!/usr/bin/perl -w
=head1 NAME
pty.t - Test suite for IPC::Run's pty (psuedo-terminal) support
=head1 DESCRIPTION
This test suite starts off with a test that seems to cause a deadlock
on freebsd: \@cmd, '<pty<', ... '>', ..., '2>'...
This seems to cause the child process entry in the process table to
hang around after the child exits. Both output pipes are closed, but
the PID is still valid so IPC::Run::finish() thinks it's still alive and
the whole shebang deadlocks waiting for the child to exit.
This is a very rare corner condition, so I'm not patching in a fix yet.
One fix might be to hack IPC::Run to close the master pty when all outputs
from the child are closed. That's a hack, not sure what to do about it.
This problem needs to be reproduced in a standalone script and investigated
further, but I have not the time.
=cut
BEGIN {
if( $ENV{PERL_CORE} ) {
chdir '../lib/IPC/Run' if -d '../lib/IPC/Run';
unshift @INC, 'lib', '../..';
$^X = '../../../t/' . $^X;
}
}
use strict ;
use Test ;
use IPC::Run::Debug qw( _map_fds );
use IPC::Run qw( start pump finish ) ;
use UNIVERSAL qw( isa ) ;
select STDERR ; $| = 1 ; select STDOUT ;
sub pty_warn {
warn "\nWARNING: $_[0].\nWARNING: '<pty<', '>pty>' $_[1] not work.\n\n";
}
if ( $^O !~ /Win32/ ) {
# my $min = 0.9 ;
for ( eval { require IO::Pty ; IO::Pty->VERSION } ) {
s/_//g if defined ;
if ( ! defined ) {
pty_warn "IO::Pty not found", "will" ;
}
elsif ( $_ == 0.02 ) {
pty_warn "IO::Pty v$_ has spurious warnings, try 0.9 or later", "may"
}
elsif ( $_ < 1.00 ) {
pty_warn "IO::Pty 1.00 is strongly recommended", "may" ;
}
}
}
my $echoer_script = <<TOHERE ;
\$| = 1 ;
\$s = select STDERR ; \$| = 1 ; select \$s ;
while (<>) {
print STDERR uc \$_ ;
print ;
last if /quit/ ;
}
TOHERE
##
## $^X is the path to the perl binary. This is used run all the subprocesses.
##
my @echoer = ( $^X, '-e', $echoer_script ) ;
my $in ;
my $out ;
my $err;
my $h ;
my $r ;
my $fd_map ;
my $text = "hello world\n" ;
## TODO: test lots of mixtures of pty's and pipes & files. Use run().
## Older Perls can't ok( a, qr// ), so I manually do that here.
my $exp ;
my $platform_skip = $^O =~ /(?:aix|freebsd|openbsd)/ ? "$^O deadlocks on this test" : "" ;
my @tests = (
##
## stdin only
##
sub {
return skip $platform_skip, 1 if $platform_skip;
$out = 'REPLACE ME' ;
$? = 99 ;
$fd_map = _map_fds ;
$h = start \@echoer, '<pty<', \$in, '>', \$out, '2>', \$err ;
$in = "hello\n" ;
$? = 0 ;
pump $h until $out =~ /hello/ && $err =~ /HELLO/ ;
ok( $out, "hello\n" ) ;
},
sub {
return skip $platform_skip, 1 if $platform_skip;
$exp = qr/^HELLO\n(?!\n)$/ ;
$err =~ $exp ? ok( 1 ) : ok( $err, $exp ) ;
},
sub {
return skip $platform_skip, 1 if $platform_skip;
ok( $in, '' )
},
sub {
return skip $platform_skip, 1 if $platform_skip;
$in = "world\n" ;
$? = 0 ;
pump $h until $out =~ /world/ && $err =~ /WORLD/ ;
ok( $out, "hello\nworld\n" ) ;
},
sub {
return skip $platform_skip, 1 if $platform_skip;
$exp = qr/^HELLO\nWORLD\n(?!\n)$/ ;
$err =~ $exp ? ok( 1 ) : ok( $err, $exp ) ;
},
sub {
return skip $platform_skip, 1 if $platform_skip;
ok( $in, '' )
},
sub {
return skip $platform_skip, 1 if $platform_skip;
$in = "quit\n" ;
ok( $h->finish ) ;
},
sub {
return skip $platform_skip, 1 if $platform_skip;
ok( ! $? )
},
sub {
return skip $platform_skip, 1 if $platform_skip;
ok( _map_fds, $fd_map )
},
##
## stdout, stderr
##
sub {
$out = 'REPLACE ME' ;
$? = 99 ;
$fd_map = _map_fds ;
$h = start \@echoer, \$in, '>pty>', \$out ;
$in = "hello\n" ;
$? = 0 ;
pump $h until $out =~ /hello/ ;
## We assume that the slave's write()s are atomic
$exp = qr/^(?:hello\r?\n){2}(?!\n)$/i ;
$out =~ $exp ? ok( 1 ) : ok( $out, $exp ) ;
},
sub { ok( $in, '' ) },
sub {
$in = "world\n" ;
$? = 0 ;
pump $h until $out =~ /world/ ;
$exp = qr/^(?:hello\r?\n){2}(?:world\r?\n){2}(?!\n)$/i ;
$out =~ $exp ? ok( 1 ) : ok( $out, $exp ) ;
},
sub { ok( $in, '' ) },
sub {
$in = "quit\n" ;
ok( $h->finish ) ;
},
sub { ok( ! $? ) },
sub { ok( _map_fds, $fd_map ) },
##
## stdout only
##
sub {
$out = 'REPLACE ME' ;
$? = 99 ;
$fd_map = _map_fds ;
$h = start \@echoer, \$in, '>pty>', \$out, '2>', \$err ;
$in = "hello\n" ;
$? = 0 ;
pump $h until $out =~ /hello/ && $err =~ /HELLO/ ;
$exp = qr/^hello\r?\n(?!\n)$/ ;
$out =~ $exp ? ok( 1 ) : ok( $out, $exp ) ;
},
sub {
$exp = qr/^HELLO\n(?!\n)$/ ;
$err =~ $exp ? ok( 1 ) : ok( $err, $exp ) ;
},
sub { ok( $in, '' ) },
sub {
$in = "world\n" ;
$? = 0 ;
pump $h until $out =~ /world/ && $err =~ /WORLD/ ;
$exp = qr/^hello\r?\nworld\r?\n(?!\n)$/ ;
$out =~ $exp ? ok( 1 ) : ok( $out, $exp ) ;
},
sub {
$exp = qr/^HELLO\nWORLD\n(?!\n)$/ ,
$err =~ $exp ? ok( 1 ) : ok( $err, $exp ) ;
},
sub { ok( $in, '' ) },
sub {
$in = "quit\n" ;
ok( $h->finish ) ;
},
sub { ok( ! $? ) },
sub { ok( _map_fds, $fd_map ) },
##
## stdin, stdout, stderr
##
sub {
$out = 'REPLACE ME' ;
$? = 99 ;
$fd_map = _map_fds ;
$h = start \@echoer, '<pty<', \$in, '>pty>', \$out ;
$in = "hello\n" ;
$? = 0 ;
pump $h until $out =~ /hello.*hello.*hello/is ;
## We assume that the slave's write()s are atomic
$exp = qr/^(?:hello\r?\n){3}(?!\n)$/i ;
$out =~ $exp ? ok( 1 ) : ok( $out, $exp ) ;
},
sub { ok( $in, '' ) },
sub {
$in = "world\n" ;
$? = 0 ;
pump $h until $out =~ /world.*world.*world/is ;
$exp = qr/^(?:hello\r?\n){3}(?:world\r?\n){3}(?!\n)$/i ;
$out =~ $exp ? ok( 1 ) : ok( $out, $exp ) ;
},
sub { ok( $in, '' ) },
sub {
$in = "quit\n" ;
ok( $h->finish ) ;
},
sub { ok( ! $? ) },
sub { ok( _map_fds, $fd_map ) },
) ;
plan tests => scalar @tests ;
unless ( eval { require IO::Pty ; } ) {
skip( "skip: IO::Pty not found", 0 ) for @tests ;
exit ;
}
print "# Using IO::Tty $IO::Tty::VERSION\n";
print "# Using IO::Pty $IO::Pty::VERSION\n";
$_->() for ( @tests ) ;
|