ok,.. more tests and stuff
[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
8ac4733f 18my $CWD = Cwd::cwd;
19$ENV{MX_DAEMON_STDOUT} = catfile($CWD, 'Out.txt');
20$ENV{MX_DAEMON_STDERR} = catfile($CWD, 'Err.txt');
d8985b7d 21
22{
23 package MyFooDaemon;
24 use Moose;
25
26 with 'MooseX::Daemonize::Core';
27
28 has 'daemon_pid' => (is => 'rw', isa => 'MooseX::Daemonize::Pid');
29
30 # capture the PID from the fork
31 around 'daemon_fork' => sub {
32 my $next = shift;
33 my $self = shift;
34 if (my $pid = $self->$next(@_)) {
35 $self->daemon_pid(
36 MooseX::Daemonize::Pid->new(pid => $pid)
37 );
38 }
39 };
40
41 sub start {
42 my $self = shift;
43 # tell it to ignore zombies ...
44 $self->daemonize(
45 ignore_zombies => 1,
46 no_double_fork => 1,
47 );
48 return unless $self->is_daemon;
49 # change to our local dir
50 # so that we can debug easier
51 chdir $CWD;
52 # make it easy to find with ps
53 $0 = 'test-app';
54 $SIG{INT} = sub {
55 print "Got INT! Oh Noes!";
56 exit;
57 };
58 while (1) {
59 print "Hello from $$\n";
60 sleep(10);
61 }
62 exit;
63 }
64}
65
66my $d = MyFooDaemon->new;
67isa_ok($d, 'MyFooDaemon');
68does_ok($d, 'MooseX::Daemonize::Core');
69
70lives_ok {
71 $d->start;
72} '... successfully daemonized from (' . $$ . ')';
73
74my $p = $d->daemon_pid;
75isa_ok($p, 'MooseX::Daemonize::Pid');
76
77ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
78
79my $pid = $p->pid;
8ac4733f 80diag `ps $pid`;
81diag "-------";
82diag `ps -x | grep test-app`;
83diag "-------";
84diag "killing $pid";
d8985b7d 85kill INT => $p->pid;
8ac4733f 86diag "killed $pid";
d8985b7d 87sleep(2);
8ac4733f 88diag `ps $pid`;
89diag "-------";
90diag `ps -x | grep test-app`;
d8985b7d 91
92ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
93
94