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