bunch of stuff
[gitmo/MooseX-Daemonize.git] / t / 01.filecreate.t
index 01cc2b7..8f108f8 100644 (file)
@@ -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};