0.08
[gitmo/MooseX-Daemonize.git] / t / 01.filecreate.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use File::Spec::Functions;
6
7 use Test::More tests => 29;
8 use Test::Moose;
9
10 use File::Temp qw(tempdir);
11
12 my $dir = tempdir( CLEANUP => 1 );
13
14 BEGIN {
15     use_ok('MooseX::Daemonize');
16 }
17
18 use constant DEBUG => 0;
19
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');
23
24 {
25
26     package FileMaker;
27     use Moose;
28     with qw(MooseX::Daemonize);
29
30     has filename => ( isa => 'Str', is => 'ro' );
31     
32     after start => sub { 
33         my $self = shift;
34         if ($self->is_daemon) {
35             $self->create_file( $self->filename );
36         }
37     };
38
39     sub create_file {
40         my ( $self, $file ) = @_;
41         open( my $FILE, ">$file" ) || die $!;
42         close($FILE);
43         sleep 1 while 1;
44     }
45 }
46
47 my $app = FileMaker->new(
48     pidbase  => $dir,
49     filename => $FILENAME,
50 );
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
58 is($app->pidfile->file, catfile($dir, "filemaker.pid"), '... got the right PID file path');
59 ok(not(-e $app->pidfile->file), '... our pidfile does not exist');
60
61 ok(!$app->status, '... the daemon is running');
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');
67
68 diag $$ if DEBUG;
69
70 ok($app->start, '... daemon started');
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
74 sleep(1); # give it a second ...
75
76 ok(-e $app->pidfile->file, '... our pidfile exists' );
77
78 my $pid = $app->pidfile->pid;
79 isnt($pid, $$, '... the pid in our pidfile is correct (and not us)');
80
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
85 if (DEBUG) {
86     diag `ps $pid`;
87     diag "Status is: " . $app->status_message;    
88 }
89
90 ok( -e $app->filename, "file exists" );
91
92 if (DEBUG) {
93     diag `ps $pid`;
94     diag "Status is: " . $app->status_message;    
95 }
96
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
101 ok(!$app->status, '... the daemon is no longer running');
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');
104
105 if (DEBUG) {
106     diag `ps $pid`;
107     diag "Status is: " . $app->status_message;    
108 }
109
110 ok( not(-e $app->pidfile->file) , '... pidfile gone' );
111
112 unlink $FILENAME;
113 unlink $ENV{MX_DAEMON_STDOUT};
114 unlink $ENV{MX_DAEMON_STDERR};