This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/filehandle.t: Provide descriptions for all tests.
[perl5.git] / t / op / alarm.t
1 #!./perl 
2
3 BEGIN {
4     chdir 't';
5     @INC = '../lib';
6     require './test.pl';
7 }
8
9 BEGIN {
10     use Config;
11     if( !$Config{d_alarm} ) {
12         skip_all("alarm() not implemented on this platform");
13     }
14 }
15
16 plan tests => 5;
17 my $Perl = which_perl();
18
19 my ($start_time, $end_time);
20
21 eval {
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 };
30 alarm 0;
31 my $diff = $end_time - $start_time;
32
33 # alarm time might be one second less than you said.
34 is( $@, "ALARM!\n",             'alarm w/$SIG{ALRM} vs inf loop' );
35 ok( abs($diff - 3) <= 1,   "   right time (waited $diff secs for 3-sec alarm)" );
36
37
38 eval {
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 };
46 alarm 0;
47 $diff = $end_time - $start_time;
48
49 # alarm time might be one second less than you said.
50 is( $@, "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 }