1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7}
8
9
10use Config;
11if ( !$Config{d_alarm} ) {
12    skip_all("alarm() not implemented on this platform");
13}
14
15plan tests => 5;
16my $Perl = which_perl();
17
18my ($start_time, $end_time);
19
20eval {
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};
29alarm 0;
30my $diff = $end_time - $start_time;
31
32# alarm time might be one second less than you said.
33is( $@, "ALARM!\n",             'alarm w/$SIG{ALRM} vs inf loop' );
34ok( abs($diff - 3) <= 1,   "   right time (waited $diff secs for 3-sec alarm)" );
35
36
37eval {
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};
45alarm 0;
46$diff = $end_time - $start_time;
47
48# alarm time might be one second less than you said.
49is( $@, "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}
64