Upgrade to File-Temp-0.19
[p5sagit/p5-mst-13.2.git] / lib / File / Temp / t / lock.t
1 #!perl -w
2 # Test O_EXLOCK
3
4 use Test::More;
5 use strict;
6 use Fcntl;
7
8 BEGIN {use_ok( "File::Temp" ); }
9
10 # see if we have O_EXLOCK
11 eval { &Fcntl::O_EXLOCK; };
12 if ($@) {
13   plan skip_all => 'Do not seem to have O_EXLOCK';
14 } else {
15   plan tests => 3;
16 }
17
18 # Get a tempfile with O_EXLOCK
19 my $fh = new File::Temp();
20 ok( -e "$fh", "temp file is present" );
21
22 # try to open it with a lock
23 my $flags = O_CREAT | O_RDWR | O_EXLOCK;
24
25 my $timeout = 5;
26 my $status;
27 eval {
28    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
29    alarm $timeout;
30    $status = sysopen(my $newfh, "$fh", $flags, 0600);
31    alarm 0;
32 };
33 if ($@) {
34    die unless $@ eq "alarm\n";   # propagate unexpected errors
35    # timed out
36 }
37 ok( !$status, "File $fh is locked" );
38
39 # Now get a tempfile with locking disabled
40 $fh = new File::Temp( EXLOCK => 0 );
41
42 eval {
43    local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
44    alarm $timeout;
45    $status = sysopen(my $newfh, "$fh", $flags, 0600);
46    alarm 0;
47 };
48 if ($@) {
49    die unless $@ eq "alarm\n";   # propagate unexpected errors
50    # timed out
51 }
52 ok( $status, "File $fh is not locked");