X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F01.filecreate.t;h=b0af491244829e12d93f5080e1d37c3a8213826a;hb=10769ed347f340ee0e81d85933ae8f82ff0413da;hp=fba64ec1dca9c03d4ade8ba0601936b0399f0f85;hpb=3543c999585655814699a2bbe30cb308823a5c6d;p=gitmo%2FMooseX-Daemonize.git diff --git a/t/01.filecreate.t b/t/01.filecreate.t index fba64ec..b0af491 100644 --- a/t/01.filecreate.t +++ b/t/01.filecreate.t @@ -1,10 +1,25 @@ -use Test::More tests => 4; -use Test::MooseX::Daemonize; -use MooseX::Daemonize; +#!/usr/bin/perl -## 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. +use strict; +use warnings; +use File::Spec::Functions; + +use Test::More tests => 29; +use Test::Moose; + +use File::Temp qw(tempdir); + +my $dir = tempdir( CLEANUP => 1 ); + +BEGIN { + use_ok('MooseX::Daemonize'); +} + +use constant DEBUG => 0; + +my $FILENAME = catfile($dir, "im_alive"); +$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt'); +$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt'); { @@ -13,30 +28,87 @@ use MooseX::Daemonize; with qw(MooseX::Daemonize); has filename => ( isa => 'Str', is => 'ro' ); - after start => sub { $_[0]->create_file( $_[0]->filename ) }; + + after start => sub { + my $self = shift; + if ($self->is_daemon) { + $self->create_file( $self->filename ); + } + }; sub create_file { my ( $self, $file ) = @_; open( my $FILE, ">$file" ) || die $!; close($FILE); + sleep 1 while 1; } +} + +my $app = FileMaker->new( + pidbase => $dir, + 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, catfile($dir, "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'); - no Moose; +diag $$ if DEBUG; + +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'); + +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; } -package main; -use Cwd; +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'); -## Try to make sure we are in the test directory -chdir 't' if ( Cwd::cwd() !~ m|/t$| ); -my $cwd = Cwd::cwd(); +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'); -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( -e $app->pidfile == undef, 'pidfile gone' ); -unlink( $app->filename ); +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};