xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/alarm.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate    require './test.pl';
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gateBEGIN {
10*0Sstevel@tonic-gate    use Config;
11*0Sstevel@tonic-gate    if( !$Config{d_alarm} ) {
12*0Sstevel@tonic-gate        skip_all("alarm() not implemented on this platform");
13*0Sstevel@tonic-gate    }
14*0Sstevel@tonic-gate}
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gateplan tests => 4;
17*0Sstevel@tonic-gatemy $Perl = which_perl();
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gatemy $start_time = time;
20*0Sstevel@tonic-gateeval {
21*0Sstevel@tonic-gate    local $SIG{ALRM} = sub { die "ALARM!\n" };
22*0Sstevel@tonic-gate    alarm 3;
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate    # perlfunc recommends against using sleep in combination with alarm.
25*0Sstevel@tonic-gate    1 while (time - $start_time < 6);
26*0Sstevel@tonic-gate};
27*0Sstevel@tonic-gatealarm 0;
28*0Sstevel@tonic-gatemy $diff = time - $start_time;
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate# alarm time might be one second less than you said.
31*0Sstevel@tonic-gateis( $@, "ALARM!\n",             'alarm w/$SIG{ALRM} vs inf loop' );
32*0Sstevel@tonic-gateok( abs($diff - 3) <= 1,   "   right time" );
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gatemy $start_time = time;
36*0Sstevel@tonic-gateeval {
37*0Sstevel@tonic-gate    local $SIG{ALRM} = sub { die "ALARM!\n" };
38*0Sstevel@tonic-gate    alarm 3;
39*0Sstevel@tonic-gate    system(qq{$Perl -e "sleep 6"});
40*0Sstevel@tonic-gate};
41*0Sstevel@tonic-gatealarm 0;
42*0Sstevel@tonic-gate$diff = time - $start_time;
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate# alarm time might be one second less than you said.
45*0Sstevel@tonic-gateis( $@, "ALARM!\n",             'alarm w/$SIG{ALRM} vs system()' );
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate{
48*0Sstevel@tonic-gate    local $TODO = "Why does system() block alarm() on $^O?"
49*0Sstevel@tonic-gate		if $^O eq 'VMS' || $^O eq'MacOS' || $^O eq 'dos';
50*0Sstevel@tonic-gate    ok( abs($diff - 3) <= 1,   "   right time (waited $diff secs for 3-sec alarm)" );
51*0Sstevel@tonic-gate}
52