The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
# -*- mode: cperl -*-
use strict;
use warnings;
use Test::More;

use IPC::Lock::WithTTL;

my $myself = $$;
my $lockfile = '/tmp/11_by-someone';

my $lock = IPC::Lock::WithTTL->new( file => $lockfile, ttl => 3 );
my($r, $locker);

my $child;
if ($child = fork) {
    wait;
} else {
    $lock->acquire;
    # not release lock
    exit;
}

# previous time another process locked but didn't release lock
# not expired
($r, $locker) = $lock->acquire;
ok !$r, 'lock NG (not expired)';
is $locker->{pid}, $child, 'pid another(child)';
ok $locker->{expiration} > time(), 'expiration';

sleep 4;
# expired
($r, $locker) = $lock->acquire;
ok $r, 'lock OK (expired)';
is $locker->{pid}, $myself, 'pid myself';
ok $locker->{expiration} > time(), 'expiration';

done_testing;

unlink $lockfile;