Daemon-Generic
view release on metacpan or search on metacpan
sub run {
set_alarm();
my $r = `$base @_`;
alarm(0);
return $r;
}
sub setup_test
{
my ($head_frag, $body_frag) = @_;
unlink "$tmp/$_" for qw(pid counter counter1 log);
write_file("$tmp/daemon.pl", "use lib '$FindBin::Bin/../lib';\n", $daemon_header, $head_frag, $daemon_body, $body_frag || '');
config_deamon();
}
sub config_deamon {
write_file("$tmp/config", <<END_CONFIG);
pidfile=$tmp/pid
COUNTER=$tmp/counter
RELOAD_COUNTER=$tmp/reload
foreground=0
PATH=$tmp:$ENV{PATH}
END_CONFIG
}
sub do_test
{
my ($name) = @_;
like(run('start'), qr/Starting/, "start message - $name");
expect { -s "$tmp/pid" };
my $pid = read_file("$tmp/pid");
chomp($pid);
like($pid, qr/^\d+$/, "pid");
expect { -s "$tmp/counter" };
my $counter1 = read_file("$tmp/pid");
chomp($counter1);
like($counter1, qr/^\d+$/, "counter1 - $name");
expect { -s "$tmp/log" };
like(read_file("$tmp/log"), qr/START LOG/, "logged output");
expect { my @l = read_file("$tmp/log"); @l > 1 };
like(read_file("$tmp/log"), qr/Sucessfully daemonized/, "daemonized");
append_file("$tmp/config", "COUNTER=$tmp/counter2");
like(run('reload'), qr/reconfiguration/, "reconfig message");
expect { -s "$tmp/counter2" };
my $counter2 = read_file("$tmp/pid");
chomp($counter2);
like($counter2, qr/^\d+$/, "counter2");
ok(kill(0,$pid), "process $pid is alive - $name");
my $check = run('check');
like($check, qr/Configuration looks okay/, "config ok - $name");
like($check, qr/running - pid \d+/, "running");
like(run('stop'), qr/Killing/, "kill message");
ok(!kill(0,$pid), "process is dead - $name");
my $check2 = run('check');
like($check2, qr/Configuration looks okay/, "config ok");
like($check2, qr/No \S+ running/, "not running");
like(run('restart'), qr/Starting/, "restart message - start - $name");
like(run('restart'), qr/Killing/, "restart message - kill");
like(run('restart'), qr/Starting/, "restart message - start - $name");
like(run('check'), qr/running - pid/, "check");
like(run('stop'), qr/Killing/, "stop message");
unlike(run('check'), qr/running - pid/, "check - $name");
}
( run in 2.172 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )