bcb6ba18ed1166fbf4bc2a912c591efefe5881aa
[gitmo/MooseX-Daemonize.git] / t / 10.pidfile.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 25;
5 use Test::Fatal;
6
7 BEGIN {
8     use_ok('MooseX::Daemonize::Pid::File');
9 }
10
11 {
12     my $f = MooseX::Daemonize::Pid::File->new(
13         file => [ 't', 'foo.pid' ]
14     );
15     isa_ok($f, 'MooseX::Daemonize::Pid::File');
16
17     isa_ok($f->file, 'Path::Class::File');
18
19     is($f->pid, $$, '... the PID is our current process');
20
21     is(
22         exception { $f->write },
23         undef,
24         '... writing the PID file',
25     );
26
27     is($f->file->slurp(chomp => 1), $f->pid, '... the PID in the file is correct');
28     
29     ok($f->is_running, '... it is running too');
30
31     is(
32         exception { $f->remove },
33         undef,
34         '... removing the PID file',
35     );
36
37     ok(!-e $f->file, '... the PID file does not exist anymore');
38 }
39
40 {
41     my $f = MooseX::Daemonize::Pid::File->new(
42         file => [ 't', 'bar.pid' ]
43     );
44     isa_ok($f, 'MooseX::Daemonize::Pid::File');
45
46     isa_ok($f->file, 'Path::Class::File');
47
48     is(
49         exception { $f->write },
50         undef,
51         '... writing the PID file',
52     );
53
54     is($f->file->slurp(chomp => 1), $f->pid, '... the PID in the file is correct');
55     is($f->pid, $$, '... the PID is our current process');
56     
57     ok($f->is_running, '... it is running too');    
58
59     is(
60         exception { $f->remove },
61         undef,
62         '... removing the PID file',
63     );
64
65     ok(!-e $f->file, '... the PID file does not exist anymore');
66 }
67
68 {
69     my $PID = 9999;
70     
71     my $f = MooseX::Daemonize::Pid::File->new(
72         file => [ 't', 'baz.pid' ],
73         pid  => $PID,
74     );
75     isa_ok($f, 'MooseX::Daemonize::Pid::File');
76
77     isa_ok($f->file, 'Path::Class::File');
78     
79     is($f->pid, $PID, '... the PID is our made up PID');
80
81     is(
82         exception { $f->write },
83         undef,
84         '... writing the PID file',
85     );
86
87     is($f->file->slurp(chomp => 1), $f->pid, '... the PID in the file is correct');
88
89     ok(!$f->is_running, '... it is not running (cause we made the PID up)');
90
91     is(
92         exception { $f->remove },
93         undef,
94         '... removing the PID file',
95     );
96
97     ok(!-e $f->file, '... the PID file does not exist anymore');
98 }