#!/usr/bin/perl -w
my
$devnull
= File::Spec->devnull;
$|++; $\ =
"\n"
;
my
%args
= (
name
=>
"test"
,
dir
=>
"."
,
debug
=>
$ENV
{DEBUG} ||
""
);
my
$cmd
=
shift
||
""
;
if
(
$cmd
eq
"--daemon"
) {
print
"-- daemon --"
;
$args
{verify} = 1;
die
"Already running!"
if
Proc::PID::File->running(
%args
);
sleep
(5);
print
"-- deamon: exiting --"
;
exit
();
}
exit
()
if
$cmd
eq
"--short"
;
my
$pid
;
our
$ProcessObj
;
sub
rundaemon {
my
$pipes
=
$args
{debug} =~ /D/ ?
""
:
"> $devnull 2>&1"
;
if
($^O eq
'MSWin32'
) {
$ProcessObj
->Kill(0)
if
$ProcessObj
;
Win32::Process::Create(
$ProcessObj
,
"$^X"
,
"$^X $0 --daemon $pipes"
,
0,
32 + 134217728,
"."
) ||
die
$^E;
}
else
{
system
qq|$^X $0 --daemon $pipes &|
;
}
sleep
1;
open
my
$fh
,
'<'
,
"$args{dir}/$args{name}.pid"
or
die
"Error reading $args{dir}/$args{name}.pid - $!"
;
$pid
=<
$fh
>;
chomp
(
$pid
);
}
ok(1,
"SKIPPED - simple: thread safe"
)
unless
$] >= 5.008001
&&
$Config
{
"useithreads"
}
&&
eval
{
Proc::PID::File->running(
%args
);
threads->create(
sub
{})->
join
();
sleep
(2);
ok(-f
"test.pid"
,
"simple: thread safe"
);
};
unlink
(
"test.pid"
) ||
die
$!
if
-e
"test.pid"
;
rundaemon();
my
$rc
= Proc::PID::File->running(
%args
);
ok(
$rc
,
"simple: running"
);
ok(1,
"SKIPPED - simple: verified (real)"
)
unless
$^O =~ /linux|freebsd|cygwin/i
&&
eval
{
$rc
= Proc::PID::File->running(
%args
,
verify
=> 1);
ok(
$rc
,
"simple: verified (real)"
);
};
ok(1,
"SKIPPED - simple: verified (false)"
)
unless
$^O =~ /linux|freebsd|cygwin/i
&&
eval
{
$rc
= Proc::PID::File->running(
%args
,
verify
=>
"falsetest"
);
ok(!
$rc
,
"simple: verified (false)"
);
};
if
(
$ProcessObj
) {
$ProcessObj
->Wait(6000) or
$ProcessObj
->Kill(0);
$ProcessObj
=
undef
;
}
else
{
sleep
1
while
kill
0,
$pid
;
}
$rc
= Proc::PID::File->running(
%args
);
ok(!
$rc
,
"simple: single instance"
);
system
qq|$^X $0 --short > $devnull 2>&1|
;
ok(-f
"test.pid"
,
"simple: destroy"
);
my
$c1
= Proc::PID::File->new(
%args
);
ok(
$c1
->{path},
"oo: object initialised"
);
$c1
->touch();
ok(-f
$c1
->{path},
"oo: file touched"
);
ok(!
$c1
->alive(),
"oo: alive (with current process)"
);
unlink
(
"test.pid"
) ||
die
$!
if
-e
"test.pid"
;
rundaemon();
ok(
$c1
->alive(),
"oo: alive (with daemon)"
);
ok(1,
"SKIPPED - oo: alive (verified)"
)
unless
$^O =~ /linux|freebsd|cygwin/i
&&
eval
{
ok(
$c1
->alive(
verify
=> 1),
"oo: alive (verified)"
);
};
$c1
->release();
ok(! -f
$c1
->{path},
"oo: released"
);
$\ =
undef
;
print
"#waiting for daemon death\n"
;
if
(
$ProcessObj
) {
$ProcessObj
->Wait(1000) or
$ProcessObj
->Kill(0);
}
else
{
sleep
1,
print
"."
while
kill
0,
$pid
;
}