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