Commit | Line | Data |
5b9ebe08 |
1 | use strict; |
2 | use warnings; |
5b9ebe08 |
3 | use File::Spec::Functions; |
4 | |
92cf56b7 |
5 | use Test::More tests => 29; |
6 | use Test::Moose; |
7 | |
23b6ee85 |
8 | use File::Temp qw(tempdir); |
9 | |
10 | my $dir = tempdir( CLEANUP => 1 ); |
11 | |
92cf56b7 |
12 | BEGIN { |
13 | use_ok('MooseX::Daemonize'); |
14 | } |
a4952679 |
15 | |
5b9ebe08 |
16 | use constant DEBUG => 0; |
17 | |
23b6ee85 |
18 | my $FILENAME = catfile($dir, "im_alive"); |
19 | $ENV{MX_DAEMON_STDOUT} = catfile($dir, 'Out.txt'); |
20 | $ENV{MX_DAEMON_STDERR} = catfile($dir, 'Err.txt'); |
a4952679 |
21 | |
22 | { |
23 | |
24 | package FileMaker; |
25 | use Moose; |
26 | with qw(MooseX::Daemonize); |
27 | |
7d7115e5 |
28 | has filename => ( isa => 'Str', is => 'ro' ); |
f3372ec9 |
29 | |
30 | after start => sub { |
43d819aa |
31 | my $self = shift; |
5b9ebe08 |
32 | if ($self->is_daemon) { |
33 | $self->create_file( $self->filename ); |
34 | } |
43d819aa |
35 | }; |
7d7115e5 |
36 | |
a4952679 |
37 | sub create_file { |
38 | my ( $self, $file ) = @_; |
3543c999 |
39 | open( my $FILE, ">$file" ) || die $!; |
40 | close($FILE); |
91e059f2 |
41 | sleep 1 while 1; |
a4952679 |
42 | } |
a4952679 |
43 | } |
44 | |
3543c999 |
45 | my $app = FileMaker->new( |
e803cff5 |
46 | pidbase => "$dir/subdir", |
5b9ebe08 |
47 | filename => $FILENAME, |
3543c999 |
48 | ); |
92cf56b7 |
49 | isa_ok($app, 'FileMaker'); |
50 | does_ok($app, 'MooseX::Daemonize'); |
51 | does_ok($app, 'MooseX::Daemonize::WithPidFile'); |
52 | does_ok($app, 'MooseX::Daemonize::Core'); |
53 | |
54 | isa_ok($app->pidfile, 'MooseX::Daemonize::Pid::File'); |
55 | |
e803cff5 |
56 | is($app->pidfile->file, catfile("$dir/subdir", "filemaker.pid"), '... got the right PID file path'); |
92cf56b7 |
57 | ok(not(-e $app->pidfile->file), '... our pidfile does not exist'); |
5b9ebe08 |
58 | |
59 | ok(!$app->status, '... the daemon is running'); |
92cf56b7 |
60 | is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); |
61 | |
62 | ok($app->stop, '... the app will stop cause its not running'); |
63 | is($app->status_message, "Not running", '... got the correct status message'); |
64 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
5b9ebe08 |
65 | |
66 | diag $$ if DEBUG; |
67 | |
68 | ok($app->start, '... daemon started'); |
92cf56b7 |
69 | is($app->status_message, "Start succeeded", '... got the correct status message'); |
70 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
71 | |
5b9ebe08 |
72 | sleep(1); # give it a second ... |
73 | |
92cf56b7 |
74 | ok(-e $app->pidfile->file, '... our pidfile exists' ); |
5b9ebe08 |
75 | |
76 | my $pid = $app->pidfile->pid; |
77 | isnt($pid, $$, '... the pid in our pidfile is correct (and not us)'); |
78 | |
92cf56b7 |
79 | ok($app->status, '... the daemon is running'); |
80 | is($app->status_message, "Daemon is running with pid ($pid)", '... got the correct status message'); |
81 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
82 | |
5b9ebe08 |
83 | if (DEBUG) { |
84 | diag `ps $pid`; |
f3372ec9 |
85 | diag "Status is: " . $app->status_message; |
5b9ebe08 |
86 | } |
87 | |
b916501e |
88 | ok( -e $app->filename, "file exists" ); |
5b9ebe08 |
89 | |
90 | if (DEBUG) { |
91 | diag `ps $pid`; |
f3372ec9 |
92 | diag "Status is: " . $app->status_message; |
5b9ebe08 |
93 | } |
94 | |
92cf56b7 |
95 | ok( $app->stop, '... app stopped' ); |
96 | is($app->status_message, "Stop succeeded", '... got the correct status message'); |
97 | is($app->exit_code, MooseX::Daemonize->OK, '... got the right error code'); |
98 | |
5b9ebe08 |
99 | ok(!$app->status, '... the daemon is no longer running'); |
92cf56b7 |
100 | is($app->status_message, "Daemon is not running with pid ($pid)", '... got the correct status message'); |
101 | is($app->exit_code, MooseX::Daemonize->ERROR, '... got the right error code'); |
5b9ebe08 |
102 | |
103 | if (DEBUG) { |
104 | diag `ps $pid`; |
f3372ec9 |
105 | diag "Status is: " . $app->status_message; |
5b9ebe08 |
106 | } |
107 | |
92cf56b7 |
108 | ok( not(-e $app->pidfile->file) , '... pidfile gone' ); |
5b9ebe08 |
109 | |
110 | unlink $FILENAME; |
111 | unlink $ENV{MX_DAEMON_STDOUT}; |
112 | unlink $ENV{MX_DAEMON_STDERR}; |