Deprecate shellwords.pl with a warning
[p5sagit/p5-mst-13.2.git] / lib / autodie / t / flock.t
CommitLineData
0b09a93a 1#!/usr/bin/perl -w
2use strict;
3use Test::More;
4use Fcntl qw(:flock);
5use POSIX qw(EWOULDBLOCK);
6
7require Fatal;
8
9my $EWOULDBLOCK = eval { EWOULDBLOCK() }
10 || $Fatal::_EWOULDBLOCK{$^O}
11 || plan skip_all => "EWOULDBLOCK not defined on this system";
12
13my ($self_fh, $self_fh2);
14
15eval {
16 use autodie;
17 open($self_fh, '<', $0);
18 open($self_fh2, '<', $0);
19 open(SELF, '<', $0);
20};
21
22if ($@) {
23 plan skip_all => "Cannot lock this test on this system.";
24}
25
5472f9ed 26my $flock_return = eval { flock($self_fh, LOCK_EX | LOCK_NB); };
0b09a93a 27
28if (not $flock_return) {
29 plan skip_all => "flock on my own test not supported on this system.";
30}
31
32my $flock_return2 = flock($self_fh2, LOCK_EX | LOCK_NB);
33
34if ($flock_return2) {
35 plan skip_all => "this test requires locking a file twice with ".
36 "different filehandles to fail";
37}
38
39$flock_return = flock($self_fh, LOCK_UN);
40
41if (not $flock_return) {
42 plan skip_all => "Odd, I can't unlock a file with flock on this system.";
43}
44
45# If we're here, then we can lock and unlock our own file.
46
47plan 'no_plan';
48
49ok( flock($self_fh, LOCK_EX | LOCK_NB), "Test file locked");
50
51my $return;
52
53eval {
54 use autodie qw(flock);
55 $return = flock($self_fh2, LOCK_EX | LOCK_NB);
56};
57
58is($!+0, $EWOULDBLOCK, "Double-flocking should be EWOULDBLOCK");
59ok(!$return, "flocking a file twice should fail");
60is($@, "", "Non-blocking flock should not fail on EWOULDBLOCK");
61
62__END__
63
64# These are old tests which I'd love to resurrect, but they need
65# a reliable way of getting flock to throw exceptions but with
66# minimal blocking. They may turn into author tests.
67
68eval {
69 use autodie;
70 flock($self_fh2, LOCK_EX | LOCK_NB);
71};
72
73ok($@, "Locking a file twice throws an exception with vanilla autodie");
74isa_ok($@, "autodie::exception", "Exception is from autodie::exception");
75
76like($@, qr/LOCK_EX/, "error message contains LOCK_EX switch");
77like($@, qr/LOCK_NB/, "error message contains LOCK_NB switch");
78unlike($@, qr/GLOB/ , "error doesn't include ugly GLOB mention");
79
80eval {
81 use autodie;
82 flock(SELF, LOCK_EX | LOCK_NB);
83};
84
85ok($@, "Locking a package filehanlde twice throws exception with vanilla autodie");
86isa_ok($@, "autodie::exception", "Exception is from autodie::exception");
87
88like($@, qr/LOCK_EX/, "error message contains LOCK_EX switch");
89like($@, qr/LOCK_NB/, "error message contains LOCK_NB switch");
90like($@, qr/SELF/ , "error mentions actual filehandle name.");