1# Blocking Exclusive test within a single process (no fork) 2 3use Test; 4use File::NFSLock; 5use Fcntl qw(O_CREAT O_RDWR O_RDONLY O_TRUNC LOCK_EX); 6 7plan tests => 3; 8 9# Everything loaded fine 10ok (1); 11 12my $datafile = "testfile.dat"; 13 14# Create a blank file 15sysopen ( FH, $datafile, O_CREAT | O_RDWR | O_TRUNC ); 16close (FH); 17ok (-e $datafile && !-s _); 18# Wipe any old stale locks 19unlink "$datafile$File::NFSLock::LOCK_EXTENSION"; 20 21# Single process trying to count to $n 22my $n = 20; 23 24for (my $i = 0; $i < $n ; $i++) { 25 my $lock = new File::NFSLock { 26 file => $datafile, 27 lock_type => LOCK_EX, 28 }; 29 sysopen(FH, $datafile, O_RDWR); 30 31 # Read the current value 32 my $count = <FH>; 33 # Increment it 34 $count ++; 35 36 # And put it back 37 seek (FH,0,0); 38 print FH "$count\n"; 39 close FH; 40} 41 42# Load up whatever the file says now 43sysopen(FH, $datafile, O_RDONLY); 44$_ = <FH>; 45close FH; 46chomp; 47# It should be the same as the number of times it looped 48ok $n, $_; 49 50# Wipe the temporary file 51unlink $datafile; 52