5 use File::Spec::Functions;
7 use Test::More tests => 29;
10 use File::Temp qw(tempdir);
12 my $dir = tempdir( CLEANUP => 1 );
15 use_ok('MooseX::Daemonize');
18 use constant DEBUG => 0;
20 my $FILENAME = catfile($dir, "im_alive");
21 $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
22 $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
28 with qw(MooseX::Daemonize);
30 has filename => ( isa => 'Str', is => 'ro' );
34 if ($self->is_daemon) {
35 $self->create_file( $self->filename );
40 my ( $self, $file ) = @_;
41 open( my $FILE, ">$file" ) || die $!;
46 my $app = FileMaker->new(
48 filename => $FILENAME,
50 isa_ok($app, 'FileMaker');
51 does_ok($app, 'MooseX::Daemonize');
52 does_ok($app, 'MooseX::Daemonize::WithPidFile');
53 does_ok($app, 'MooseX::Daemonize::Core');
55 isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File');
57 is($app->pidfile->file, catfile($dir, "filemaker.pid"), '... got the right PID file path');
58 ok(not(-e $app->pidfile->file), '... our pidfile does not exist');
60 ok(!$app->status, '... the daemon is running');
61 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
63 ok($app->stop, '... the app will stop cause its not running');
64 is($app->status_message, "Not running", '... got the correct status message');
65 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
69 ok($app->start, '... daemon started');
70 is($app->status_message, "Start succeeded", '... got the correct status message');
71 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
73 sleep(1); # give it a second ...
75 ok(-e $app->pidfile->file, '... our pidfile exists' );
77 my $pid = $app->pidfile->pid;
78 isnt($pid, $$, '... the pid in our pidfile is correct (and not us)');
80 ok($app->status, '... the daemon is running');
81 is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message');
82 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
86 diag "Status is: " . $app->status_message;
89 ok( -e $app->filename, "file exists" );
93 diag "Status is: " . $app->status_message;
96 ok( $app->stop, '... app stopped' );
97 is($app->status_message, "Stop succeeded", '... got the correct status message');
98 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
100 ok(!$app->status, '... the daemon is no longer running');
101 is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message');
102 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
106 diag "Status is: " . $app->status_message;
109 ok( not(-e $app->pidfile->file) , '... pidfile gone' );
112 unlink $ENV{MX_DAEMON_STDOUT};
113 unlink $ENV{MX_DAEMON_STDERR};