okay NOW I think Core is done
[gitmo/MooseX-Daemonize.git] / t / 20.core.t
CommitLineData
d8985b7d 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Cwd;
7
8use Test::More no_plan => 1;
9use Test::Exception;
10use Test::Moose;
11
12BEGIN {
13 use_ok('MooseX::Daemonize::Core');
14 use_ok('MooseX::Daemonize::Pid');
15}
16
17my $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
63my $d = MyFooDaemon->new;
64isa_ok($d, 'MyFooDaemon');
65does_ok($d, 'MooseX::Daemonize::Core');
66
67lives_ok {
68 $d->start;
69} '... successfully daemonized from (' . $$ . ')';
70
71my $p = $d->daemon_pid;
72isa_ok($p, 'MooseX::Daemonize::Pid');
73
74ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
75
76my $pid = $p->pid;
77#diag `ps $pid`;
78#diag "-------";
79#diag `ps -x | grep test-app`;
80#diag "-------";
81#diag "killing $pid";
82kill INT => $p->pid;
83#diag "killed $pid";
84sleep(2);
85#diag `ps $pid`;
86#diag "-------";
87#diag `ps -x | grep test-app`;
88
89ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
90
91