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