| 1 |
use Test::More qw[no_plan]; |
| 2 |
use strict; |
| 3 |
$^W = 1; |
| 4 |
|
| 5 |
use_ok 'File::Pid'; |
| 6 |
use File::Spec::Functions qw[tmpdir catfile]; |
| 7 |
|
| 8 |
is File::Pid->program_name, 'test.t', 'program_name correct'; |
| 9 |
|
| 10 |
my $pid = File::Pid->new; |
| 11 |
|
| 12 |
is $pid->pid, $$, 'current process by default'; |
| 13 |
is $pid->file, catfile(tmpdir,'test.t.pid'), 'pid file correct'; |
| 14 |
|
| 15 |
is $pid->write, $$, 'writing file'; |
| 16 |
is $pid->running, $$, 'we are running'; |
| 17 |
ok $pid->remove, 'deleted file'; |
| 18 |
|
| 19 |
my $child; |
| 20 |
my $file = catfile(tmpdir, 'test.t.child.pid'); |
| 21 |
unlink $file; |
| 22 |
|
| 23 |
if ( $child = fork ) { #parent |
| 24 |
waitpid $child, 0; |
| 25 |
my $cpid = File::Pid->new({file => $file}); |
| 26 |
is $cpid->pid, $child, "$$:$child: child pid correct"; |
| 27 |
ok !$cpid->running, 'child is not running'; |
| 28 |
ok $cpid->remove, 'removed child pid file'; |
| 29 |
} else { # child |
| 30 |
File::Pid->new({ |
| 31 |
file => $file, |
| 32 |
pid => $$, |
| 33 |
})->write; # hope for the best |
| 34 |
} |