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