-use Test::More no_plan => 1;
-use Proc::Daemon;
-use Cwd;
+use Test::More tests => 4;
+use Test::MooseX::Daemonize;
+use MooseX::Daemonize;
## Since a daemon will not be able to print terminal output, we
## have a test daemon create a file, and another process test for
use Moose;
with qw(MooseX::Daemonize);
+ has filename => ( isa => 'Str', is => 'ro' );
+
+ after start => sub { $_[0]->create_file( $_[0]->filename ) };
+
sub create_file {
my ( $self, $file ) = @_;
- open( FILE, ">$file" ) || die $!;
- close(FILE);
+ open( my $FILE, ">$file" ) || die $!;
+ close($FILE);
}
no Moose;
}
package main;
+use strict;
+use warnings;
+use Cwd;
## Try to make sure we are in the test directory
+chdir 't' if ( Cwd::cwd() !~ m|/t$| );
my $cwd = Cwd::cwd();
-chdir 't' if ( $cwd !~ m|/t$| );
-$cwd = Cwd::cwd();
-
-## Test filename
-my $file = join( '/', $cwd, 'im_alive' );
-## Parent process will check if file created. Child becomes the daemon.
-if ( my $pid = Proc::Daemon::Fork ) {
- sleep(5); # Punt on sleep time, 5 seconds should be enough
- ok( -e $file, "$file exists");
- unlink($file);
-}
-else {
- my $daemon = FileMaker->new(pidbase => '.');
- $daemon->start();
- $daemon->create_file($file);
- $daemon->stop();
- exit;
-}
+
+my $app = FileMaker->new(
+ pidbase => $cwd,
+ filename => "$cwd/im_alive",
+);
+daemonize_ok( $app, 'child forked okay' );
+ok( -e $app->filename, "file exists" );
+ok( $app->stop( no_exit => 1 ), 'app stopped' );
+ok( not(-e $app->pidfile) , 'pidfile gone' );
+unlink( $app->filename );