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