RT#85229 - wait longer for processes to die, when needed
[gitmo/MooseX-Daemonize.git] / t / 20.core.t
index 0f14dab..f3bda2c 100644 (file)
@@ -1,10 +1,8 @@
-#!/usr/bin/perl
-
 use strict;
 use warnings;
 
-use Test::More 'no_plan';
-use Test::Exception;
+use Test::More;
+use Test::Fatal;
 use Test::Moose;
 use File::Temp qw(tempdir);
 use File::Spec::Functions;
@@ -13,7 +11,7 @@ my $dir = tempdir( CLEANUP => 1 );
 
 BEGIN {
     use_ok('MooseX::Daemonize::Core');
-    use_ok('MooseX::Daemonize::Pid');    
+    use_ok('MooseX::Daemonize::Pid');
 }
 
 use constant DEBUG => 0;
@@ -24,11 +22,11 @@ $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
 {
     package MyFooDaemon;
     use Moose;
-    
+
     with 'MooseX::Daemonize::Core';
-    
+
     has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid');
-    
+
     # capture the PID from the fork
     around 'daemon_fork' => sub {
         my $next = shift;
@@ -39,27 +37,26 @@ $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
             );
         }
     };
-    
+
     sub start {
-        my $self = shift;  
+        my $self = shift;
         # tell it to ignore zombies ...
-        $self->daemonize(
-            ignore_zombies => 1,
-            no_double_fork => 1,
-        );
+        $self->ignore_zombies( 1 );
+        $self->no_double_fork( 1 );
+        $self->daemonize;
         return unless $self->is_daemon;
         # change to our local dir
         # so that we can debug easier
         chdir $dir;
         # make it easy to find with ps
         $0 = 'test-app';
-        $SIG{INT} = sub { 
-            print "Got INT! Oh Noes!"; 
+        $SIG{INT} = sub {
+            print "Got INT! Oh Noes!";
             exit;
-        };      
+        };
         while (1) {
-            print "Hello from $$\n"; 
-            sleep(10);       
+            print "Hello from $$\n";
+            sleep(10);
         }
         exit;
     }
@@ -69,9 +66,11 @@ my $d = MyFooDaemon->new;
 isa_ok($d, 'MyFooDaemon');
 does_ok($d, 'MooseX::Daemonize::Core');
 
-lives_ok {
-    $d->start;
-} '... successfully daemonized from (' . $$ . ')';
+is(
+    exception { $d->start },
+    undef,
+    '... successfully daemonized from (' . $$ . ')',
+);
 
 my $p = $d->daemon_pid;
 isa_ok($p, 'MooseX::Daemonize::Pid');
@@ -88,7 +87,14 @@ if (DEBUG) {
 }
 kill INT => $p->pid;
 diag "killed $pid" if DEBUG;
-sleep(2);
+
+# give the process time to be killed on slow/loaded systems
+for (1..10) {
+    last unless kill 0 => $pid;
+    # sleep a little before retrying
+    sleep(2);
+}
+
 if (DEBUG) {
     diag `ps $pid`;
     diag "-------";
@@ -100,4 +106,5 @@ ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid .
 unlink $ENV{MX_DAEMON_STDOUT};
 unlink $ENV{MX_DAEMON_STDERR};
 
+done_testing;