okay NOW I think Core is done
[gitmo/MooseX-Daemonize.git] / t / 20.core.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Cwd;
7
8 use Test::More no_plan => 1;
9 use Test::Exception;
10 use Test::Moose;
11
12 BEGIN {
13     use_ok('MooseX::Daemonize::Core');
14     use_ok('MooseX::Daemonize::Pid');    
15 }
16
17 my $CWD = Cwd::cwd;
18
19 {
20     package MyFooDaemon;
21     use Moose;
22     
23     with 'MooseX::Daemonize::Core';
24     
25     has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid');
26     
27     # capture the PID from the fork
28     around 'daemon_fork' => sub {
29         my $next = shift;
30         my $self = shift;
31         if (my $pid = $self->$next(@_)) {
32             $self->daemon_pid(
33                 MooseX::Daemonize::Pid->new(pid => $pid)
34             );
35         }
36     };
37     
38     sub start {
39         my $self = shift;  
40         # tell it to ignore zombies ...
41         $self->daemonize(
42             ignore_zombies => 1,
43             no_double_fork => 1,
44         );
45         return unless $self->is_daemon;
46         # change to our local dir
47         # so that we can debug easier
48         chdir $CWD;
49         # make it easy to find with ps
50         $0 = 'test-app';
51         $SIG{INT} = sub { 
52             print "Got INT! Oh Noes!"; 
53             exit;
54         };      
55         while (1) {
56             print "Hello from $$\n"; 
57             sleep(10);       
58         }
59         exit;
60     }
61 }
62
63 my $d = MyFooDaemon->new;
64 isa_ok($d, 'MyFooDaemon');
65 does_ok($d, 'MooseX::Daemonize::Core');
66
67 lives_ok {
68     $d->start;
69 } '... successfully daemonized from (' . $$ . ')';
70
71 my $p = $d->daemon_pid;
72 isa_ok($p, 'MooseX::Daemonize::Pid');
73
74 ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
75
76 my $pid = $p->pid;
77 #diag `ps $pid`;
78 #diag "-------";
79 #diag `ps -x | grep test-app`;
80 #diag "-------";
81 #diag "killing $pid";
82 kill INT => $p->pid;
83 #diag "killed $pid";
84 sleep(2);
85 #diag `ps $pid`;
86 #diag "-------";
87 #diag `ps -x | grep test-app`;
88
89 ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
90
91