1#!./perl 2 3BEGIN { 4 chdir 't'; 5 @INC = '../lib'; 6 require './test.pl'; 7} 8 9BEGIN { 10 use Config; 11 if( !$Config{d_alarm} ) { 12 skip_all("alarm() not implemented on this platform"); 13 } 14} 15 16plan tests => 5; 17my $Perl = which_perl(); 18 19my ($start_time, $end_time); 20 21eval { 22 local $SIG{ALRM} = sub { $end_time = time; die "ALARM!\n" }; 23 $start_time = time; 24 alarm 3; 25 26 # perlfunc recommends against using sleep in combination with alarm. 27 1 while (($end_time = time) - $start_time < 6); 28 alarm 0; 29}; 30alarm 0; 31my $diff = $end_time - $start_time; 32 33# alarm time might be one second less than you said. 34is( $@, "ALARM!\n", 'alarm w/$SIG{ALRM} vs inf loop' ); 35ok( abs($diff - 3) <= 1, " right time (waited $diff secs for 3-sec alarm)" ); 36 37 38eval { 39 local $SIG{ALRM} = sub { $end_time = time; die "ALARM!\n" }; 40 $start_time = time; 41 alarm 3; 42 system(qq{$Perl -e "sleep 6"}); 43 $end_time = time; 44 alarm 0; 45}; 46alarm 0; 47$diff = $end_time - $start_time; 48 49# alarm time might be one second less than you said. 50is( $@, "ALARM!\n", 'alarm w/$SIG{ALRM} vs system()' ); 51 52{ 53 local $TODO = "Why does system() block alarm() on $^O?" 54 if $^O eq 'VMS' || $^O eq 'dos'; 55 ok( abs($diff - 3) <= 1, " right time (waited $diff secs for 3-sec alarm)" ); 56} 57 58 59{ 60 local $SIG{"ALRM"} = sub { die }; 61 eval { alarm(1); my $x = qx($Perl -e "sleep 3"); alarm(0); }; 62 chomp (my $foo = "foo\n"); 63 ok($foo eq "foo", '[perl #33928] chomp() fails after alarm(), `sleep`'); 64} 65