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