AnyEvent-FileLock
view release on metacpan or search on metacpan
lib/AnyEvent/FileLock.pm view on Meta::CPAN
}
if ($ok) {
$self->{user_cb}->($self->{fh});
}
elsif ($! == Errno::EAGAIN() and
(!defined($self->{max_time}) or $self->{max_time} >= $now)) {
# we add some randomness into the delay to avoid the case
# where all the contenders follow exactly the same pattern so
# that they end looking for the pattern all at once every time
# (and obviosly all but one failing).
$self->{timer} = &AE::timer($self->{delay} * (0.8 + rand 0.40), 0, $self->{acquire_lock_cb});
return;
}
else {
$self->{user_cb}->();
}
# release all the references, the object is useless from this
# point on time.
%$self = ();
}
t/AnyEvent-FileLock.t view on Meta::CPAN
my $got_lock;
my $w = AnyEvent::FileLock->flock( file => $filename,
cb => sub {
if (defined (my $fh = shift)) {
$got_lock = 1;
close $fh;
}
$cv->send();
},
);
my $t = AE::timer(0.3, 0, sub { flock($temp_fh, Fcntl::LOCK_UN); });
$cv->recv();
ok($got_lock, 'got lock');
is_deeply($w, {}, 'object emptied');
done_testing();
};
subtest 'abort after timeout' => sub {
my $cv = AE::cv();
my ($temp_fh, $filename) = tempfile();
( run in 0.541 second using v1.01-cache-2.11-cpan-49f99fa48dc )