repository moved to https://github.com/moose/MooseX-Daemonize
[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     # find a pid that doesn't currently exist - start by looking at our own
70     # and going backwards (not 100% reliable but better than hardcoding one)
71     my $PID = $$;
72     do { $PID--; $PID = 2**32 if $PID < 1 } while kill(0, $PID);
73     diag 'assigning the non-existent pid ' . $PID;
74
75     my $f = MooseX::Daemonize::Pid::File->new(
76         file => [ 't', 'baz.pid' ],
77         pid  => $PID,
78     );
79     isa_ok($f, 'MooseX::Daemonize::Pid::File');
80
81     isa_ok($f->file, 'Path::Class::File');
82
83     is($f->pid, $PID, '... the PID is our made up PID');
84
85     is(
86         exception { $f->write },
87         undef,
88         '... writing the PID file',
89     );
90
91     is($f->file->slurp(chomp => 1), $f->pid, '... the PID in the file is correct');
92
93     ok(!$f->is_running, '... it is not running (cause we made the PID up)');
94
95     is(
96         exception { $f->remove },
97         undef,
98         '... removing the PID file',
99     );
100
101     ok(!-e $f->file, '... the PID file does not exist anymore');
102 }