forked from sympa-community/sympa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockedFile.t
83 lines (62 loc) · 2.23 KB
/
LockedFile.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$
use strict;
use warnings;
use English qw(-no_match_vars);
use File::Temp;
use Test::More;
use Sympa::LockedFile;
plan tests => 23;
my $lock;
my $temp_dir = File::Temp->newdir(CLEANUP => $ENV{TEST_DEBUG} ? 0 : 1);
my $main_file = $temp_dir . '/file';
my $lock_file = $main_file . ',lock';
eval {
$lock = Sympa::LockedFile->new();
$lock->open();
};
like($@, qr/^Usage: /, 'Usage: ');
eval { $lock = Sympa::LockedFile->new($main_file, 0, 'something'); };
like($@, qr/^IO::Handle: bad open mode/, 'IO::Handle: bad open mode');
ok(!-f $lock_file, "underlying lock file doesn't exist");
open my $fh, '>', $main_file;
close $fh;
eval { $lock = Sympa::LockedFile->new($main_file); };
ok(!$@, 'all parameters OK');
isa_ok($lock, 'Sympa::LockedFile');
can_ok($lock, 'open');
can_ok($lock, 'close');
ok(-f $lock_file, "underlying lock file does exist");
ok($lock->open($main_file), 'locking locked file, unspecified mode');
is($lock->last_error, undef);
##ok($lock->open($main_file, 0, 'Anything'), 'locking, irrelevant mode');
ok($lock->open($main_file, 0, '<'), 'locking locked file, read mode');
is($lock->last_error, undef);
ok(!$lock->open($main_file, 2, '>'), 'prevented locking, write mode');
isnt($lock->last_error, undef);
ok(!$lock->open($main_file, -1, '>'),
'prevented non-blocking locking, write mode');
isnt($lock->last_error, undef);
ok($lock->close, 'unlocking');
ok($lock->open($main_file, 0, '>'), 'locking unlocked file, write mode');
ok(attempt_parallel_lock($temp_dir . '/foo', '>'),
'write lock on another file');
ok(!attempt_parallel_lock($main_file, '<'), 'read lock on same file');
ok(!attempt_parallel_lock($main_file, '>'), 'write lock on same file');
$lock->close;
$lock->open($main_file);
my $another_lock = Sympa::LockedFile->new($main_file);
ok($another_lock->close(), 'unlocking, new lock');
$lock->close;
ok(!-f $lock_file, "all locks released, underlying lock file doesn't exist");
sub attempt_parallel_lock {
my ($file, $mode) = @_;
my $code = <<EOF;
my \$lock = Sympa::LockedFile->new("$file", -1, "$mode");
exit !!\$lock;
EOF
my @command = ($EXECUTABLE_NAME, "-MSympa::LockedFile", "-e", $code);
system(@command);
return $CHILD_ERROR >> 8;
}