X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F01.filecreate.t;h=d4e977af93b6205632243fc70fb2d4849fe7ae17;hb=b6b69acaef633259ece4dcf3c62625d3d5097965;hp=a0e27d958c0551cc52f3ea7aa959c22a148b9022;hpb=a49526793f4ad674abfd7670f4c6a0de9b6df265;p=gitmo%2FMooseX-Daemonize.git diff --git a/t/01.filecreate.t b/t/01.filecreate.t index a0e27d9..d4e977a 100644 --- a/t/01.filecreate.t +++ b/t/01.filecreate.t @@ -1,10 +1,23 @@ -use Test::More no_plan => 1; -use Proc::Daemon; +#!/usr/bin/perl + +use strict; +use warnings; use Cwd; +use File::Spec::Functions; + +use Test::More tests => 29; +use Test::Moose; + +BEGIN { + use_ok('MooseX::Daemonize'); +} + +use constant DEBUG => 0; -## Since a daemon will not be able to print terminal output, we -## have a test daemon create a file, and another process test for -## its existence. +my $CWD = Cwd::cwd; +my $FILENAME = "$CWD/im_alive"; +$ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt'); +$ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt'); { @@ -12,34 +25,87 @@ use Cwd; use Moose; with qw(MooseX::Daemonize); + has filename => ( isa => 'Str', is => 'ro' ); + + after start => sub { + my $self = shift; + if ($self->is_daemon) { + $self->create_file( $self->filename ); + } + }; + sub create_file { my ( $self, $file ) = @_; - open( FILE, ">$file" ) || die $!; - close(FILE); + open( my $FILE, ">$file" ) || die $!; + close($FILE); } - - no Moose; } -package main; +my $app = FileMaker->new( + pidbase => $CWD, + filename => $FILENAME, +); +isa_ok($app, 'FileMaker'); +does_ok($app, 'MooseX::Daemonize'); +does_ok($app, 'MooseX::Daemonize::WithPidFile'); +does_ok($app, 'MooseX::Daemonize::Core'); + +isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File'); + +is($app->pidfile->file, "$CWD/filemaker.pid", '... got the right PID file path'); +ok(not(-e $app->pidfile->file), '... our pidfile does not exist'); + +ok(!$app->status, '... the daemon is running'); +is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); + +ok($app->stop, '... the app will stop cause its not running'); +is($app->status_message, "Not running", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); + +diag $$ if DEBUG; -## Try to make sure we are in the test directory -my $cwd = Cwd::cwd(); -chdir 't' if ( $cwd !~ m|/t$| ); -$cwd = Cwd::cwd(); +ok($app->start, '... daemon started'); +is($app->status_message, "Start succeeded", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); -## 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); +sleep(1); # give it a second ... + +ok(-e $app->pidfile->file, '... our pidfile exists' ); + +my $pid = $app->pidfile->pid; +isnt($pid, $$, '... the pid in our pidfile is correct (and not us)'); + +ok($app->status, '... the daemon is running'); +is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); + +if (DEBUG) { + diag `ps $pid`; + diag "Status is: " . $app->status_message; +} + +ok( -e $app->filename, "file exists" ); + +if (DEBUG) { + diag `ps $pid`; + diag "Status is: " . $app->status_message; } -else { - my $daemon = FileMaker->new(pidbase => '.'); - $daemon->start(); - $daemon->create_file($file); - $daemon->stop(); - exit; + +ok( $app->stop, '... app stopped' ); +is($app->status_message, "Stop succeeded", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); + +ok(!$app->status, '... the daemon is no longer running'); +is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message'); +is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); + +if (DEBUG) { + diag `ps $pid`; + diag "Status is: " . $app->status_message; } + +ok( not(-e $app->pidfile->file) , '... pidfile gone' ); + +unlink $FILENAME; +unlink $ENV{MX_DAEMON_STDOUT}; +unlink $ENV{MX_DAEMON_STDERR};