convert uses of Test::Exception to Test::Fatal
[gitmo/MooseX-Daemonize.git] / t / 30.with_pid_file.t
CommitLineData
8ac4733f 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8ac4733f 6use File::Spec::Functions;
7
10769ed3 8use Test::More 'no_plan';
e7d94e6a 9use Test::Fatal;
8ac4733f 10use Test::Moose;
380acf65 11use File::Temp qw(tempdir);
12
13my $dir = tempdir( CLEANUP => 1 );
14
8ac4733f 15
16BEGIN {
17 use_ok('MooseX::Daemonize::Core');
18}
19
d02fc704 20use constant DEBUG => 0;
21
380acf65 22my $PIDFILE = catfile($dir, 'test-app.pid');
23$ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt');
24$ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt');
8ac4733f 25
26{
27 package MyFooDaemon;
28 use Moose;
29
d02fc704 30 with 'MooseX::Daemonize::WithPidFile';
8ac4733f 31
32 sub init_pidfile {
33 MooseX::Daemonize::Pid::File->new( file => $PIDFILE )
34 }
35
36 sub start {
37 my $self = shift;
38
d02fc704 39 # this tests our bad PID
40 # cleanup functionality.
92cf56b7 41 print "Our parent PID is " . $self->pidfile->pid . "\n" if ::DEBUG;
d02fc704 42
8ac4733f 43 $self->daemonize;
44 return unless $self->is_daemon;
45
8ac4733f 46 # make it easy to find with ps
92cf56b7 47 $0 = 'test-app-2';
8ac4733f 48 $SIG{INT} = sub {
49 print "Got INT! Oh Noes!";
50 $self->pidfile->remove;
51 exit;
52 };
53 while (1) {
54 print "Hello from $$\n";
55 sleep(10);
56 }
57 exit;
58 }
59}
60
61my $d = MyFooDaemon->new( pidfile => $PIDFILE );
62isa_ok($d, 'MyFooDaemon');
63does_ok($d, 'MooseX::Daemonize::Core');
64does_ok($d, 'MooseX::Daemonize::WithPidFile');
65
66ok($d->has_pidfile, '... we have a pidfile value');
67
68{
69 my $p = $d->pidfile;
70 isa_ok($p, 'MooseX::Daemonize::Pid::File');
71 #diag $p->dump;
72}
73
74ok(!(-e $PIDFILE), '... the PID file does not exist yet');
75
e7d94e6a 76is(
77 exception { $d->start },
78 undef,
79 '... successfully daemonized from (' . $$ . ')',
80);
8ac4733f 81
82my $p = $d->pidfile;
83isa_ok($p, 'MooseX::Daemonize::Pid::File');
84#diag $p->dump;
85
86sleep(2);
87
88ok($p->does_file_exist, '... the PID file exists');
89ok($p->is_running, '... the daemon process is running (' . $p->pid . ')');
90
91my $pid = $p->pid;
d02fc704 92if (DEBUG) {
93 diag `ps $pid`;
94 diag "-------";
95 diag `ps -x | grep test-app`;
96 diag "-------";
97 diag "killing $pid";
98}
8ac4733f 99kill INT => $p->pid;
d02fc704 100diag "killed $pid" if DEBUG;
8ac4733f 101sleep(2);
d02fc704 102if (DEBUG) {
103 diag `ps $pid`;
104 diag "-------";
105 diag `ps -x | grep test-app`;
106}
8ac4733f 107
108ok(!$p->is_running, '... the daemon process is no longer running (' . $p->pid . ')');
109ok(!(-e $PIDFILE), '... the PID file has been removed');
110
fe0eeebc 111unlink $ENV{MX_DAEMON_STDOUT};
112unlink $ENV{MX_DAEMON_STDERR};