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