X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F01.filecreate.t;h=8f108f8f9a763d45dca56885c8c8af6b1ddecaf0;hb=5b9ebe08eff2f6410804c2f3c2fa361db0b446a2;hp=01cc2b7bf1d9703ae21c6294d6a0076b58212046;hpb=eaf252a6da8a9b8b1e70c2d491302872df5f71eb;p=gitmo%2FMooseX-Daemonize.git diff --git a/t/01.filecreate.t b/t/01.filecreate.t index 01cc2b7..8f108f8 100644 --- a/t/01.filecreate.t +++ b/t/01.filecreate.t @@ -1,10 +1,19 @@ -use Test::More tests => 4; -use Test::MooseX::Daemonize; +#!/usr/bin/perl + +use strict; +use warnings; +use Cwd; +use File::Spec::Functions; + +use Test::More tests => 9; use MooseX::Daemonize; -## 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 constant DEBUG => 0; + +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'); { @@ -16,8 +25,9 @@ use MooseX::Daemonize; after start => sub { my $self = shift; - $self->create_file( $self->filename ) - if $self->is_daemon; + if ($self->is_daemon) { + $self->create_file( $self->filename ); + } }; sub create_file { @@ -25,25 +35,48 @@ use MooseX::Daemonize; open( my $FILE, ">$file" ) || die $!; close($FILE); } - - no Moose; } -package main; -use strict; -use warnings; -use Cwd; - -## Try to make sure we are in the test directory -chdir 't' if ( Cwd::cwd() !~ m|/t$| ); -my $cwd = Cwd::cwd(); - my $app = FileMaker->new( - pidbase => $cwd, - filename => "$cwd/im_alive", + pidbase => $CWD, + filename => $FILENAME, ); -daemonize_ok( $app, 'child forked okay' ); + +ok(!$app->status, '... the daemon is running'); + +diag $$ if DEBUG; + +ok($app->start, '... daemon started'); +sleep(1); # give it a second ... + +ok($app->status, '... the daemon is running'); + +my $pid = $app->pidfile->pid; +isnt($pid, $$, '... the pid in our pidfile is correct (and not us)'); + +if (DEBUG) { + diag `ps $pid`; + diag "Status is: " . $app->status_message; +} + ok( -e $app->filename, "file exists" ); -ok( $app->stop( no_exit => 1 ), 'app stopped' ); -ok( not(-e $app->pidfile) , 'pidfile gone' ); -unlink( $app->filename ); +ok($app->status, '... the daemon is still running'); + +if (DEBUG) { + diag `ps $pid`; + diag "Status is: " . $app->status_message; +} + +ok( $app->stop, 'app stopped' ); +ok(!$app->status, '... the daemon is no longer running'); + +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};