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 $!;
47 my $app = FileMaker->new(
48 pidbase => "$dir/subdir",
49 filename => $FILENAME,
51 isa_ok($app, 'FileMaker');
52 does_ok($app, 'MooseX::Daemonize');
53 does_ok($app, 'MooseX::Daemonize::WithPidFile');
54 does_ok($app, 'MooseX::Daemonize::Core');
56 isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File');
58 is($app->pidfile->file, catfile("$dir/subdir", "filemaker.pid"), '... got the right PID file path');
59 ok(not(-e $app->pidfile->file), '... our pidfile does not exist');
61 ok(!$app->status, '... the daemon is running');
62 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
64 ok($app->stop, '... the app will stop cause its not running');
65 is($app->status_message, "Not running", '... got the correct status message');
66 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
70 ok($app->start, '... daemon started');
71 is($app->status_message, "Start succeeded", '... got the correct status message');
72 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
74 sleep(1); # give it a second ...
76 ok(-e $app->pidfile->file, '... our pidfile exists' );
78 my $pid = $app->pidfile->pid;
79 isnt($pid, $$, '... the pid in our pidfile is correct (and not us)');
81 ok($app->status, '... the daemon is running');
82 is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message');
83 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
87 diag "Status is: " . $app->status_message;
90 ok( -e $app->filename, "file exists" );
94 diag "Status is: " . $app->status_message;
97 ok( $app->stop, '... app stopped' );
98 is($app->status_message, "Stop succeeded", '... got the correct status message');
99 is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code');
101 ok(!$app->status, '... the daemon is no longer running');
102 is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message');
103 is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code');
107 diag "Status is: " . $app->status_message;
110 ok( not(-e $app->pidfile->file) , '... pidfile gone' );
113 unlink $ENV{MX_DAEMON_STDOUT};
114 unlink $ENV{MX_DAEMON_STDERR};