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
|
#!/usr/bin/perl -w
use strict;
use IO::Async::Test;
use Test::More tests => 36;
use Test::Exception;
use POSIX qw( WIFEXITED WEXITSTATUS );
use IO::Async::Loop::Poll;
my $loop = IO::Async::Loop::Poll->new();
testing_loop( $loop );
my ( $exitcode, $child_out, $child_err );
$loop->run_child(
code => sub { 0 },
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after sub { 0 }' );
is( WEXITSTATUS($exitcode), 0, 'WEXITSTATUS($exitcode) after sub { 0 }' );
is( $child_out, "", '$child_out after sub { 0 }' );
is( $child_err, "", '$child_err after sub { 0 }' );
$loop->run_child(
code => sub { 3 },
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after sub { 3 }' );
is( WEXITSTATUS($exitcode), 3, 'WEXITSTATUS($exitcode) after sub { 3 }' );
is( $child_out, "", '$child_out after sub { 3 }' );
is( $child_err, "", '$child_err after sub { 3 }' );
$loop->run_child(
command => [ $^X, "-e", '1' ],
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after perl -e 1' );
is( WEXITSTATUS($exitcode), 0, 'WEXITSTATUS($exitcode) after perl -e 1' );
is( $child_out, "", '$child_out after perl -e 1' );
is( $child_err, "", '$child_err after perl -e 1' );
$loop->run_child(
command => [ $^X, "-e", 'exit 5' ],
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after perl -e exit 5' );
is( WEXITSTATUS($exitcode), 5, 'WEXITSTATUS($exitcode) after perl -e exit 5' );
is( $child_out, "", '$child_out after perl -e exit 5' );
is( $child_err, "", '$child_err after perl -e exit 5' );
$loop->run_child(
code => sub { print "hello\n"; 0 },
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after sub { print }' );
is( WEXITSTATUS($exitcode), 0, 'WEXITSTATUS($exitcode) after sub { print }' );
is( $child_out, "hello\n", '$child_out after sub { print }' );
is( $child_err, "", '$child_err after sub { print }' );
$loop->run_child(
command => [ $^X, "-e", 'print "goodbye\n"' ],
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after perl STDOUT' );
is( WEXITSTATUS($exitcode), 0, 'WEXITSTATUS($exitcode) after perl STDOUT' );
is( $child_out, "goodbye\n", '$child_out after perl STDOUT' );
is( $child_err, "", '$child_err after perl STDOUT' );
$loop->run_child(
command => [ $^X, "-e", 'print STDOUT "output\n"; print STDERR "error\n";' ],
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after perl STDOUT/STDERR' );
is( WEXITSTATUS($exitcode), 0, 'WEXITSTATUS($exitcode) after perl STDOUT/STDERR' );
is( $child_out, "output\n", '$child_out after perl STDOUT/STDERR' );
is( $child_err, "error\n", '$child_err after perl STDOUT/STDERR' );
# perl -pe 1 behaves like cat; copies STDIN to STDOUT
$loop->run_child(
command => [ $^X, "-pe", '1' ],
stdin => "some data\n",
on_finish => sub { ( undef, $exitcode, $child_out, $child_err ) = @_; },
);
undef $exitcode;
wait_for { defined $exitcode };
ok( WIFEXITED($exitcode), 'WIFEXITED($exitcode) after perl STDIN->STDOUT' );
is( WEXITSTATUS($exitcode), 0, 'WEXITSTATUS($exitcode) after perl STDIN->STDOUT' );
is( $child_out, "some data\n", '$child_out after perl STDIN->STDOUT' );
is( $child_err, "", '$child_err after perl STDIN->STDOUT' );
dies_ok( sub { $loop->run_child(
command => [ $^X, "-e", 1 ]
) },
'Missing on_finish fails' );
dies_ok( sub { $loop->run_child(
command => [ $^X, "-e", 1 ],
on_finish => "hello"
) },
'on_finish not CODE ref fails' );
dies_ok( sub { $loop->run_child(
command => [ $^X, "-e", 1 ],
on_finish => sub {},
on_exit => sub {},
) },
'on_exit parameter fails' );
dies_ok( sub { $loop->run_child(
command => [ $^X, "-e", 1 ],
on_finish => sub {},
some_key_you_fail => 1
) },
'unrecognised key fails' );
|