3 use File::Spec::Functions;
5 use Test::More tests => 29;
8 use File::Temp qw(tempdir);
10 my $dir = tempdir( CLEANUP => 1 );
13 use_ok('MooseX::Daemonize');
16 use constant DEBUG => 0;
18 my $FILENAME = catfile($dir, "im_alive");
19 $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
20 $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
26 with qw(MooseX::Daemonize);
28 has filename => ( isa => 'Str', is => 'ro' );
32 if ($self->is_daemon) {
33 $self->create_file( $self->filename );
38 my ( $self, $file ) = @_;
39 open( my $FILE, ">$file" ) || die $!;
45 my $app = FileMaker->new(
46 pidbase => "$dir/subdir",
47 filename => $FILENAME,
49 isa_ok($app, 'FileMaker');
50 does_ok($app, 'MooseX::Daemonize');
51 does_ok($app, 'MooseX::Daemonize::WithPidFile');
52 does_ok($app, 'MooseX::Daemonize::Core');
54 isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File');
56 is($app->pidfile->file, catfile("$dir/subdir", "filemaker.pid"), '... got the right PID file path');
57 ok(not(-e $app->pidfile->file), '... our pidfile does not exist');
59 ok(!$app->status, '... the daemon is running');
60 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
62 ok($app->stop, '... the app will stop cause its not running');
63 is($app->status_message, "Not running", '... got the correct status message');
64 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
68 ok($app->start, '... daemon started');
69 is($app->status_message, "Start succeeded", '... got the correct status message');
70 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
72 sleep(1); # give it a second ...
74 ok(-e $app->pidfile->file, '... our pidfile exists' );
76 my $pid = $app->pidfile->pid;
77 isnt($pid, $$, '... the pid in our pidfile is correct (and not us)');
79 ok($app->status, '... the daemon is running');
80 is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message');
81 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
85 diag "Status is: " . $app->status_message;
88 ok( -e $app->filename, "file exists" );
92 diag "Status is: " . $app->status_message;
95 ok( $app->stop, '... app stopped' );
96 is($app->status_message, "Stop succeeded", '... got the correct status message');
97 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
99 ok(!$app->status, '... the daemon is no longer running');
100 is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message');
101 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
105 diag "Status is: " . $app->status_message;
108 ok( not(-e $app->pidfile->file) , '... pidfile gone' );
111 unlink $ENV{MX_DAEMON_STDOUT};
112 unlink $ENV{MX_DAEMON_STDERR};