convert uses of Test::Exception to Test::Fatal
[gitmo/MooseX-Daemonize.git] / t / 21.core-back-compat.t
CommitLineData
2ecc2ccb 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More 'no_plan';
e7d94e6a 7use Test::Fatal;
2ecc2ccb 8use Test::Moose;
9use File::Temp qw(tempdir);
10use File::Spec::Functions;
11
12my $dir = tempdir( CLEANUP => 1 );
13
14BEGIN {
15 use_ok('MooseX::Daemonize::Core');
16 use_ok('MooseX::Daemonize::Pid');
17}
18
19use constant DEBUG => 0;
20
21$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
22$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
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 $dir;
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
e7d94e6a 72is(
73 exception { $d->start },
74 undef,
75 '... successfully daemonized from (' . $$ . ')',
76);
2ecc2ccb 77
78my $p = $d->daemon_pid;
79isa_ok($p, 'MooseX::Daemonize::Pid');
80
81ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
82
83my $pid = $p->pid;
84if (DEBUG) {
85 diag `ps $pid`;
86 diag "-------";
87 diag `ps -x | grep test-app`;
88 diag "-------";
89 diag "killing $pid";
90}
91kill INT => $p->pid;
92diag "killed $pid" if DEBUG;
93sleep(2);
94if (DEBUG) {
95 diag `ps $pid`;
96 diag "-------";
97 diag `ps -x | grep test-app`;
98}
99
100ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
101
102unlink $ENV{MX_DAEMON_STDOUT};
103unlink $ENV{MX_DAEMON_STDERR};
104
105