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
|
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 1.t'
#########################
# change 'tests => 1' to 'tests => last_test_to_print';
use Test::More;
BEGIN {
use_ok('Sys::SigAction');
if ( Sys::SigAction::have_hires() )
{
eval "use Time::HiRes qw( clock_gettime CLOCK_REALTIME ); ";
} else {
eval "use constant CLOCK_REALTIME => 1;"; #get it defined
}
}
#########################
# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.
#use strict;
#use warnings;
use Carp qw( carp cluck croak confess );
use Data::Dumper;
use Sys::SigAction qw( set_sig_handler timeout_call );
use POSIX ':signal_h' ;
sub hash { die { hash=>1 }; }
sub immediate { die "immediate"; }
sub forever { while ( 1 ) { 1; } }
my $ret = 0;
my $num_tests = 1; #start at 1 because of use_ok above
eval {
$ret = timeout_call( 1, sub { hash(); } );
};
ok( (ref( $@ ) and exists($@->{'hash'})) ,'die with hash' ); $num_tests++;
ok( $ret == 0 ,'hash did not timeout' ); $num_tests++;
$ret = 0;
eval {
$ret = timeout_call( 1, sub { immediate(); } );
};
ok( (not ref($@) and $@ ),'immediate -- die with string' ); $num_tests++;
ok( $ret == 0 ,'immediate did not timeout' ); $num_tests++;
$ret = 0;
eval {
$ret = Sys::SigAction::timeout_call( 1, \&forever );
#print "forever timed out\n" if $ret;
};
if ( $@ )
{
print "why did forever throw exception:" .Dumper( $@ );
}
ok( (not $@ ) ,'forever did NOT die' ); $num_tests++;
ok( $ret ,'forever timed out' ); $num_tests++;
if ( Sys::SigAction::have_hires() )
{
$ret = 0;
my $btime;
my $etime;
eval {
$btime = clock_gettime( CLOCK_REALTIME );
$ret = Sys::SigAction::timeout_call( 0.1, \&forever );
};
if ( $@ )
{
print "hires: why did forever throw exception:" .Dumper( $@ );
}
$etime = clock_gettime( CLOCK_REALTIME );
# diag( $btime );
# diag( $etime );
# diag( ($etime-$btime) );
ok( (not $@ ) ,'hires: forever did NOT die' ); $num_tests++;
ok( $ret ,'hires: forever timed out' ); $num_tests++;
ok( (($etime - $btime) < 0.2 ), "hires: timeout in < 0.2 seconds" ); $num_tests++;
}
else
{
diag "fractional second timeout test skipped: Time::HiRes is not installed" ;
}
plan tests => $num_tests;
#foreach my $level ( @levels )
#{
# ok( $level ,"level $i" );
# print "level $i = $level\n" ;
# $i++;
#}
exit;
|