1# Exclusive Double Lock Test 2# 3# This tests to make sure the same process can aquire 4# an exclusive lock multiple times for the same file. 5 6use strict; 7use Test; 8use File::NFSLock; 9use Fcntl qw(O_CREAT O_RDWR O_RDONLY O_TRUNC O_APPEND LOCK_EX LOCK_SH LOCK_NB); 10 11$| = 1; 12plan tests => 5; 13 14my $datafile = "testfile.dat"; 15 16# Wipe lock file in case it exists 17unlink ("$datafile$File::NFSLock::LOCK_EXTENSION"); 18 19# Create a blank file 20sysopen ( FH, $datafile, O_CREAT | O_RDWR | O_TRUNC ); 21close (FH); 22ok (-e $datafile && !-s _); 23 24 25my $lock1 = new File::NFSLock { 26 file => $datafile, 27 lock_type => LOCK_EX, 28 blocking_timeout => 10, 29}; 30 31ok ($lock1); 32 33sysopen(FH, $datafile, O_RDWR | O_APPEND); 34print FH "lock1\n"; 35close FH; 36 37my $lock2 = new File::NFSLock { 38 file => $datafile, 39 lock_type => LOCK_EX, 40 blocking_timeout => 10, 41}; 42 43ok ($lock2); 44 45sysopen(FH, $datafile, O_RDWR | O_APPEND); 46print FH "lock2\n"; 47close FH; 48 49# Load up whatever the file says now 50sysopen(FH, $datafile, O_RDONLY); 51$_ = <FH>; 52ok /lock1/; 53$_ = <FH>; 54ok /lock2/; 55close FH; 56 57# Wipe the temporary file 58unlink $datafile; 59