Dev-Util

 view release on metacpan or  search on metacpan

t/07-sem.t  view on Meta::CPAN

#======================================#
#          new - w/ temp dir           #
#======================================#

is( file_exists( $existing_dir_name . $lock_filename ),
    0, "Lockfile should not exist yet" );

my $sem = Dev::Util::Sem->new( $existing_dir_name . $lock_filename, 30 );

is( file_exists( $existing_dir_name . $lock_filename ),
    1, "Lockfile should exist now" );

$sem->unlock;

is( file_exists( $existing_dir_name . $lock_filename ),
    0, "Lockfile should be gone now via unlock" );

#======================================#
#         new - w/ default dir         #
#======================================#

if ( file_exists( $expected_dir . $lock_filename ) ) {
    unlink( $expected_dir . $lock_filename )
        or Carp::carp("Could not unlink file\n");
}

is( file_exists( $expected_dir . $lock_filename ),
    0, "Lockfile in default dir should not exist yet" );

my $sem2 = Dev::Util::Sem->new($lock_filename);

is( file_exists( $expected_dir . $lock_filename ),
    1, "Lockfile in default dir should exist now" );

$sem2->unlock;

is( file_exists( $expected_dir . $lock_filename ),
    0, "Lockfile in default dir should be gone now via unlock" );

#======================================#
#        second sem should fail        #
#======================================#
my $semA = Dev::Util::Sem->new( $lock_filename, 3 );
is( file_exists( $expected_dir . $lock_filename ),
    1, "Lockfile in default dir should exist now" );
is( file_is_empty( $expected_dir . $lock_filename ),
    1, "Lockfile should be empty" );

$semA->{ fh }->autoflush();
$semA->{ fh }->print("Now is the time for all good men...");
is( file_size_equals( $expected_dir . $lock_filename, 35 ),
    1, "Lockfile should be 35 characters" );

like(
       dies {
           my $semB = Dev::Util::Sem->new( $lock_filename, 1 );
           $semB->{ fh }->autoflush();
           $semB->{ fh }->print("to come to the aid of their country!");
       },
       qr{Timeout aquiring the lock},
       "Fails when lock file is locked"
    );

$semA->unlock;

is( file_exists( $expected_dir . $lock_filename ),
    0, "Lockfile in default dir should be gone now via unlock" );

done_testing;



( run in 2.902 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )