]>
git.proxmox.com Git - pve-common.git/blob - test/lock_file.pl
8 use POSIX
(); # don't import assert()
10 use PVE
::Tools
'lock_file_full';
12 my $name = "test.lockfile.$$-";
21 my ($code1, $code2) = @_;
23 pipe(my $except_r, my $except_w) or die "pipe: $!\n";
26 die "fork failed: $!\n" if !defined($pid);
45 my $err = do { local $/ = undef; <$except_r> };
47 die "interrupted\n" if waitpid($pid, 0) != $pid;
51 my $status = POSIX
::WEXITSTATUS
($?);
53 die "failed to execute\n";
54 } elsif (POSIX
::WIFSIGNALED
($?)) {
55 my $sig = POSIX
::WTERMSIG
($?);
56 die "got signal $sig\n";
57 } elsif ($status != 0) {
58 die "exit code $status\n";
74 die "code didn't run: $what\n" if !$_ran{$what};
78 die "code shouldn't have run: $what\n" if $_ran{$what};
83 lock_file_full
($name, 10, 0, sub { ran
('single lock') });
84 assert
('single lock');
86 # Lock multiple times in a row:
88 lock_file_full
($name, 10, 0, sub { ran
('lock A') });
90 lock_file_full
($name, 10, 0, sub { ran
('lock B') });
95 lock_file_full
($name, 10, 0, sub {
97 lock_file_full
($name, 10, 0, sub { ran
('lock B') });
107 lock_file_full
($name, 10, 0, sub {
109 # locks file "${name}2"
110 lock_file_full
($name.2, 10, 0, sub { ran
('lock B') });
118 # Does it actually lock? (shared=0)
119 # Can we get two simultaneous shared locks? (shared=1)
123 # socket pair for synchronization
124 socketpair(my $fmain, my $fother, AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
)
125 or die "socketpair(): $!\n";
130 lock_file_full
($name, 60, $shared, sub {
132 # tell parent we've acquired the lock
133 print {$fother} "1\n";
135 # wait for parent to be done trying to lock
139 die "parent failed\n" if !$line || $line ne "2\n";
140 assert
('other side');
143 # Wait for our child to lock:
146 die "child failed to acquire a lock\n" if !$line || $line ne "1\n";
147 lock_file_full
($name, 1, $shared, sub {
151 assert
('local side');
153 assert_not
('local side');
155 print {$fmain} "2\n";